let KinisiteCssFormatters = { /** * Return a lightened hex colour value from an input hex * * @param hexValue * @param percentage */ lighten: function (hexValue: any, percentage: any) { return KinisiteCssFormatters._colourChange(hexValue, percentage, 'lighten'); }, /** * Return a darkened hex colour value from an input hex * * @param hexValue * @param percentage */ darken: function (hexValue: any, percentage: any) { return KinisiteCssFormatters._colourChange(hexValue, percentage, 'darken'); }, /** * Convert a hex value to RGB octets in comma separated format * * @param hexValue */ hexToRGB(hexValue: any) { let rgb = KinisiteCssFormatters._hexToRGB(hexValue); return String(rgb.r) + "," + String(rgb.g) + "," + String(rgb.b); }, // Internal colour change function _colourChange: function (hexValue: any, percentage: any, mode: any) { let hasHash = String(hexValue).substr(0, 1) == "#"; let rgb = KinisiteCssFormatters._hexToRGB(hexValue); let multiplier = percentage / 100; let r,g,b; if (mode == 'lighten') { r = Math.min(rgb.r + ((255 - rgb.r) * multiplier), 255); g = Math.min(rgb.g + ((255 - rgb.g) * multiplier), 255); b = Math.min(rgb.b + ((255 - rgb.b) * multiplier), 255); } else { r = Math.max(rgb.r - (rgb.r * multiplier), 0); g = Math.max(rgb.g - (rgb.g * multiplier), 0); b = Math.max(rgb.b - (rgb.b * multiplier), 0); } var newColor = b | (g << 8) | (r << 16); hexValue = newColor.toString(16).padStart(6, '0'); return (hasHash ? "#" : "") + hexValue; }, _hexToRGB: function (hexValue: any) { // Remove leading hash if exists let hasHash = String(hexValue).substr(0, 1) == "#"; if (hasHash) { hexValue = String(hexValue).substr(1); } let value = parseInt(hexValue, 16); if (!isNaN(value)) { var r = (value >> 16); var g = ((value >> 8) & 0x00FF); var b = (value & 0x0000FF); return {r, g, b}; } else { return {r: 0, g: 0., b: 0}; } } }; export default KinisiteCssFormatters;