UNPKG

colorconvertjs

Version:

Colour Conversion Utility CMYK HEX HSV RGB HSL

230 lines (229 loc) 6.1 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); /** * Convert rgb To Hex Value * @param r red * @param g green * @param b blue * @return hex */ function rgbToHex(r, g, b) { return '#' + r.toString(16) + g.toString(16) + b.toString(16); } exports.rgbToHex = rgbToHex; /** * Convert RGB to CMYK * @param r Red * @param g Green * @param b Blue * @return [C, M, Y, K] */ function rgbToCmyk(r, g, b) { let _a; const _r = r / 255; const _g = g / 255; const _b = b / 255; let k = 1 - Math.max(_r, _g, _b); let c = (1 - _r - k) / (1 - k); let m = (1 - _g - k) / (1 - k); let y = (1 - _b - k) / (1 - k); (_a = [c, m, y, k].map(function(value) { return Math.round(value * 100); })), (c = _a[0]), (m = _a[1]), (y = _a[2]), (k = _a[3]); return [c, m, y, k]; } exports.rgbToCmyk = rgbToCmyk; /** * Rgb to HSV * @param r Red * @param g Green * @param b Blue * @return [H, S, V] */ function rgbToHsv(r, g, b) { r = r / 255; b = b / 255; g = g / 255; const max = Math.max(r, g, b); const min = Math.min(r, g, b); const diff = max - min; let hue, saturation; if (diff === 0) { hue = 0; } else if (max === r) { var _x = ((g - b) / diff) % 6; hue = 60 * _x; } else if (max === g) { var _x = (b - r) / diff + 2; hue = 60 * _x; } else { var _x = (r - g) / diff + 4; hue = 60 * _x; } if (max === 0) { saturation = 0; } else { saturation = diff / max; } return [Math.round(hue), Math.round(saturation * 100), Math.round(max * 100)]; } exports.rgbToHsv = rgbToHsv; /** * RGB to HSL * @param r Red * @param g Green * @param b Blue * @return [Hue, Saturation, Lightness] */ function rgbToHsl(r, g, b) { r = r / 255; b = b / 255; g = g / 255; const max = Math.max(r, g, b); const min = Math.min(r, g, b); const diff = max - min; const l = (max + min) / 2; let hue, saturation; if (diff === 0) { hue = 0; } else if (max === r) { var _x = ((g - b) / diff) % 6; hue = 60 * _x; } else if (max === g) { var _x = (b - r) / diff + 2; hue = 60 * _x; } else { var _x = (r - g) / diff + 4; hue = 60 * _x; } if (max === 0) { saturation = 0; } else { saturation = diff / (1 - Math.abs(2 * l - 1)); } return [Math.round(hue), Math.round(saturation * 100), Math.round(l * 100)]; } exports.rgbToHsl = rgbToHsl; /** * Convert Hex to Rgb * @param hex hex Color * @return [r,g,b] */ function hexToRgb(hex) { if (!hex.startsWith('#') || (hex.length !== 4 && hex.length !== 7)) { throw 'Hex Value Allowed'; } if (hex.length === 4) { hex = '#' + hex.charAt(1).repeat(2) + hex.charAt(2).repeat(2) + hex.charAt(3).repeat(2); } return [parseInt(hex.substring(1, 3), 16), parseInt(hex.substring(3, 5), 16), parseInt(hex.substring(5), 16)]; } exports.hexToRgb = hexToRgb; /** * Convert CMYK to RGB * @param c Cyan * @param m Magenta * @param y Yellow * @param k Black * @return [r,g,b] */ function cmykToRgb(c, m, y, k) { let _a; let r = 255 * (1 - c / 100) * (1 - k / 100); let g = 255 * (1 - m / 100) * (1 - k / 100); let b = 255 * (1 - y / 100) * (1 - k / 100); (_a = [r, g, b].map(function(val) { return Math.ceil(val); })), (r = _a[0]), (g = _a[1]), (b = _a[2]); return [r, g, b]; } exports.cmykToRgb = cmykToRgb; /** * Convert HSV to RGB * @param h Hue * @param s Saturation * @param v Value * @return [Red, Green , Blue] */ function hsvToRgb(h, s, v) { let _a, _d, _e, _f, _h, _j, _k; s = s / 100; v = v / 100; let r, g, b; const c = v * s; const _c = ((h / 60) % 2) - 1; const x = c * (1 - Math.abs(_c)); const m = v - c; if (h > 360 || h < 0) { throw 'Invalid Input'; } if (0 <= h && h < 60) { (_a = [c, x, 0]), (r = _a[0]), (g = _a[1]), (b = _a[2]); } else if (60 <= h && h < 120) { (_d = [x, c, 0]), (r = _d[0]), (g = _d[1]), (b = _d[2]); } else if (120 <= h && h < 180) { (_e = [0, c, x]), (r = _e[0]), (g = _e[1]), (b = _e[2]); } else if (180 <= h && h < 240) { (_f = [0, x, c]), (r = _f[0]), (g = _f[1]), (b = _f[2]); } else if (240 <= h && h < 300) { (_h = [x, 0, c]), (r = _h[0]), (g = _h[1]), (b = _h[2]); } else { (_j = [c, 0, x]), (r = _j[0]), (g = _j[1]), (b = _j[2]); } (_k = [r + m, g + m, b + m].map(function(value) { return Math.round(value * 255); })), (r = _k[0]), (g = _k[1]), (b = _k[2]); return [r, g, b]; } exports.hsvToRgb = hsvToRgb; /** * HSL to RGB * @param h HUE * @param s Saturation * @param l Lightness * @return [Red, Green , Blue] */ function hslToRgb(h, s, l) { let _a, _d, _e, _f, _h, _j, _k; s = s / 100; l = l / 100; let r, g, b; const c = (1 - Math.abs(2 * l - 1)) * s; const _c = ((h / 60) % 2) - 1; const x = c * (1 - Math.abs(_c)); const m = l - c / 2; if (h > 360 || h < 0) { throw 'Invalid Input'; } if (0 <= h && h < 60) { (_a = [c, x, 0]), (r = _a[0]), (g = _a[1]), (b = _a[2]); } else if (60 <= h && h < 120) { (_d = [x, c, 0]), (r = _d[0]), (g = _d[1]), (b = _d[2]); } else if (120 <= h && h < 180) { (_e = [0, c, x]), (r = _e[0]), (g = _e[1]), (b = _e[2]); } else if (180 <= h && h < 240) { (_f = [0, x, c]), (r = _f[0]), (g = _f[1]), (b = _f[2]); } else if (240 <= h && h < 300) { (_h = [x, 0, c]), (r = _h[0]), (g = _h[1]), (b = _h[2]); } else { (_j = [c, 0, x]), (r = _j[0]), (g = _j[1]), (b = _j[2]); } (_k = [r + m, g + m, b + m].map(function(value) { return Math.round(value * 255); })), (r = _k[0]), (g = _k[1]), (b = _k[2]); return [r, g, b]; } exports.hslToRgb = hslToRgb;