color-convert
Advanced tools
Comparing version 0.6.0 to 0.7.0
1363
conversions.js
/* MIT license */ | ||
module.exports = { | ||
rgb2hsl: rgb2hsl, | ||
rgb2hsv: rgb2hsv, | ||
rgb2hwb: rgb2hwb, | ||
rgb2cmyk: rgb2cmyk, | ||
rgb2keyword: rgb2keyword, | ||
rgb2xyz: rgb2xyz, | ||
rgb2lab: rgb2lab, | ||
rgb2lch: rgb2lch, | ||
rgb2ansi16: rgb2ansi16, | ||
rgb2ansi: rgb2ansi, | ||
rgb2hsl: rgb2hsl, | ||
rgb2hsv: rgb2hsv, | ||
rgb2hwb: rgb2hwb, | ||
rgb2cmyk: rgb2cmyk, | ||
rgb2keyword: rgb2keyword, | ||
rgb2xyz: rgb2xyz, | ||
rgb2lab: rgb2lab, | ||
rgb2lch: rgb2lch, | ||
rgb2ansi16: rgb2ansi16, | ||
rgb2ansi: rgb2ansi, | ||
rgb2hex: rgb2hex, | ||
hsl2rgb: hsl2rgb, | ||
hsl2hsv: hsl2hsv, | ||
hsl2hwb: hsl2hwb, | ||
hsl2cmyk: hsl2cmyk, | ||
hsl2keyword: hsl2keyword, | ||
hsl2ansi16: hsl2ansi16, | ||
hsl2ansi: hsl2ansi, | ||
hsl2rgb: hsl2rgb, | ||
hsl2hsv: hsl2hsv, | ||
hsl2hwb: hsl2hwb, | ||
hsl2cmyk: hsl2cmyk, | ||
hsl2keyword: hsl2keyword, | ||
hsl2ansi16: hsl2ansi16, | ||
hsl2ansi: hsl2ansi, | ||
hsl2hex: hsl2hex, | ||
hsv2rgb: hsv2rgb, | ||
hsv2hsl: hsv2hsl, | ||
hsv2hwb: hsv2hwb, | ||
hsv2cmyk: hsv2cmyk, | ||
hsv2keyword: hsv2keyword, | ||
hsv2ansi16: hsv2ansi16, | ||
hsv2ansi: hsv2ansi, | ||
hsv2rgb: hsv2rgb, | ||
hsv2hsl: hsv2hsl, | ||
hsv2hwb: hsv2hwb, | ||
hsv2cmyk: hsv2cmyk, | ||
hsv2keyword: hsv2keyword, | ||
hsv2ansi16: hsv2ansi16, | ||
hsv2ansi: hsv2ansi, | ||
hsv2hex: hsv2hex, | ||
hwb2rgb: hwb2rgb, | ||
hwb2hsl: hwb2hsl, | ||
hwb2hsv: hwb2hsv, | ||
hwb2cmyk: hwb2cmyk, | ||
hwb2keyword: hwb2keyword, | ||
hwb2ansi16: hwb2ansi16, | ||
hwb2ansi: hwb2ansi, | ||
hwb2rgb: hwb2rgb, | ||
hwb2hsl: hwb2hsl, | ||
hwb2hsv: hwb2hsv, | ||
hwb2cmyk: hwb2cmyk, | ||
hwb2keyword: hwb2keyword, | ||
hwb2ansi16: hwb2ansi16, | ||
hwb2ansi: hwb2ansi, | ||
hwb2hex: hwb2hex, | ||
cmyk2rgb: cmyk2rgb, | ||
cmyk2hsl: cmyk2hsl, | ||
cmyk2hsv: cmyk2hsv, | ||
cmyk2hwb: cmyk2hwb, | ||
cmyk2keyword: cmyk2keyword, | ||
cmyk2ansi16: cmyk2ansi16, | ||
cmyk2ansi: cmyk2ansi, | ||
cmyk2rgb: cmyk2rgb, | ||
cmyk2hsl: cmyk2hsl, | ||
cmyk2hsv: cmyk2hsv, | ||
cmyk2hwb: cmyk2hwb, | ||
cmyk2keyword: cmyk2keyword, | ||
cmyk2ansi16: cmyk2ansi16, | ||
cmyk2ansi: cmyk2ansi, | ||
cmyk2hex: cmyk2hex, | ||
keyword2rgb: keyword2rgb, | ||
keyword2hsl: keyword2hsl, | ||
keyword2hsv: keyword2hsv, | ||
keyword2hwb: keyword2hwb, | ||
keyword2cmyk: keyword2cmyk, | ||
keyword2lab: keyword2lab, | ||
keyword2xyz: keyword2xyz, | ||
keyword2ansi16: keyword2ansi16, | ||
keyword2ansi: keyword2ansi, | ||
keyword2rgb: keyword2rgb, | ||
keyword2hsl: keyword2hsl, | ||
keyword2hsv: keyword2hsv, | ||
keyword2hwb: keyword2hwb, | ||
keyword2cmyk: keyword2cmyk, | ||
keyword2lab: keyword2lab, | ||
keyword2xyz: keyword2xyz, | ||
keyword2ansi16: keyword2ansi16, | ||
keyword2ansi: keyword2ansi, | ||
keyword2hex: keyword2hex, | ||
xyz2rgb: xyz2rgb, | ||
xyz2lab: xyz2lab, | ||
xyz2lch: xyz2lch, | ||
xyz2rgb: xyz2rgb, | ||
xyz2lab: xyz2lab, | ||
xyz2lch: xyz2lch, | ||
lab2xyz: lab2xyz, | ||
lab2rgb: lab2rgb, | ||
lab2lch: lab2lch, | ||
lab2xyz: lab2xyz, | ||
lab2rgb: lab2rgb, | ||
lab2lch: lab2lch, | ||
lch2lab: lch2lab, | ||
lch2xyz: lch2xyz, | ||
lch2rgb: lch2rgb, | ||
lch2lab: lch2lab, | ||
lch2xyz: lch2xyz, | ||
lch2rgb: lch2rgb, | ||
ansi162rgb: ansi162rgb, | ||
ansi162hsl: ansi162hsl, | ||
ansi162hsv: ansi162hsv, | ||
ansi162hwb: ansi162hwb, | ||
ansi162cmyk: ansi162cmyk, | ||
ansi162keyword: ansi162keyword, | ||
ansi162rgb: ansi162rgb, | ||
ansi162hsl: ansi162hsl, | ||
ansi162hsv: ansi162hsv, | ||
ansi162hwb: ansi162hwb, | ||
ansi162cmyk: ansi162cmyk, | ||
ansi162keyword: ansi162keyword, | ||
ansi162hex: ansi162hex, | ||
ansi2rgb: ansi2rgb, | ||
ansi2hsl: ansi2hsl, | ||
ansi2hsv: ansi2hsv, | ||
ansi2hwb: ansi2hwb, | ||
ansi2cmyk: ansi2cmyk, | ||
ansi2keyword: ansi2keyword, | ||
ansi2rgb: ansi2rgb, | ||
ansi2hsl: ansi2hsl, | ||
ansi2hsv: ansi2hsv, | ||
ansi2hwb: ansi2hwb, | ||
ansi2cmyk: ansi2cmyk, | ||
ansi2keyword: ansi2keyword, | ||
ansi2hex: ansi2hex, | ||
hex2rgb: hex2rgb, | ||
hex2hsl: hex2hsl, | ||
hex2hsv: hex2hsv, | ||
hex2hwb: hex2hwb, | ||
hex2cmyk: hex2cmyk, | ||
hex2keyword: hex2keyword, | ||
hex2ansi16: hex2ansi16, | ||
hex2ansi: hex2ansi | ||
}; | ||
var cssKeywords = { | ||
aliceblue: [240, 248, 255], | ||
antiquewhite: [250, 235, 215], | ||
aqua: [0, 255, 255], | ||
aquamarine: [127, 255, 212], | ||
azure: [240, 255, 255], | ||
beige: [245, 245, 220], | ||
bisque: [255, 228, 196], | ||
black: [0, 0, 0], | ||
blanchedalmond: [255, 235, 205], | ||
blue: [0, 0, 255], | ||
blueviolet: [138, 43, 226], | ||
brown: [165, 42, 42], | ||
burlywood: [222, 184, 135], | ||
cadetblue: [95, 158, 160], | ||
chartreuse: [127, 255, 0], | ||
chocolate: [210, 105, 30], | ||
coral: [255, 127, 80], | ||
cornflowerblue: [100, 149, 237], | ||
cornsilk: [255, 248, 220], | ||
crimson: [220, 20, 60], | ||
cyan: [0, 255, 255], | ||
darkblue: [0, 0, 139], | ||
darkcyan: [0, 139, 139], | ||
darkgoldenrod: [184, 134, 11], | ||
darkgray: [169, 169, 169], | ||
darkgreen: [0, 100, 0], | ||
darkgrey: [169, 169, 169], | ||
darkkhaki: [189, 183, 107], | ||
darkmagenta: [139, 0, 139], | ||
darkolivegreen: [85, 107, 47], | ||
darkorange: [255, 140, 0], | ||
darkorchid: [153, 50, 204], | ||
darkred: [139, 0, 0], | ||
darksalmon: [233, 150, 122], | ||
darkseagreen: [143, 188, 143], | ||
darkslateblue: [72, 61, 139], | ||
darkslategray: [47, 79, 79], | ||
darkslategrey: [47, 79, 79], | ||
darkturquoise: [0, 206, 209], | ||
darkviolet: [148, 0, 211], | ||
deeppink: [255, 20, 147], | ||
deepskyblue: [0, 191, 255], | ||
dimgray: [105, 105, 105], | ||
dimgrey: [105, 105, 105], | ||
dodgerblue: [30, 144, 255], | ||
firebrick: [178, 34, 34], | ||
floralwhite: [255, 250, 240], | ||
forestgreen: [34, 139, 34], | ||
fuchsia: [255, 0, 255], | ||
gainsboro: [220, 220, 220], | ||
ghostwhite: [248, 248, 255], | ||
gold: [255, 215, 0], | ||
goldenrod: [218, 165, 32], | ||
gray: [128, 128, 128], | ||
green: [0, 128, 0], | ||
greenyellow: [173, 255, 47], | ||
grey: [128, 128, 128], | ||
honeydew: [240, 255, 240], | ||
hotpink: [255, 105, 180], | ||
indianred: [205, 92, 92], | ||
indigo: [75, 0, 130], | ||
ivory: [255, 255, 240], | ||
khaki: [240, 230, 140], | ||
lavender: [230, 230, 250], | ||
lavenderblush: [255, 240, 245], | ||
lawngreen: [124, 252, 0], | ||
lemonchiffon: [255, 250, 205], | ||
lightblue: [173, 216, 230], | ||
lightcoral: [240, 128, 128], | ||
lightcyan: [224, 255, 255], | ||
lightgoldenrodyellow: [250, 250, 210], | ||
lightgray: [211, 211, 211], | ||
lightgreen: [144, 238, 144], | ||
lightgrey: [211, 211, 211], | ||
lightpink: [255, 182, 193], | ||
lightsalmon: [255, 160, 122], | ||
lightseagreen: [32, 178, 170], | ||
lightskyblue: [135, 206, 250], | ||
lightslategray: [119, 136, 153], | ||
lightslategrey: [119, 136, 153], | ||
lightsteelblue: [176, 196, 222], | ||
lightyellow: [255, 255, 224], | ||
lime: [0, 255, 0], | ||
limegreen: [50, 205, 50], | ||
linen: [250, 240, 230], | ||
magenta: [255, 0, 255], | ||
maroon: [128, 0, 0], | ||
mediumaquamarine: [102, 205, 170], | ||
mediumblue: [0, 0, 205], | ||
mediumorchid: [186, 85, 211], | ||
mediumpurple: [147, 112, 219], | ||
mediumseagreen: [60, 179, 113], | ||
mediumslateblue: [123, 104, 238], | ||
mediumspringgreen: [0, 250, 154], | ||
mediumturquoise: [72, 209, 204], | ||
mediumvioletred: [199, 21, 133], | ||
midnightblue: [25, 25, 112], | ||
mintcream: [245, 255, 250], | ||
mistyrose: [255, 228, 225], | ||
moccasin: [255, 228, 181], | ||
navajowhite: [255, 222, 173], | ||
navy: [0, 0, 128], | ||
oldlace: [253, 245, 230], | ||
olive: [128, 128, 0], | ||
olivedrab: [107, 142, 35], | ||
orange: [255, 165, 0], | ||
orangered: [255, 69, 0], | ||
orchid: [218, 112, 214], | ||
palegoldenrod: [238, 232, 170], | ||
palegreen: [152, 251, 152], | ||
paleturquoise: [175, 238, 238], | ||
palevioletred: [219, 112, 147], | ||
papayawhip: [255, 239, 213], | ||
peachpuff: [255, 218, 185], | ||
peru: [205, 133, 63], | ||
pink: [255, 192, 203], | ||
plum: [221, 160, 221], | ||
powderblue: [176, 224, 230], | ||
purple: [128, 0, 128], | ||
rebeccapurple: [102, 51, 153], | ||
red: [255, 0, 0], | ||
rosybrown: [188, 143, 143], | ||
royalblue: [65, 105, 225], | ||
saddlebrown: [139, 69, 19], | ||
salmon: [250, 128, 114], | ||
sandybrown: [244, 164, 96], | ||
seagreen: [46, 139, 87], | ||
seashell: [255, 245, 238], | ||
sienna: [160, 82, 45], | ||
silver: [192, 192, 192], | ||
skyblue: [135, 206, 235], | ||
slateblue: [106, 90, 205], | ||
slategray: [112, 128, 144], | ||
slategrey: [112, 128, 144], | ||
snow: [255, 250, 250], | ||
springgreen: [0, 255, 127], | ||
steelblue: [70, 130, 180], | ||
tan: [210, 180, 140], | ||
teal: [0, 128, 128], | ||
thistle: [216, 191, 216], | ||
tomato: [255, 99, 71], | ||
turquoise: [64, 224, 208], | ||
violet: [238, 130, 238], | ||
wheat: [245, 222, 179], | ||
white: [255, 255, 255], | ||
whitesmoke: [245, 245, 245], | ||
yellow: [255, 255, 0], | ||
yellowgreen: [154, 205, 50] | ||
}; | ||
var reverseKeywords = {}; | ||
for (var key in cssKeywords) { | ||
if (cssKeywords.hasOwnProperty(key)) { | ||
reverseKeywords[cssKeywords[key].join()] = key; | ||
} | ||
} | ||
function rgb2hsl(rgb) { | ||
var r = rgb[0]/255, | ||
g = rgb[1]/255, | ||
b = rgb[2]/255, | ||
min = Math.min(r, g, b), | ||
max = Math.max(r, g, b), | ||
delta = max - min, | ||
h, s, l; | ||
var r = rgb[0] / 255; | ||
var g = rgb[1] / 255; | ||
var b = rgb[2] / 255; | ||
var min = Math.min(r, g, b); | ||
var max = Math.max(r, g, b); | ||
var delta = max - min; | ||
var h; | ||
var s; | ||
var l; | ||
if (max == min) | ||
h = 0; | ||
else if (r == max) | ||
h = (g - b) / delta; | ||
else if (g == max) | ||
h = 2 + (b - r) / delta; | ||
else if (b == max) | ||
h = 4 + (r - g)/ delta; | ||
if (max === min) { | ||
h = 0; | ||
} else if (r === max) { | ||
h = (g - b) / delta; | ||
} else if (g === max) { | ||
h = 2 + (b - r) / delta; | ||
} else if (b === max) { | ||
h = 4 + (r - g) / delta; | ||
} | ||
h = Math.min(h * 60, 360); | ||
h = Math.min(h * 60, 360); | ||
if (h < 0) | ||
h += 360; | ||
if (h < 0) { | ||
h += 360; | ||
} | ||
l = (min + max) / 2; | ||
l = (min + max) / 2; | ||
if (max == min) | ||
s = 0; | ||
else if (l <= 0.5) | ||
s = delta / (max + min); | ||
else | ||
s = delta / (2 - max - min); | ||
if (max === min) { | ||
s = 0; | ||
} else if (l <= 0.5) { | ||
s = delta / (max + min); | ||
} else { | ||
s = delta / (2 - max - min); | ||
} | ||
return [h, s * 100, l * 100]; | ||
return [h, s * 100, l * 100]; | ||
} | ||
function rgb2hsv(rgb) { | ||
var r = rgb[0], | ||
g = rgb[1], | ||
b = rgb[2], | ||
min = Math.min(r, g, b), | ||
max = Math.max(r, g, b), | ||
delta = max - min, | ||
h, s, v; | ||
var r = rgb[0]; | ||
var g = rgb[1]; | ||
var b = rgb[2]; | ||
var min = Math.min(r, g, b); | ||
var max = Math.max(r, g, b); | ||
var delta = max - min; | ||
var h; | ||
var s; | ||
var v; | ||
if (max == 0) | ||
s = 0; | ||
else | ||
s = (delta/max * 1000)/10; | ||
if (max === 0) { | ||
s = 0; | ||
} else { | ||
s = (delta / max * 1000) / 10; | ||
} | ||
if (max == min) | ||
h = 0; | ||
else if (r == max) | ||
h = (g - b) / delta; | ||
else if (g == max) | ||
h = 2 + (b - r) / delta; | ||
else if (b == max) | ||
h = 4 + (r - g) / delta; | ||
if (max === min) { | ||
h = 0; | ||
} else if (r === max) { | ||
h = (g - b) / delta; | ||
} else if (g === max) { | ||
h = 2 + (b - r) / delta; | ||
} else if (b === max) { | ||
h = 4 + (r - g) / delta; | ||
} | ||
h = Math.min(h * 60, 360); | ||
h = Math.min(h * 60, 360); | ||
if (h < 0) | ||
h += 360; | ||
if (h < 0) { | ||
h += 360; | ||
} | ||
v = ((max / 255) * 1000) / 10; | ||
v = ((max / 255) * 1000) / 10; | ||
return [h, s, v]; | ||
return [h, s, v]; | ||
} | ||
function rgb2hwb(rgb) { | ||
var r = rgb[0], | ||
g = rgb[1], | ||
b = rgb[2], | ||
h = rgb2hsl(rgb)[0], | ||
w = 1/255 * Math.min(r, Math.min(g, b)), | ||
b = 1 - 1/255 * Math.max(r, Math.max(g, b)); | ||
var r = rgb[0]; | ||
var g = rgb[1]; | ||
var b = rgb[2]; | ||
var h = rgb2hsl(rgb)[0]; | ||
var w = 1 / 255 * Math.min(r, Math.min(g, b)); | ||
return [h, w * 100, b * 100]; | ||
b = 1 - 1 / 255 * Math.max(r, Math.max(g, b)); | ||
return [h, w * 100, b * 100]; | ||
} | ||
function rgb2cmyk(rgb) { | ||
var r = rgb[0] / 255, | ||
g = rgb[1] / 255, | ||
b = rgb[2] / 255, | ||
c, m, y, k; | ||
var r = rgb[0] / 255; | ||
var g = rgb[1] / 255; | ||
var b = rgb[2] / 255; | ||
var c; | ||
var m; | ||
var y; | ||
var k; | ||
k = Math.min(1 - r, 1 - g, 1 - b); | ||
c = (1 - r - k) / (1 - k) || 0; | ||
m = (1 - g - k) / (1 - k) || 0; | ||
y = (1 - b - k) / (1 - k) || 0; | ||
return [c * 100, m * 100, y * 100, k * 100]; | ||
k = Math.min(1 - r, 1 - g, 1 - b); | ||
c = (1 - r - k) / (1 - k) || 0; | ||
m = (1 - g - k) / (1 - k) || 0; | ||
y = (1 - b - k) / (1 - k) || 0; | ||
return [c * 100, m * 100, y * 100, k * 100]; | ||
} | ||
function rgb2keyword(rgb) { | ||
return reverseKeywords[JSON.stringify(rgb)]; | ||
return reverseKeywords[rgb.join()]; | ||
} | ||
function rgb2xyz(rgb) { | ||
var r = rgb[0] / 255, | ||
g = rgb[1] / 255, | ||
b = rgb[2] / 255; | ||
var r = rgb[0] / 255; | ||
var g = rgb[1] / 255; | ||
var b = rgb[2] / 255; | ||
// assume sRGB | ||
r = r > 0.04045 ? Math.pow(((r + 0.055) / 1.055), 2.4) : (r / 12.92); | ||
g = g > 0.04045 ? Math.pow(((g + 0.055) / 1.055), 2.4) : (g / 12.92); | ||
b = b > 0.04045 ? Math.pow(((b + 0.055) / 1.055), 2.4) : (b / 12.92); | ||
// assume sRGB | ||
r = r > 0.04045 ? Math.pow(((r + 0.055) / 1.055), 2.4) : (r / 12.92); | ||
g = g > 0.04045 ? Math.pow(((g + 0.055) / 1.055), 2.4) : (g / 12.92); | ||
b = b > 0.04045 ? Math.pow(((b + 0.055) / 1.055), 2.4) : (b / 12.92); | ||
var x = (r * 0.4124) + (g * 0.3576) + (b * 0.1805); | ||
var y = (r * 0.2126) + (g * 0.7152) + (b * 0.0722); | ||
var z = (r * 0.0193) + (g * 0.1192) + (b * 0.9505); | ||
var x = (r * 0.4124) + (g * 0.3576) + (b * 0.1805); | ||
var y = (r * 0.2126) + (g * 0.7152) + (b * 0.0722); | ||
var z = (r * 0.0193) + (g * 0.1192) + (b * 0.9505); | ||
return [x * 100, y *100, z * 100]; | ||
return [x * 100, y * 100, z * 100]; | ||
} | ||
function rgb2lab(rgb) { | ||
var xyz = rgb2xyz(rgb), | ||
x = xyz[0], | ||
y = xyz[1], | ||
z = xyz[2], | ||
l, a, b; | ||
var xyz = rgb2xyz(rgb); | ||
var x = xyz[0]; | ||
var y = xyz[1]; | ||
var z = xyz[2]; | ||
var l; | ||
var a; | ||
var b; | ||
x /= 95.047; | ||
y /= 100; | ||
z /= 108.883; | ||
x /= 95.047; | ||
y /= 100; | ||
z /= 108.883; | ||
x = x > 0.008856 ? Math.pow(x, 1/3) : (7.787 * x) + (16 / 116); | ||
y = y > 0.008856 ? Math.pow(y, 1/3) : (7.787 * y) + (16 / 116); | ||
z = z > 0.008856 ? Math.pow(z, 1/3) : (7.787 * z) + (16 / 116); | ||
x = x > 0.008856 ? Math.pow(x, 1 / 3) : (7.787 * x) + (16 / 116); | ||
y = y > 0.008856 ? Math.pow(y, 1 / 3) : (7.787 * y) + (16 / 116); | ||
z = z > 0.008856 ? Math.pow(z, 1 / 3) : (7.787 * z) + (16 / 116); | ||
l = (116 * y) - 16; | ||
a = 500 * (x - y); | ||
b = 200 * (y - z); | ||
l = (116 * y) - 16; | ||
a = 500 * (x - y); | ||
b = 200 * (y - z); | ||
return [l, a, b]; | ||
return [l, a, b]; | ||
} | ||
function rgb2lch(args) { | ||
return lab2lch(rgb2lab(args)); | ||
return lab2lch(rgb2lab(args)); | ||
} | ||
function hsl2rgb(hsl) { | ||
var h = hsl[0] / 360, | ||
s = hsl[1] / 100, | ||
l = hsl[2] / 100, | ||
t1, t2, t3, rgb, val; | ||
var h = hsl[0] / 360; | ||
var s = hsl[1] / 100; | ||
var l = hsl[2] / 100; | ||
var t1; | ||
var t2; | ||
var t3; | ||
var rgb; | ||
var val; | ||
if (s == 0) { | ||
val = l * 255; | ||
return [val, val, val]; | ||
} | ||
if (s === 0) { | ||
val = l * 255; | ||
return [val, val, val]; | ||
} | ||
if (l < 0.5) | ||
t2 = l * (1 + s); | ||
else | ||
t2 = l + s - l * s; | ||
t1 = 2 * l - t2; | ||
if (l < 0.5) { | ||
t2 = l * (1 + s); | ||
} else { | ||
t2 = l + s - l * s; | ||
} | ||
rgb = [0, 0, 0]; | ||
for (var i = 0; i < 3; i++) { | ||
t3 = h + 1 / 3 * - (i - 1); | ||
t3 < 0 && t3++; | ||
t3 > 1 && t3--; | ||
t1 = 2 * l - t2; | ||
if (6 * t3 < 1) | ||
val = t1 + (t2 - t1) * 6 * t3; | ||
else if (2 * t3 < 1) | ||
val = t2; | ||
else if (3 * t3 < 2) | ||
val = t1 + (t2 - t1) * (2 / 3 - t3) * 6; | ||
else | ||
val = t1; | ||
rgb = [0, 0, 0]; | ||
for (var i = 0; i < 3; i++) { | ||
t3 = h + 1 / 3 * -(i - 1); | ||
if (t3 < 0) { | ||
t3++; | ||
} | ||
if (t3 > 1) { | ||
t3--; | ||
} | ||
rgb[i] = val * 255; | ||
} | ||
if (6 * t3 < 1) { | ||
val = t1 + (t2 - t1) * 6 * t3; | ||
} else if (2 * t3 < 1) { | ||
val = t2; | ||
} else if (3 * t3 < 2) { | ||
val = t1 + (t2 - t1) * (2 / 3 - t3) * 6; | ||
} else { | ||
val = t1; | ||
} | ||
return rgb; | ||
rgb[i] = val * 255; | ||
} | ||
return rgb; | ||
} | ||
function hsl2hsv(hsl) { | ||
var h = hsl[0], | ||
s = hsl[1] / 100, | ||
l = hsl[2] / 100, | ||
sv, v; | ||
var h = hsl[0]; | ||
var s = hsl[1] / 100; | ||
var l = hsl[2] / 100; | ||
var sv; | ||
var v; | ||
if(l === 0) { | ||
// no need to do calc on black | ||
// also avoids divide by 0 error | ||
return [0, 0, 0]; | ||
} | ||
if (l === 0) { | ||
// no need to do calc on black | ||
// also avoids divide by 0 error | ||
return [0, 0, 0]; | ||
} | ||
l *= 2; | ||
s *= (l <= 1) ? l : 2 - l; | ||
v = (l + s) / 2; | ||
sv = (2 * s) / (l + s); | ||
return [h, sv * 100, v * 100]; | ||
l *= 2; | ||
s *= (l <= 1) ? l : 2 - l; | ||
v = (l + s) / 2; | ||
sv = (2 * s) / (l + s); | ||
return [h, sv * 100, v * 100]; | ||
} | ||
function hsl2hwb(args) { | ||
return rgb2hwb(hsl2rgb(args)); | ||
return rgb2hwb(hsl2rgb(args)); | ||
} | ||
function hsl2cmyk(args) { | ||
return rgb2cmyk(hsl2rgb(args)); | ||
return rgb2cmyk(hsl2rgb(args)); | ||
} | ||
function hsl2keyword(args) { | ||
return rgb2keyword(hsl2rgb(args)); | ||
return rgb2keyword(hsl2rgb(args)); | ||
} | ||
function hsv2rgb(hsv) { | ||
var h = hsv[0] / 60, | ||
s = hsv[1] / 100, | ||
v = hsv[2] / 100, | ||
hi = Math.floor(h) % 6; | ||
var h = hsv[0] / 60; | ||
var s = hsv[1] / 100; | ||
var v = hsv[2] / 100; | ||
var hi = Math.floor(h) % 6; | ||
var f = h - Math.floor(h), | ||
p = 255 * v * (1 - s), | ||
q = 255 * v * (1 - (s * f)), | ||
t = 255 * v * (1 - (s * (1 - f))), | ||
v = 255 * v; | ||
var f = h - Math.floor(h); | ||
var p = 255 * v * (1 - s); | ||
var q = 255 * v * (1 - (s * f)); | ||
var t = 255 * v * (1 - (s * (1 - f))); | ||
v *= 255; | ||
switch(hi) { | ||
case 0: | ||
return [v, t, p]; | ||
case 1: | ||
return [q, v, p]; | ||
case 2: | ||
return [p, v, t]; | ||
case 3: | ||
return [p, q, v]; | ||
case 4: | ||
return [t, p, v]; | ||
case 5: | ||
return [v, p, q]; | ||
} | ||
switch (hi) { | ||
case 0: | ||
return [v, t, p]; | ||
case 1: | ||
return [q, v, p]; | ||
case 2: | ||
return [p, v, t]; | ||
case 3: | ||
return [p, q, v]; | ||
case 4: | ||
return [t, p, v]; | ||
case 5: | ||
return [v, p, q]; | ||
} | ||
} | ||
function hsv2hsl(hsv) { | ||
var h = hsv[0], | ||
s = hsv[1] / 100, | ||
v = hsv[2] / 100, | ||
sl, l; | ||
var h = hsv[0]; | ||
var s = hsv[1] / 100; | ||
var v = hsv[2] / 100; | ||
var sl; | ||
var l; | ||
l = (2 - s) * v; | ||
sl = s * v; | ||
sl /= (l <= 1) ? l : 2 - l; | ||
sl = sl || 0; | ||
l /= 2; | ||
return [h, sl * 100, l * 100]; | ||
l = (2 - s) * v; | ||
sl = s * v; | ||
sl /= (l <= 1) ? l : 2 - l; | ||
sl = sl || 0; | ||
l /= 2; | ||
return [h, sl * 100, l * 100]; | ||
} | ||
function hsv2hwb(args) { | ||
return rgb2hwb(hsv2rgb(args)) | ||
return rgb2hwb(hsv2rgb(args)); | ||
} | ||
function hsv2cmyk(args) { | ||
return rgb2cmyk(hsv2rgb(args)); | ||
return rgb2cmyk(hsv2rgb(args)); | ||
} | ||
function hsv2keyword(args) { | ||
return rgb2keyword(hsv2rgb(args)); | ||
return rgb2keyword(hsv2rgb(args)); | ||
} | ||
@@ -350,536 +555,500 @@ | ||
function hwb2rgb(hwb) { | ||
var h = hwb[0] / 360, | ||
wh = hwb[1] / 100, | ||
bl = hwb[2] / 100, | ||
ratio = wh + bl, | ||
i, v, f, n; | ||
var h = hwb[0] / 360; | ||
var wh = hwb[1] / 100; | ||
var bl = hwb[2] / 100; | ||
var ratio = wh + bl; | ||
var i; | ||
var v; | ||
var f; | ||
var n; | ||
// wh + bl cant be > 1 | ||
if (ratio > 1) { | ||
wh /= ratio; | ||
bl /= ratio; | ||
} | ||
// wh + bl cant be > 1 | ||
if (ratio > 1) { | ||
wh /= ratio; | ||
bl /= ratio; | ||
} | ||
i = Math.floor(6 * h); | ||
v = 1 - bl; | ||
f = 6 * h - i; | ||
if ((i & 0x01) != 0) { | ||
f = 1 - f; | ||
} | ||
n = wh + f * (v - wh); // linear interpolation | ||
i = Math.floor(6 * h); | ||
v = 1 - bl; | ||
f = 6 * h - i; | ||
switch (i) { | ||
default: | ||
case 6: | ||
case 0: r = v; g = n; b = wh; break; | ||
case 1: r = n; g = v; b = wh; break; | ||
case 2: r = wh; g = v; b = n; break; | ||
case 3: r = wh; g = n; b = v; break; | ||
case 4: r = n; g = wh; b = v; break; | ||
case 5: r = v; g = wh; b = n; break; | ||
} | ||
if ((i & 0x01) !== 0) { | ||
f = 1 - f; | ||
} | ||
return [r * 255, g * 255, b * 255]; | ||
n = wh + f * (v - wh); // linear interpolation | ||
var r; | ||
var g; | ||
var b; | ||
switch (i) { | ||
default: | ||
case 6: | ||
case 0: r = v; g = n; b = wh; break; | ||
case 1: r = n; g = v; b = wh; break; | ||
case 2: r = wh; g = v; b = n; break; | ||
case 3: r = wh; g = n; b = v; break; | ||
case 4: r = n; g = wh; b = v; break; | ||
case 5: r = v; g = wh; b = n; break; | ||
} | ||
return [r * 255, g * 255, b * 255]; | ||
} | ||
function hwb2hsl(args) { | ||
return rgb2hsl(hwb2rgb(args)); | ||
return rgb2hsl(hwb2rgb(args)); | ||
} | ||
function hwb2hsv(args) { | ||
return rgb2hsv(hwb2rgb(args)); | ||
return rgb2hsv(hwb2rgb(args)); | ||
} | ||
function hwb2cmyk(args) { | ||
return rgb2cmyk(hwb2rgb(args)); | ||
return rgb2cmyk(hwb2rgb(args)); | ||
} | ||
function hwb2keyword(args) { | ||
return rgb2keyword(hwb2rgb(args)); | ||
return rgb2keyword(hwb2rgb(args)); | ||
} | ||
function cmyk2rgb(cmyk) { | ||
var c = cmyk[0] / 100, | ||
m = cmyk[1] / 100, | ||
y = cmyk[2] / 100, | ||
k = cmyk[3] / 100, | ||
r, g, b; | ||
var c = cmyk[0] / 100; | ||
var m = cmyk[1] / 100; | ||
var y = cmyk[2] / 100; | ||
var k = cmyk[3] / 100; | ||
var r; | ||
var g; | ||
var b; | ||
r = 1 - Math.min(1, c * (1 - k) + k); | ||
g = 1 - Math.min(1, m * (1 - k) + k); | ||
b = 1 - Math.min(1, y * (1 - k) + k); | ||
return [r * 255, g * 255, b * 255]; | ||
r = 1 - Math.min(1, c * (1 - k) + k); | ||
g = 1 - Math.min(1, m * (1 - k) + k); | ||
b = 1 - Math.min(1, y * (1 - k) + k); | ||
return [r * 255, g * 255, b * 255]; | ||
} | ||
function cmyk2hsl(args) { | ||
return rgb2hsl(cmyk2rgb(args)); | ||
return rgb2hsl(cmyk2rgb(args)); | ||
} | ||
function cmyk2hsv(args) { | ||
return rgb2hsv(cmyk2rgb(args)); | ||
return rgb2hsv(cmyk2rgb(args)); | ||
} | ||
function cmyk2hwb(args) { | ||
return rgb2hwb(cmyk2rgb(args)); | ||
return rgb2hwb(cmyk2rgb(args)); | ||
} | ||
function cmyk2keyword(args) { | ||
return rgb2keyword(cmyk2rgb(args)); | ||
return rgb2keyword(cmyk2rgb(args)); | ||
} | ||
function xyz2rgb(xyz) { | ||
var x = xyz[0] / 100, | ||
y = xyz[1] / 100, | ||
z = xyz[2] / 100, | ||
r, g, b; | ||
var x = xyz[0] / 100; | ||
var y = xyz[1] / 100; | ||
var z = xyz[2] / 100; | ||
var r; | ||
var g; | ||
var b; | ||
r = (x * 3.2406) + (y * -1.5372) + (z * -0.4986); | ||
g = (x * -0.9689) + (y * 1.8758) + (z * 0.0415); | ||
b = (x * 0.0557) + (y * -0.2040) + (z * 1.0570); | ||
r = (x * 3.2406) + (y * -1.5372) + (z * -0.4986); | ||
g = (x * -0.9689) + (y * 1.8758) + (z * 0.0415); | ||
b = (x * 0.0557) + (y * -0.2040) + (z * 1.0570); | ||
// assume sRGB | ||
r = r > 0.0031308 ? ((1.055 * Math.pow(r, 1.0 / 2.4)) - 0.055) | ||
: r = (r * 12.92); | ||
// assume sRGB | ||
r = r > 0.0031308 | ||
? ((1.055 * Math.pow(r, 1.0 / 2.4)) - 0.055) | ||
: r *= 12.92; | ||
g = g > 0.0031308 ? ((1.055 * Math.pow(g, 1.0 / 2.4)) - 0.055) | ||
: g = (g * 12.92); | ||
g = g > 0.0031308 | ||
? ((1.055 * Math.pow(g, 1.0 / 2.4)) - 0.055) | ||
: g *= 12.92; | ||
b = b > 0.0031308 ? ((1.055 * Math.pow(b, 1.0 / 2.4)) - 0.055) | ||
: b = (b * 12.92); | ||
b = b > 0.0031308 | ||
? ((1.055 * Math.pow(b, 1.0 / 2.4)) - 0.055) | ||
: b *= 12.92; | ||
r = Math.min(Math.max(0, r), 1); | ||
g = Math.min(Math.max(0, g), 1); | ||
b = Math.min(Math.max(0, b), 1); | ||
r = Math.min(Math.max(0, r), 1); | ||
g = Math.min(Math.max(0, g), 1); | ||
b = Math.min(Math.max(0, b), 1); | ||
return [r * 255, g * 255, b * 255]; | ||
return [r * 255, g * 255, b * 255]; | ||
} | ||
function xyz2lab(xyz) { | ||
var x = xyz[0], | ||
y = xyz[1], | ||
z = xyz[2], | ||
l, a, b; | ||
var x = xyz[0]; | ||
var y = xyz[1]; | ||
var z = xyz[2]; | ||
var l; | ||
var a; | ||
var b; | ||
x /= 95.047; | ||
y /= 100; | ||
z /= 108.883; | ||
x /= 95.047; | ||
y /= 100; | ||
z /= 108.883; | ||
x = x > 0.008856 ? Math.pow(x, 1/3) : (7.787 * x) + (16 / 116); | ||
y = y > 0.008856 ? Math.pow(y, 1/3) : (7.787 * y) + (16 / 116); | ||
z = z > 0.008856 ? Math.pow(z, 1/3) : (7.787 * z) + (16 / 116); | ||
x = x > 0.008856 ? Math.pow(x, 1 / 3) : (7.787 * x) + (16 / 116); | ||
y = y > 0.008856 ? Math.pow(y, 1 / 3) : (7.787 * y) + (16 / 116); | ||
z = z > 0.008856 ? Math.pow(z, 1 / 3) : (7.787 * z) + (16 / 116); | ||
l = (116 * y) - 16; | ||
a = 500 * (x - y); | ||
b = 200 * (y - z); | ||
l = (116 * y) - 16; | ||
a = 500 * (x - y); | ||
b = 200 * (y - z); | ||
return [l, a, b]; | ||
return [l, a, b]; | ||
} | ||
function xyz2lch(args) { | ||
return lab2lch(xyz2lab(args)); | ||
return lab2lch(xyz2lab(args)); | ||
} | ||
function lab2xyz(lab) { | ||
var l = lab[0], | ||
a = lab[1], | ||
b = lab[2], | ||
x, y, z, y2; | ||
var l = lab[0]; | ||
var a = lab[1]; | ||
var b = lab[2]; | ||
var x; | ||
var y; | ||
var z; | ||
var y2; | ||
if (l <= 8) { | ||
y = (l * 100) / 903.3; | ||
y2 = (7.787 * (y / 100)) + (16 / 116); | ||
} else { | ||
y = 100 * Math.pow((l + 16) / 116, 3); | ||
y2 = Math.pow(y / 100, 1/3); | ||
} | ||
if (l <= 8) { | ||
y = (l * 100) / 903.3; | ||
y2 = (7.787 * (y / 100)) + (16 / 116); | ||
} else { | ||
y = 100 * Math.pow((l + 16) / 116, 3); | ||
y2 = Math.pow(y / 100, 1 / 3); | ||
} | ||
x = x / 95.047 <= 0.008856 ? x = (95.047 * ((a / 500) + y2 - (16 / 116))) / 7.787 : 95.047 * Math.pow((a / 500) + y2, 3); | ||
x = x / 95.047 <= 0.008856 | ||
? x = (95.047 * ((a / 500) + y2 - (16 / 116))) / 7.787 | ||
: 95.047 * Math.pow((a / 500) + y2, 3); | ||
z = z / 108.883 <= 0.008859 | ||
? z = (108.883 * (y2 - (b / 200) - (16 / 116))) / 7.787 | ||
: 108.883 * Math.pow(y2 - (b / 200), 3); | ||
z = z / 108.883 <= 0.008859 ? z = (108.883 * (y2 - (b / 200) - (16 / 116))) / 7.787 : 108.883 * Math.pow(y2 - (b / 200), 3); | ||
return [x, y, z]; | ||
return [x, y, z]; | ||
} | ||
function lab2lch(lab) { | ||
var l = lab[0], | ||
a = lab[1], | ||
b = lab[2], | ||
hr, h, c; | ||
var l = lab[0]; | ||
var a = lab[1]; | ||
var b = lab[2]; | ||
var hr; | ||
var h; | ||
var c; | ||
hr = Math.atan2(b, a); | ||
h = hr * 360 / 2 / Math.PI; | ||
if (h < 0) { | ||
h += 360; | ||
} | ||
c = Math.sqrt(a * a + b * b); | ||
return [l, c, h]; | ||
hr = Math.atan2(b, a); | ||
h = hr * 360 / 2 / Math.PI; | ||
if (h < 0) { | ||
h += 360; | ||
} | ||
c = Math.sqrt(a * a + b * b); | ||
return [l, c, h]; | ||
} | ||
function lab2rgb(args) { | ||
return xyz2rgb(lab2xyz(args)); | ||
return xyz2rgb(lab2xyz(args)); | ||
} | ||
function lch2lab(lch) { | ||
var l = lch[0], | ||
c = lch[1], | ||
h = lch[2], | ||
a, b, hr; | ||
var l = lch[0]; | ||
var c = lch[1]; | ||
var h = lch[2]; | ||
var a; | ||
var b; | ||
var hr; | ||
hr = h / 360 * 2 * Math.PI; | ||
a = c * Math.cos(hr); | ||
b = c * Math.sin(hr); | ||
return [l, a, b]; | ||
hr = h / 360 * 2 * Math.PI; | ||
a = c * Math.cos(hr); | ||
b = c * Math.sin(hr); | ||
return [l, a, b]; | ||
} | ||
function lch2xyz(args) { | ||
return lab2xyz(lch2lab(args)); | ||
return lab2xyz(lch2lab(args)); | ||
} | ||
function lch2rgb(args) { | ||
return lab2rgb(lch2lab(args)); | ||
return lab2rgb(lch2lab(args)); | ||
} | ||
function keyword2rgb(keyword) { | ||
return cssKeywords[keyword]; | ||
return cssKeywords[keyword]; | ||
} | ||
function keyword2hsl(args) { | ||
return rgb2hsl(keyword2rgb(args)); | ||
return rgb2hsl(keyword2rgb(args)); | ||
} | ||
function keyword2hsv(args) { | ||
return rgb2hsv(keyword2rgb(args)); | ||
return rgb2hsv(keyword2rgb(args)); | ||
} | ||
function keyword2hwb(args) { | ||
return rgb2hwb(keyword2rgb(args)); | ||
return rgb2hwb(keyword2rgb(args)); | ||
} | ||
function keyword2cmyk(args) { | ||
return rgb2cmyk(keyword2rgb(args)); | ||
return rgb2cmyk(keyword2rgb(args)); | ||
} | ||
function keyword2lab(args) { | ||
return rgb2lab(keyword2rgb(args)); | ||
return rgb2lab(keyword2rgb(args)); | ||
} | ||
function keyword2xyz(args) { | ||
return rgb2xyz(keyword2rgb(args)); | ||
return rgb2xyz(keyword2rgb(args)); | ||
} | ||
function rgb2ansi16(args) { | ||
var r = args[0], | ||
g = args[1], | ||
b = args[2], | ||
value = arguments[1] || rgb2hsv(args)[2]; // hsv2ansi16 optimization | ||
var r = args[0]; | ||
var g = args[1]; | ||
var b = args[2]; | ||
var value = arguments[1] || rgb2hsv(args)[2]; // hsv2ansi16 optimization | ||
value = Math.round(value / 50); | ||
if (value === 0) | ||
return 30; | ||
value = Math.round(value / 50); | ||
var ansi = 30 + | ||
((Math.round(b / 255) << 2) | | ||
(Math.round(g / 255) << 1) | | ||
Math.round(r / 255)); | ||
if (value === 0) { | ||
return 30; | ||
} | ||
if (value === 2) | ||
ansi += 60; | ||
var ansi = 30 | ||
+ ((Math.round(b / 255) << 2) | ||
| (Math.round(g / 255) << 1) | ||
| Math.round(r / 255)); | ||
return ansi; | ||
if (value === 2) { | ||
ansi += 60; | ||
} | ||
return ansi; | ||
} | ||
function rgb2ansi(args) { | ||
var r = args[0], | ||
g = args[1], | ||
b = args[2]; | ||
var r = args[0]; | ||
var g = args[1]; | ||
var b = args[2]; | ||
// we use the extended greyscale palette here, with the exception of | ||
// black and white. normal palette only has 4 greyscale shades. | ||
if (r === g && g === b) { | ||
if (r < 8) | ||
return 16; | ||
if (r > 248) | ||
return 231; | ||
return Math.round(((r - 8) / 247) * 24) + 232; | ||
} | ||
// we use the extended greyscale palette here, with the exception of | ||
// black and white. normal palette only has 4 greyscale shades. | ||
if (r === g && g === b) { | ||
if (r < 8) { | ||
return 16; | ||
} | ||
var ansi = 16 + | ||
(36 * Math.round(r / 255 * 5)) + | ||
(6 * Math.round(g / 255 * 5)) + | ||
Math.round(b / 255 * 5); | ||
if (r > 248) { | ||
return 231; | ||
} | ||
return ansi; | ||
return Math.round(((r - 8) / 247) * 24) + 232; | ||
} | ||
var ansi = 16 | ||
+ (36 * Math.round(r / 255 * 5)) | ||
+ (6 * Math.round(g / 255 * 5)) | ||
+ Math.round(b / 255 * 5); | ||
return ansi; | ||
} | ||
function hsl2ansi16(args) { | ||
return rgb2ansi16(hsl2rgb(args)); | ||
return rgb2ansi16(hsl2rgb(args)); | ||
} | ||
function hsl2ansi(args) { | ||
return rgb2ansi(hsl2rgb(args)); | ||
return rgb2ansi(hsl2rgb(args)); | ||
} | ||
function hsv2ansi16(args) { | ||
return rgb2ansi16(hsv2rgb(args), args[2]); | ||
return rgb2ansi16(hsv2rgb(args), args[2]); | ||
} | ||
function hsv2ansi(args) { | ||
return rgb2ansi(hsv2rgb(args)); | ||
return rgb2ansi(hsv2rgb(args)); | ||
} | ||
function hwb2ansi16(args) { | ||
return rgb2ansi16(hwb2rgb(args)); | ||
return rgb2ansi16(hwb2rgb(args)); | ||
} | ||
function hwb2ansi(args) { | ||
return rgb2ansi(hwb2rgb(args)); | ||
return rgb2ansi(hwb2rgb(args)); | ||
} | ||
function cmyk2ansi16(args) { | ||
return rgb2ansi16(cmyk2rgb(args)); | ||
return rgb2ansi16(cmyk2rgb(args)); | ||
} | ||
function cmyk2ansi(args) { | ||
return rgb2ansi(cmyk2rgb(args)); | ||
return rgb2ansi(cmyk2rgb(args)); | ||
} | ||
function keyword2ansi16(args) { | ||
return rgb2ansi16(keyword2rgb(args)); | ||
return rgb2ansi16(keyword2rgb(args)); | ||
} | ||
function keyword2ansi(args) { | ||
return rgb2ansi(keyword2rgb(args)); | ||
return rgb2ansi(keyword2rgb(args)); | ||
} | ||
function ansi162rgb(args) { | ||
var color = args % 10; | ||
var color = args % 10; | ||
// handle greyscale | ||
if (color === 0 || color === 7) { | ||
if (args > 50) | ||
color += 3.5; | ||
color = color / 10.5 * 255; | ||
return [color, color, color]; | ||
} | ||
// handle greyscale | ||
if (color === 0 || color === 7) { | ||
if (args > 50) { | ||
color += 3.5; | ||
} | ||
var mult = (~~(args > 50) + 1) * 0.5, | ||
r = ((color & 1) * mult) * 255, | ||
g = (((color >> 1) & 1) * mult) * 255, | ||
b = (((color >> 2) & 1) * mult) * 255; | ||
color = color / 10.5 * 255; | ||
return [r, g, b]; | ||
return [color, color, color]; | ||
} | ||
var mult = (~~(args > 50) + 1) * 0.5; | ||
var r = ((color & 1) * mult) * 255; | ||
var g = (((color >> 1) & 1) * mult) * 255; | ||
var b = (((color >> 2) & 1) * mult) * 255; | ||
return [r, g, b]; | ||
} | ||
function ansi162hsl(args) { | ||
return rgb2hsl(ansi162rgb(args)); | ||
return rgb2hsl(ansi162rgb(args)); | ||
} | ||
function ansi162hsv(args) { | ||
return rgb2hsv(ansi162rgb(args)); | ||
return rgb2hsv(ansi162rgb(args)); | ||
} | ||
function ansi162hwb(args) { | ||
return rgb2hwb(ansi162rgb(args)); | ||
return rgb2hwb(ansi162rgb(args)); | ||
} | ||
function ansi162cmyk(args) { | ||
return rgb2cmyk(ansi162rgb(args)); | ||
return rgb2cmyk(ansi162rgb(args)); | ||
} | ||
function ansi162keyword(args) { | ||
return rgb2keyword(ansi162rgb(args)); | ||
return rgb2keyword(ansi162rgb(args)); | ||
} | ||
function ansi2rgb(args) { | ||
// handle greyscale | ||
if (args >= 232) { | ||
var c = (args - 232) * 10 + 8; | ||
return [c, c, c]; | ||
} | ||
// handle greyscale | ||
if (args >= 232) { | ||
var c = (args - 232) * 10 + 8; | ||
return [c, c, c]; | ||
} | ||
args -= 16; | ||
args -= 16; | ||
var rem, | ||
r = Math.floor(args / 36) / 5 * 255, | ||
g = Math.floor((rem = args % 36) / 6) / 5 * 255, | ||
b = (rem % 6) / 5 * 255; | ||
var rem; | ||
var r = Math.floor(args / 36) / 5 * 255; | ||
var g = Math.floor((rem = args % 36) / 6) / 5 * 255; | ||
var b = (rem % 6) / 5 * 255; | ||
return [r, g, b]; | ||
return [r, g, b]; | ||
} | ||
function ansi2hsl(args) { | ||
return rgb2hsl(ansi2rgb(args)); | ||
return rgb2hsl(ansi2rgb(args)); | ||
} | ||
function ansi2hsv(args) { | ||
return rgb2hsv(ansi2rgb(args)); | ||
return rgb2hsv(ansi2rgb(args)); | ||
} | ||
function ansi2hwb(args) { | ||
return rgb2hwb(ansi2rgb(args)); | ||
return rgb2hwb(ansi2rgb(args)); | ||
} | ||
function ansi2cmyk(args) { | ||
return rgb2cmyk(ansi2rgb(args)); | ||
return rgb2cmyk(ansi2rgb(args)); | ||
} | ||
function ansi2keyword(args) { | ||
return rgb2keyword(ansi2rgb(args)); | ||
return rgb2keyword(ansi2rgb(args)); | ||
} | ||
var cssKeywords = { | ||
aliceblue: [240,248,255], | ||
antiquewhite: [250,235,215], | ||
aqua: [0,255,255], | ||
aquamarine: [127,255,212], | ||
azure: [240,255,255], | ||
beige: [245,245,220], | ||
bisque: [255,228,196], | ||
black: [0,0,0], | ||
blanchedalmond: [255,235,205], | ||
blue: [0,0,255], | ||
blueviolet: [138,43,226], | ||
brown: [165,42,42], | ||
burlywood: [222,184,135], | ||
cadetblue: [95,158,160], | ||
chartreuse: [127,255,0], | ||
chocolate: [210,105,30], | ||
coral: [255,127,80], | ||
cornflowerblue: [100,149,237], | ||
cornsilk: [255,248,220], | ||
crimson: [220,20,60], | ||
cyan: [0,255,255], | ||
darkblue: [0,0,139], | ||
darkcyan: [0,139,139], | ||
darkgoldenrod: [184,134,11], | ||
darkgray: [169,169,169], | ||
darkgreen: [0,100,0], | ||
darkgrey: [169,169,169], | ||
darkkhaki: [189,183,107], | ||
darkmagenta: [139,0,139], | ||
darkolivegreen: [85,107,47], | ||
darkorange: [255,140,0], | ||
darkorchid: [153,50,204], | ||
darkred: [139,0,0], | ||
darksalmon: [233,150,122], | ||
darkseagreen: [143,188,143], | ||
darkslateblue: [72,61,139], | ||
darkslategray: [47,79,79], | ||
darkslategrey: [47,79,79], | ||
darkturquoise: [0,206,209], | ||
darkviolet: [148,0,211], | ||
deeppink: [255,20,147], | ||
deepskyblue: [0,191,255], | ||
dimgray: [105,105,105], | ||
dimgrey: [105,105,105], | ||
dodgerblue: [30,144,255], | ||
firebrick: [178,34,34], | ||
floralwhite: [255,250,240], | ||
forestgreen: [34,139,34], | ||
fuchsia: [255,0,255], | ||
gainsboro: [220,220,220], | ||
ghostwhite: [248,248,255], | ||
gold: [255,215,0], | ||
goldenrod: [218,165,32], | ||
gray: [128,128,128], | ||
green: [0,128,0], | ||
greenyellow: [173,255,47], | ||
grey: [128,128,128], | ||
honeydew: [240,255,240], | ||
hotpink: [255,105,180], | ||
indianred: [205,92,92], | ||
indigo: [75,0,130], | ||
ivory: [255,255,240], | ||
khaki: [240,230,140], | ||
lavender: [230,230,250], | ||
lavenderblush: [255,240,245], | ||
lawngreen: [124,252,0], | ||
lemonchiffon: [255,250,205], | ||
lightblue: [173,216,230], | ||
lightcoral: [240,128,128], | ||
lightcyan: [224,255,255], | ||
lightgoldenrodyellow: [250,250,210], | ||
lightgray: [211,211,211], | ||
lightgreen: [144,238,144], | ||
lightgrey: [211,211,211], | ||
lightpink: [255,182,193], | ||
lightsalmon: [255,160,122], | ||
lightseagreen: [32,178,170], | ||
lightskyblue: [135,206,250], | ||
lightslategray: [119,136,153], | ||
lightslategrey: [119,136,153], | ||
lightsteelblue: [176,196,222], | ||
lightyellow: [255,255,224], | ||
lime: [0,255,0], | ||
limegreen: [50,205,50], | ||
linen: [250,240,230], | ||
magenta: [255,0,255], | ||
maroon: [128,0,0], | ||
mediumaquamarine: [102,205,170], | ||
mediumblue: [0,0,205], | ||
mediumorchid: [186,85,211], | ||
mediumpurple: [147,112,219], | ||
mediumseagreen: [60,179,113], | ||
mediumslateblue: [123,104,238], | ||
mediumspringgreen: [0,250,154], | ||
mediumturquoise: [72,209,204], | ||
mediumvioletred: [199,21,133], | ||
midnightblue: [25,25,112], | ||
mintcream: [245,255,250], | ||
mistyrose: [255,228,225], | ||
moccasin: [255,228,181], | ||
navajowhite: [255,222,173], | ||
navy: [0,0,128], | ||
oldlace: [253,245,230], | ||
olive: [128,128,0], | ||
olivedrab: [107,142,35], | ||
orange: [255,165,0], | ||
orangered: [255,69,0], | ||
orchid: [218,112,214], | ||
palegoldenrod: [238,232,170], | ||
palegreen: [152,251,152], | ||
paleturquoise: [175,238,238], | ||
palevioletred: [219,112,147], | ||
papayawhip: [255,239,213], | ||
peachpuff: [255,218,185], | ||
peru: [205,133,63], | ||
pink: [255,192,203], | ||
plum: [221,160,221], | ||
powderblue: [176,224,230], | ||
purple: [128,0,128], | ||
rebeccapurple: [102, 51, 153], | ||
red: [255,0,0], | ||
rosybrown: [188,143,143], | ||
royalblue: [65,105,225], | ||
saddlebrown: [139,69,19], | ||
salmon: [250,128,114], | ||
sandybrown: [244,164,96], | ||
seagreen: [46,139,87], | ||
seashell: [255,245,238], | ||
sienna: [160,82,45], | ||
silver: [192,192,192], | ||
skyblue: [135,206,235], | ||
slateblue: [106,90,205], | ||
slategray: [112,128,144], | ||
slategrey: [112,128,144], | ||
snow: [255,250,250], | ||
springgreen: [0,255,127], | ||
steelblue: [70,130,180], | ||
tan: [210,180,140], | ||
teal: [0,128,128], | ||
thistle: [216,191,216], | ||
tomato: [255,99,71], | ||
turquoise: [64,224,208], | ||
violet: [238,130,238], | ||
wheat: [245,222,179], | ||
white: [255,255,255], | ||
whitesmoke: [245,245,245], | ||
yellow: [255,255,0], | ||
yellowgreen: [154,205,50] | ||
}; | ||
function rgb2hex(args) { | ||
var integer = ((Math.round(args[0]) & 0xFF) << 16) | ||
+ ((Math.round(args[1]) & 0xFF) << 8) | ||
+ (Math.round(args[2]) & 0xFF); | ||
var reverseKeywords = {}; | ||
for (var key in cssKeywords) { | ||
reverseKeywords[JSON.stringify(cssKeywords[key])] = key; | ||
var string = integer.toString(16).toUpperCase(); | ||
return '000000'.substring(string.length) + string; | ||
} | ||
function hsl2hex(args) { | ||
return rgb2hex(hsl2rgb(args)); | ||
} | ||
function hsv2hex(args) { | ||
return rgb2hex(hsv2rgb(args)); | ||
} | ||
function hwb2hex(args) { | ||
return rgb2hex(hwb2rgb(args)); | ||
} | ||
function cmyk2hex(args) { | ||
return rgb2hex(cmyk2rgb(args)); | ||
} | ||
function keyword2hex(args) { | ||
return rgb2hex(keyword2rgb(args)); | ||
} | ||
function ansi162hex(args) { | ||
return rgb2hex(ansi162rgb(args)); | ||
} | ||
function ansi2hex(args) { | ||
return rgb2hex(ansi2rgb(args)); | ||
} | ||
function hex2rgb(args) { | ||
var match = args.toString(16).match(/[a-f0-9]{6}/i); | ||
if (!match) { | ||
return [0, 0, 0]; | ||
} | ||
var integer = parseInt(match[0], 16); | ||
var r = (integer >> 16) & 0xFF; | ||
var g = (integer >> 8) & 0xFF; | ||
var b = integer & 0xFF; | ||
return [r, g, b]; | ||
} | ||
function hex2hsl(args) { | ||
return rgb2hsl(hex2rgb(args)); | ||
} | ||
function hex2hsv(args) { | ||
return rgb2hsv(hex2rgb(args)); | ||
} | ||
function hex2hwb(args) { | ||
return rgb2hwb(hex2rgb(args)); | ||
} | ||
function hex2cmyk(args) { | ||
return rgb2cmyk(hex2rgb(args)); | ||
} | ||
function hex2keyword(args) { | ||
return rgb2keyword(hex2rgb(args)); | ||
} | ||
function hex2ansi16(args) { | ||
return rgb2ansi16(hex2rgb(args)); | ||
} | ||
function hex2ansi(args) { | ||
return rgb2ansi(hex2rgb(args)); | ||
} |
152
index.js
@@ -1,92 +0,108 @@ | ||
var conversions = require("./conversions"); | ||
var conversions = require('./conversions'); | ||
var convert = function() { | ||
return new Converter(); | ||
var convert = function () { | ||
return new Converter(); | ||
}; | ||
function wrapConverter(func) { | ||
// accept array or plain args | ||
return function (arg) { | ||
if (typeof arg === 'number') { | ||
arg = Array.prototype.slice.call(arguments); | ||
} | ||
return conversions[func](arg); | ||
}; | ||
} | ||
function wrapConverterQualified(func) { | ||
return function (arg) { | ||
if (typeof arg === 'number') { | ||
arg = Array.prototype.slice.call(arguments); | ||
} | ||
var val = conversions[func](arg); | ||
if (typeof val === 'string' || val === undefined) { | ||
return val; // keyword | ||
} | ||
for (var i = 0; i < val.length; i++) { | ||
val[i] = Math.round(val[i]); | ||
} | ||
return val; | ||
}; | ||
} | ||
for (var func in conversions) { | ||
// export Raw versions | ||
convert[func + "Raw"] = (function(func) { | ||
// accept array or plain args | ||
return function(arg) { | ||
if (typeof arg == "number") | ||
arg = Array.prototype.slice.call(arguments); | ||
return conversions[func](arg); | ||
} | ||
})(func); | ||
if (!conversions.hasOwnProperty(func)) { | ||
continue; | ||
} | ||
var pair = /(\w+)2(\w+)/.exec(func), | ||
from = pair[1], | ||
to = pair[2]; | ||
// export Raw versions | ||
convert[func + 'Raw'] = wrapConverter(func); | ||
// export rgb2hsl and ["rgb"]["hsl"] | ||
convert[from] = convert[from] || {}; | ||
var pair = /(\w+)2(\w+)/.exec(func); | ||
var from = pair[1]; | ||
var to = pair[2]; | ||
convert[from][to] = convert[func] = (function(func) { | ||
return function(arg) { | ||
if (typeof arg == "number") | ||
arg = Array.prototype.slice.call(arguments); | ||
var val = conversions[func](arg); | ||
if (typeof val == "string" || val === undefined) | ||
return val; // keyword | ||
// export rgb2hsl and ["rgb"]["hsl"] | ||
convert[from] = convert[from] || {}; | ||
for (var i = 0; i < val.length; i++) | ||
val[i] = Math.round(val[i]); | ||
return val; | ||
} | ||
})(func); | ||
convert[from][to] = convert[func] = wrapConverterQualified(func); | ||
} | ||
/* Converter does lazy conversion and caching */ | ||
var Converter = function() { | ||
this.convs = {}; | ||
}; | ||
function Converter() { | ||
this.convs = {}; | ||
} | ||
/* Either get the values for a space or | ||
set the values for a space, depending on args */ | ||
Converter.prototype.routeSpace = function(space, args) { | ||
var values = args[0]; | ||
if (values === undefined) { | ||
// color.rgb() | ||
return this.getValues(space); | ||
} | ||
// color.rgb(10, 10, 10) | ||
if (typeof values == "number") { | ||
values = Array.prototype.slice.call(args); | ||
} | ||
set the values for a space, depending on args */ | ||
Converter.prototype.routeSpace = function (space, args) { | ||
var values = args[0]; | ||
if (values === undefined) { | ||
// color.rgb() | ||
return this.getValues(space); | ||
} | ||
return this.setValues(space, values); | ||
// color.rgb(10, 10, 10) | ||
if (typeof values === 'number') { | ||
values = Array.prototype.slice.call(args); | ||
} | ||
return this.setValues(space, values); | ||
}; | ||
/* Set the values for a space, invalidating cache */ | ||
Converter.prototype.setValues = function(space, values) { | ||
this.space = space; | ||
this.convs = {}; | ||
this.convs[space] = values; | ||
return this; | ||
Converter.prototype.setValues = function (space, values) { | ||
this.space = space; | ||
this.convs = {}; | ||
this.convs[space] = values; | ||
return this; | ||
}; | ||
/* Get the values for a space. If there's already | ||
a conversion for the space, fetch it, otherwise | ||
compute it */ | ||
Converter.prototype.getValues = function(space) { | ||
var vals = this.convs[space]; | ||
if (!vals) { | ||
var fspace = this.space, | ||
from = this.convs[fspace]; | ||
vals = convert[fspace][space](from); | ||
a conversion for the space, fetch it, otherwise | ||
compute it */ | ||
Converter.prototype.getValues = function (space) { | ||
var vals = this.convs[space]; | ||
this.convs[space] = vals; | ||
} | ||
return vals; | ||
if (!vals) { | ||
var fspace = this.space; | ||
var from = this.convs[fspace]; | ||
vals = convert[fspace][space](from); | ||
this.convs[space] = vals; | ||
} | ||
return vals; | ||
}; | ||
["rgb", "hsl", "hsv", "cmyk", "keyword"].forEach(function(space) { | ||
Converter.prototype[space] = function(vals) { | ||
return this.routeSpace(space, arguments); | ||
} | ||
['rgb', 'hsl', 'hsv', 'cmyk', 'keyword'].forEach(function (space) { | ||
Converter.prototype[space] = function () { | ||
return this.routeSpace(space, arguments); | ||
}; | ||
}); | ||
module.exports = convert; | ||
module.exports = convert; |
{ | ||
"name": "color-convert", | ||
"description": "Plain color conversion functions", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"author": "Heather Arthur <fayearthur@gmail.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/harthur/color-convert.git" | ||
}, | ||
"devDependencies": { | ||
}, | ||
"license": "MIT", | ||
"repository": "MoOx/color-convert", | ||
"scripts": { | ||
"pretest": "xo", | ||
"test": "node test/basic.js" | ||
@@ -18,4 +15,27 @@ }, | ||
"colour", | ||
"rgb" | ||
] | ||
"convert", | ||
"converter", | ||
"conversion", | ||
"rgb", | ||
"hsl", | ||
"hsv", | ||
"hwb", | ||
"cmyk", | ||
"ansi", | ||
"ansi16" | ||
], | ||
"files": [ | ||
"index.js", | ||
"conversions.js" | ||
], | ||
"xo": { | ||
"rules": { | ||
"default-case": 0, | ||
"no-inline-comments": 0, | ||
"operator-linebreak": 0 | ||
} | ||
}, | ||
"devDependencies": { | ||
"xo": "^0.11.2" | ||
} | ||
} |
# color-convert | ||
[![Build Status](https://travis-ci.org/harthur/color-convert.svg?branch=master)](https://travis-ci.org/harthur/color-convert) | ||
[![Build Status](https://travis-ci.org/MoOx/color-convert.svg?branch=master)](https://travis-ci.org/MoOx/color-convert) | ||
@@ -5,0 +5,0 @@ Color-convert is a color conversion library for JavaScript and node. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26428
1
6
955
1
2