Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

culori

Package Overview
Dependencies
Maintainers
1
Versions
256
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

culori - npm Package Compare versions

Comparing version 0.2.5 to 0.2.6

src/api/lab.js

711

build/culori.js

@@ -9,7 +9,67 @@ (function (global, factory) {

function from_hsl (_ref) {
function hsi_to_rgb (_ref) {
var h = _ref.h,
s = _ref.s,
i = _ref.i,
alpha = _ref.alpha;
var f = Math.abs(h / 60 % 2 - 1);
var res = void 0;
switch (Math.floor(h / 60)) {
case 0:
res = {
r: i * (1 + s * (3 / (2 - f) - 1)),
g: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
b: i * (1 - s)
};
break;
case 1:
res = {
r: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
g: i * (1 + s * (3 / (2 - f) - 1)),
b: i * (1 - s)
};
break;
case 2:
res = {
r: i * (1 - s),
g: i * (1 + s * (3 / (2 - f) - 1)),
b: i * (1 + s * (3 * (1 - f) / (2 - f) - 1))
};
break;
case 3:
res = {
r: i * (1 - s),
g: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
b: i * (1 + s * (3 / (2 - f) - 1))
};
break;
case 4:
res = {
r: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
g: i * (1 - s),
b: i * (1 + s * (3 / (2 - f) - 1))
};
break;
case 5:
res = {
r: i * (1 + s * (3 / (2 - f) - 1)),
g: i * (1 - s),
b: i * (1 + s * (3 * (1 - f) / (2 - f) - 1))
};
break;
}
res.mode = 'rgb';
if (alpha !== undefined) res.alpha = alpha;
return res;
}
// Based on: https://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
function hsl_to_rgb (_ref) {
var h = _ref.h,
s = _ref.s,
l = _ref.l,
a = _ref.a;
alpha = _ref.alpha;

@@ -34,3 +94,3 @@ var m1 = l + s * (l < 0.5 ? l : 1 - l);

res.mode = 'rgb';
if (a !== undefined) res.a = a;
if (alpha !== undefined) res.alpha = alpha;
return res;

@@ -41,7 +101,7 @@ }

function from_hsv (_ref) {
function hsv_to_rgb (_ref) {
var h = _ref.h,
s = _ref.s,
v = _ref.v,
a = _ref.a;
alpha = _ref.alpha;

@@ -65,63 +125,53 @@ var f = Math.abs(h / 60 % 2 - 1);

res.mode = 'rgb';
if (a !== undefined) res.a = a;
if (alpha !== undefined) res.alpha = alpha;
return res;
}
// Based on: https://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
/*
HWB to RGB converter
--------------------
function from_hsi (_ref) {
References:
* https://drafts.csswg.org/css-color/#hwb-to-rgb
* https://en.wikipedia.org/wiki/HWB_color_model
* http://alvyray.com/Papers/CG/HWB_JGTv208.pdf
*/
function hwb_to_rgb (_ref) {
var h = _ref.h,
s = _ref.s,
i = _ref.i,
a = _ref.a;
w = _ref.w,
b = _ref.b,
alpha = _ref.alpha;
var f = Math.abs(h / 60 % 2 - 1);
var res = void 0;
switch (Math.floor(h / 60)) {
case 0:
res = {
r: i * (1 + s * (3 / (2 - f) - 1)),
g: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
b: i * (1 - s)
};
break;
case 1:
res = {
r: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
g: i * (1 + s * (3 / (2 - f) - 1)),
b: i * (1 - s)
};
break;
case 2:
res = {
r: i * (1 - s),
g: i * (1 + s * (3 / (2 - f) - 1)),
b: i * (1 + s * (3 * (1 - f) / (2 - f) - 1))
};
break;
case 3:
res = {
r: i * (1 - s),
g: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
b: i * (1 + s * (3 / (2 - f) - 1))
};
break;
case 4:
res = {
r: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
g: i * (1 - s),
b: i * (1 + s * (3 / (2 - f) - 1))
};
break;
case 5:
res = {
r: i * (1 + s * (3 / (2 - f) - 1)),
g: i * (1 - s),
b: i * (1 + s * (3 * (1 - f) / (2 - f) - 1))
};
break;
// normalize w + b to 1
if (w + b > 1) {
var s = w + b;
w /= s;
b /= s;
}
return hsv_to_rgb({
h: h,
s: b === 1 ? 1 : 1 - w / (1 - b),
v: 1 - b,
alpha: alpha
});
}
res.mode = 'rgb';
if (a !== undefined) res.a = a;
// Based on: https://en.wikipedia.org/wiki/HSL_and_HSV#Formal_derivation
function rgb_to_hsi (_ref) {
var r = _ref.r,
g = _ref.g,
b = _ref.b,
alpha = _ref.alpha;
var M = Math.max(r, g, b),
m = Math.min(r, g, b);
var res = {
mode: 'hsi',
s: r + g + b === 0 ? 0 : 1 - 3 * m / (r + g + b),
i: (r + g + b) / 3
};
if (M - m !== 0) res.h = (M === r ? (g - b) / (M - m) + (g < b) * 6 : M === g ? (b - r) / (M - m) + 2 : (r - g) / (M - m) + 4) * 60;
if (alpha !== undefined) res.alpha = alpha;
return res;

@@ -132,9 +182,8 @@ }

function to_hsl (_ref) {
function rgb_to_hsl (_ref) {
var r = _ref.r,
g = _ref.g,
b = _ref.b,
a = _ref.a;
alpha = _ref.alpha;
// if (flags & IS_CULORI && flags & IS_HSL) return arguments[0];
var M = Math.max(r, g, b),

@@ -148,3 +197,3 @@ m = Math.min(r, g, b);

if (M - m !== 0) res.h = (M === r ? (g - b) / (M - m) + (g < b) * 6 : M === g ? (b - r) / (M - m) + 2 : (r - g) / (M - m) + 4) * 60;
if (a !== undefined) res.a = a;
if (alpha !== undefined) res.alpha = alpha;
return res;

@@ -155,9 +204,8 @@ }

function to_hsv (_ref) {
function rgb_to_hsv (_ref) {
var r = _ref.r,
g = _ref.g,
b = _ref.b,
a = _ref.a;
alpha = _ref.alpha;
// if (flags & IS_CULORI && flags & IS_HSV) return arguments[0];
var M = Math.max(r, g, b),

@@ -171,66 +219,257 @@ m = Math.min(r, g, b);

if (M - m !== 0) res.h = (M === r ? (g - b) / (M - m) + (g < b) * 6 : M === g ? (b - r) / (M - m) + 2 : (r - g) / (M - m) + 4) * 60;
if (a !== undefined) res.a = a;
if (alpha !== undefined) res.alpha = alpha;
return res;
}
// Based on: https://en.wikipedia.org/wiki/HSL_and_HSV#Formal_derivation
/*
RGB to HWB converter
--------------------
function to_hsi (_ref) {
var r = _ref.r,
g = _ref.g,
b = _ref.b,
a = _ref.a;
References:
* https://drafts.csswg.org/css-color/#hwb-to-rgb
* https://en.wikipedia.org/wiki/HWB_color_model
* http://alvyray.com/Papers/CG/HWB_JGTv208.pdf
*/
// if (flags & IS_CULORI && flags & IS_HSI) return arguments[0];
var M = Math.max(r, g, b),
m = Math.min(r, g, b);
function rgb_to_hwb (rgba) {
var hsv = rgb_to_hsv(rgba);
if (hsv === undefined) return undefined;
var res = {
mode: 'hsi',
s: r + g + b === 0 ? 0 : 1 - 3 * m / (r + g + b),
i: (r + g + b) / 3
mode: 'hwb',
w: (1 - hsv.s) * hsv.v,
b: 1 - hsv.v
};
if (M - m !== 0) res.h = (M === r ? (g - b) / (M - m) + (g < b) * 6 : M === g ? (b - r) / (M - m) + 2 : (r - g) / (M - m) + 4) * 60;
if (a !== undefined) res.a = a;
if (hsv.h !== undefined) res.h = hsv.h;
if (hsv.alpha !== undefined) res.alpha = hsv.alpha;
return res;
}
var convert = function convert(color, mode) {
/*
References:
* https://drafts.csswg.org/css-color/#lab-to-lch
* https://drafts.csswg.org/css-color/#color-conversion-code
*/
var lab_to_lch = (function (_ref) {
var l = _ref.l,
a = _ref.a,
b = _ref.b,
alpha = _ref.alpha;
if (color === undefined) {
return undefined;
}
var res = {
mode: 'lch',
l: l,
c: Math.sqrt(a * a + b * b),
h: Math.atan2(b, a) * 180 / Math.PI
};
if (alpha !== undefined) res.alpha = alpha;
return res;
});
// If the color's in the same mode as needed, just return the color.
if (color.mode === mode) {
return color;
}
/*
References:
* https://drafts.csswg.org/css-color/#lch-to-lab
* https://drafts.csswg.org/css-color/#color-conversion-code
*/
var lch_to_lab = (function (_ref) {
var l = _ref.l,
c = _ref.c,
h = _ref.h,
alpha = _ref.alpha;
// Otherwise convert it to RGB, which is the intermediary format
// for converting between formats.
var rgb = void 0;
switch (color.mode) {
case 'rgb':
rgb = color;break;
case 'hsl':
rgb = from_hsl(color);break;
case 'hsv':
rgb = from_hsv(color);break;
case 'hsi':
rgb = from_hsi(color);break;
default:
return undefined;
var res = {
mode: 'lab',
l: l,
a: c * Math.cos(h / 180 * Math.PI),
b: c * Math.sin(h / 180 * Math.PI)
};
if (alpha !== undefined) res.alpha = alpha;
return res;
});
// D50 white
var Xn = 0.9642;
var Yn = 1.0000;
var Zn = 0.8249;
var k = Math.pow(29, 3) / Math.pow(3, 3);
var e = Math.pow(6, 3) / Math.pow(29, 3);
var lab_to_xyz = (function (_ref) {
var l = _ref.l,
a = _ref.a,
b = _ref.b;
var fx = a / 500 + (l + 16) / 116;
var fy = (l + 16) / 116 - b / 200;
return {
x: (Math.pow(fx, 3) > e ? Math.pow(fx, 3) : (116 * fx - 16) / k) * Xn,
y: (l > 8 ? Math.pow((l + 16) / 116, 3) : l / k) * Yn,
z: (Math.pow(fy, 3) > e ? Math.pow(fy, 3) : (116 * fy - 16) / k) * Zn
};
});
// D50 -> D65
var xyz_d50_to_d65 = (function (_ref) {
var x = _ref.x,
y = _ref.y,
z = _ref.z;
return {
x: x * 0.9555766 - y * 0.0230393 + z * 0.0631636,
y: x * -0.0282895 + y * 1.0099416 + z * 0.0210077,
z: x * 0.0122982 - y * 0.0204830 + z * 1.3299098
};
});
/*
CIE XYZ values to sRGB.
References:
* https://drafts.csswg.org/css-color/#color-conversion-code
* http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/
var lrgb_to_rgb = function lrgb_to_rgb(c) {
return c > 0.0031308 ? 1.055 * Math.pow(c, 1 / 2.4) - 0.055 : 12.92 * c;
};
var xyz_to_rgb = (function (_ref) {
var x = _ref.x,
y = _ref.y,
z = _ref.z;
return {
r: lrgb_to_rgb(x * 3.2404542 - y * 1.5371385 - 0.4985314 * z),
g: lrgb_to_rgb(x * -0.9692660 + y * 1.8760108 + 0.0415560 * z),
b: lrgb_to_rgb(x * 0.0556434 - y * 0.2040259 + 1.0572252 * z)
};
});
var lab_to_rgb = (function (lab) {
var res = xyz_to_rgb(xyz_d50_to_d65(lab_to_xyz(lab)));
res.mode = 'rgb';
if (lab.alpha !== undefined) res.alpha = lab.alpha;
return res;
});
/*
Convert sRGB values to CIE XYZ
using sRGB’s own white, D65 (no chromatic adaptation)
References:
* https://drafts.csswg.org/css-color/#color-conversion-code
* http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/
var rgb_to_lrgb = function rgb_to_lrgb(c) {
return c < 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
};
var rgb_to_xyz = (function (_ref) {
var r = _ref.r,
g = _ref.g,
b = _ref.b;
return {
x: rgb_to_lrgb(0.4124564 * r + 0.3575761 * g + 0.1804375 * b),
y: rgb_to_lrgb(0.2126729 * r + 0.7151522 * g + 0.0721750 * b),
z: rgb_to_lrgb(0.0193339 * r + 0.1191920 * g + 0.9503041 * b)
};
});
// D65 -> D50
var xyz_d65_to_d50 = (function (_ref) {
var x = _ref.x,
y = _ref.y,
z = _ref.z;
return {
x: x * 1.0478112 + y * 0.0228866 - z * 0.0501270,
y: x * 0.0295424 + y * 0.9904844 - z * 0.0170491,
z: x * -0.0092345 + y * 0.0150436 + z * 0.7521316
};
});
// D50 white
var Xn$1 = 0.9642;
var Yn$1 = 1.0000;
var Zn$1 = 0.8249;
var k$1 = Math.pow(29, 3) / Math.pow(3, 3);
var e$1 = Math.pow(6, 3) / Math.pow(29, 3);
var f = function f(value) {
return value > e$1 ? Math.cbrt(value) : (k$1 * value + 16) / 116;
};
var xyz_to_lab = (function (_ref) {
var x = _ref.x,
y = _ref.y,
z = _ref.z;
var f0 = f(x / Xn$1);
var f1 = f(y / Yn$1);
var f2 = f(z / Zn$1);
return {
l: 116 * f1 - 16,
a: 500 * (f0 - f1),
b: 200 * (f1 - f2)
};
});
var rgb_to_lab = (function (rgb) {
var res = xyz_to_lab(xyz_d65_to_d50(rgb_to_xyz(rgb)));
res.mode = 'lab';
if (rgb.alpha !== undefined) res.alpha = rgb.alpha;
return res;
});
var converters = {
hsi: { rgb: hsi_to_rgb },
hsl: { rgb: hsl_to_rgb },
hsv: { rgb: hsv_to_rgb },
hwb: { rgb: hwb_to_rgb },
lab: {
lch: lab_to_lch,
rgb: lab_to_rgb
},
lch: {
lab: lch_to_lab,
rgb: function rgb(c) {
return lch_to_lab(lab_to_rgb(c));
}
},
rgb: {
hsi: rgb_to_hsi,
hsl: rgb_to_hsl,
hsv: rgb_to_hsv,
hwb: rgb_to_hwb,
lab: rgb_to_lab,
lch: function lch(c) {
return rgb_to_lab(lab_to_lch(c));
}
}
};
switch (mode) {
case 'rgb':
return rgb;
case 'hsl':
return to_hsl(rgb);
case 'hsv':
return to_hsv(rgb);
case 'hsi':
return to_hsi(rgb);
default:
return undefined;
}};
var convert = (function (color, target_mode) {
return color !== undefined ?
// if the color's mode corresponds to our target mode
color.mode === target_mode ?
// then just return the color
color
// otherwise check to see if we have a dedicated
// converter for the target mode
: converters[color.mode][target_mode] ?
// and return its result...
converters[color.mode][target_mode](color)
// ...otherwise pass through RGB as an intermediary step.
// if the target mode is RGB...
: mode === 'rgb' ?
// just return the RGB
converters[color.mode].rgb(color)
// otherwise convert color.mode -> RGB -> target_mode
: converters.rgb[target_mode](converters[color.mode].rgb(color)) : undefined;
});

@@ -258,3 +497,3 @@ var parseNumber = (function (color, len) {

b: (color >> 4 & 0xF | color & 0xF0) / 255,
a: (color & 0xF | color << 4 & 0xF0) / 255
alpha: (color & 0xF | color << 4 & 0xF0) / 255
};

@@ -280,3 +519,3 @@ }

b: (color >> 8 & 0xFF) / 255,
a: (color & 0xFF) / 255
alpha: (color & 0xFF) / 255
};

@@ -286,3 +525,3 @@ }

var named = {
var named_colors = {
aliceblue: 0xf0f8ff,

@@ -442,38 +681,61 @@ antiquewhite: 0xfaebd7,

var parseNamed = (function (color) {
return typeof color === 'string' && parseNumber(named[color.toLowerCase()], 6) || undefined;
return typeof color === 'string' && parseNumber(named_colors[color.toLowerCase()], 6) || undefined;
});
// converts a regexp to string
// (does not cover all cases, but for our purposes it works)
var s = function s(r) {
return r.toString().replace(/^\/|\/$/g, '');
};
/*
Basic building blocks for color regexes
---------------------------------------
// matches a number
var n = /([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)/;
These regexes are expressed as strings
to be interpolated in the color regexes.
*/
// matches a percentage
var p = /([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)%/;
var sp = s(p);
var num = (/([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)/ + '').replace(/^\/|\/$/g, ''); // number
var per = num + '%'; // percentage
var alpha = '(?:' + num + '%|' + num + ')'; // alpha-value
var hue = '(?:' + num + '(deg|grad|rad|turn)|' + num + ')'; // hue
var c = '\\s*,\\s*'; // comma
var s = '\\s+'; // space
// matches a percentage or number
var pn = '(?:' + s(p) + '|' + s(n) + ')';
// hue can be a number or an angle (number + angle unit)
var hue = '(?:' + s(n) + '(deg|grad|rad|turn)|' + s(n) + ')';
/*
rgb() regular expressions.
Reference: https://drafts.csswg.org/css-color/#rgb-functions
*/
var rgb_num_old = new RegExp('^rgba?\\(\\s*' + num + c + num + c + num + '\\s*(?:,\\s*' + alpha + '\\s*)?\\)$');
var rgb_per_old = new RegExp('^rgba?\\(\\s*' + per + c + per + c + per + '\\s*(?:,\\s*' + alpha + '\\s*)?\\)$');
var rgb_num_new = new RegExp('^rgba?\\(\\s*' + num + s + num + s + num + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
var rgb_per_new = new RegExp('^rgba?\\(\\s*' + per + s + per + s + per + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
// Reference:
// https://drafts.csswg.org/css-color/#rgb-functions
var rgb_legacy = new RegExp('^rgba?\\(\\s*' + pn + '\\s*,\\s*' + pn + '\\s*,\\s*' + pn + '\\s*(?:,\\s*' + pn + '\\s*)?\\)$');
var rgb_current = new RegExp('^rgba?\\(\\s*' + pn + '\\s+' + pn + '\\s+' + pn + '\\s*(?:\\/\\s*' + pn + '\\s*)?\\)$');
/*
hsl() regular expressions.
Reference: https://drafts.csswg.org/css-color/#the-hsl-notation
*/
var hsl_old = new RegExp('^hsla?\\(\\s*' + hue + c + per + c + per + '\\s*(?:,\\s*' + alpha + '\\s*)?\\)$');
var hsl_new = new RegExp('^hsla?\\(\\s*' + hue + s + per + s + per + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
// Reference:
// https://drafts.csswg.org/css-color/#the-hsl-notation
var hsl_legacy = new RegExp('^hsla?\\(\\s*' + hue + '\\s*,\\s*' + sp + '\\s*,\\s*' + sp + '\\s*(?:,\\s*' + pn + '\\s*)?\\)$');
var hsl_current = new RegExp('^hsla?\\(\\s*' + hue + '\\s+' + sp + '\\s+' + sp + '\\s*(?:\\/\\s*' + pn + '\\s*)?\\)$');
/*
hexadecimal regular expressions.
*/
var hex = /^#?([0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{4}|[0-9a-f]{3})$/i;
var hwb = new RegExp('^hwb\\(\\s*' + hue + '\\s+' + sp + '\\s+' + sp + '\\s*(?:\\/\\s*' + pn + '\\s*)?\\)$');
/*
hwb() regular expressions.
Reference: https://drafts.csswg.org/css-color/#the-hwb-notation
*/
var hwb = new RegExp('^hwb\\(\\s*' + hue + s + per + s + per + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
/*
lab() and lch() regular expressions.
Reference: https://drafts.csswg.org/css-color/#lab-colors
*/
var lab = new RegExp('^lab\\(\\s*' + num + s + num + s + num + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
var lch = new RegExp('^lch\\(\\s*' + num + s + num + s + hue + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
/*
gray() regular expressions.
Reference: https://drafts.csswg.org/css-color/#grays
*/
var gray = new RegExp('^gray\\(\\s*' + num + '()()\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
var parseHex = (function (color) {

@@ -485,17 +747,30 @@ var match;

var parseRgb = (function (color) {
var match = color.match(rgb_legacy) || color.match(rgb_current);
if (!match) return;
var res = {
mode: 'rgb',
r: match[1] === undefined ? match[2] / 255 : match[1] / 100,
g: match[3] === undefined ? match[4] / 255 : match[3] / 100,
b: match[5] === undefined ? match[6] / 255 : match[5] / 100
};
if (match[7] !== undefined) {
res.a = match[7] / 100;
} else if (match[8] !== undefined) {
res.a = +match[8];
var match = void 0,
res = void 0;
if (match = color.match(rgb_num_old) || color.match(rgb_num_new)) {
res = {
mode: 'rgb',
r: match[1] / 255,
g: match[2] / 255,
b: match[3] / 255
};
} else if (match = color.match(rgb_per_old) || color.match(rgb_per_new)) {
res = {
mode: 'rgb',
r: match[1] / 100,
g: match[2] / 100,
b: match[3] / 100
};
} else {
return undefined;
}
if (match[4] !== undefined) {
res.alpha = match[4] / 100;
} else if (match[5] !== undefined) {
res.alpha = +match[5];
}
return res;

@@ -519,3 +794,3 @@ });

if (typeof color !== 'string') return;
var match = color.match(hsl_legacy) || color.match(hsl_current);
var match = color.match(hsl_old) || color.match(hsl_new);
if (!match) return;

@@ -529,8 +804,8 @@ var res = {

if (match[6] !== undefined) {
res.a = match[6] / 100;
res.alpha = match[6] / 100;
} else if (match[7] !== undefined) {
res.a = match[7] / 255;
res.alpha = match[7] / 255;
}
// TODO better way to normalize than via rgb?
return to_hsl(from_hsl(res));
return rgb_to_hsl(hsl_to_rgb(res));
});

@@ -557,5 +832,5 @@

if (match[6] !== undefined) {
res.a = match[6] / 100;
res.alpha = match[6] / 100;
} else if (match[7] !== undefined) {
res.a = match[7] / 255;
res.alpha = match[7] / 255;
}

@@ -565,15 +840,61 @@ return res;

var parseLab = (function (color) {
var match = void 0,
res = void 0;
if (match = color.match(lab)) {
res = {
mode: 'lab',
l: +match[1],
a: +match[2],
b: +match[3]
};
} else if (match = color.match(gray)) {
res = {
mode: 'lab',
l: +match[1],
a: 0,
b: 0
};
} else {
return undefined;
}
if (match[4] !== undefined) {
res.alpha = match[4] / 100;
} else if (match[5] !== undefined) {
res.alpha = +match[5];
}
return res;
});
var parseLch = (function (color) {
var match = color.match(lch);
if (!match) {
return undefined;
}
var res = {
mode: 'lch',
l: +match[1],
c: Math.max(0, +match[2]),
h: match[5] === undefined ? hue$1(match[3], match[4]) : +match[5]
};
if (match[6] !== undefined) {
res.alpha = match[6] / 100;
} else if (match[7] !== undefined) {
res.alpha = +match[7];
}
return res;
});
var parse = function parse(color) {
return parseHex(color) || parseRgb(color) || parseHsl(color) || parseNamed(color) || (color === 'transparent' ? parseNumber(0x00000000, 8) : undefined) || parseHwb(color);
return parseHex(color) || parseRgb(color) || parseHsl(color) || parseNamed(color) || (color === 'transparent' ? parseNumber(0x00000000, 8) : undefined) || parseHwb(color) || parseLab(color) || parseLch(color);
};
Object.assign(parse, {
number: parseNumber,
named: parseNamed,
hex: parseHex,
rgb: parseRgb,
hsl: parseHsl,
hwb: parseHwb
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {

@@ -623,2 +944,10 @@ return typeof obj;

var lab$1 = (function (color) {
return convert(prepare(color, 'lab'), 'lab');
});
var lch$1 = (function (color) {
return convert(prepare(color, 'lch'), 'lch');
});
// From: https://github.com/d3/d3-format/issues/32

@@ -668,3 +997,3 @@

if (format === 'rgb') {
if (color['a'] === undefined || color['a'] === 1) {
if (color.alpha === undefined || color.alpha === 1) {
// opaque color

@@ -674,3 +1003,3 @@ return 'rgb(' + r + ', ' + g + ', ' + b + ')';

// transparent color
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + color['a'] + ')';
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + color.alpha + ')';
}

@@ -680,6 +1009,21 @@ }

var colors = {
named: named
};
var components = {};
components.rgb = ['r', 'g', 'b', 'alpha'];
components.hsl = ['h', 's', 'l', 'alpha'];
components.hsv = ['h', 's', 'v', 'alpha'];
components.hsi = ['h', 's', 'i', 'alpha'];
components.lab = ['l', 'a', 'b', 'alpha'];
components.lch = ['l', 'c', 'h', 'alpha'];
components.hwb = ['h', 'w', 'b', 'alpha'];
var map = (function (color, fn) {
return components[color.mode].reduce(function (result, k) {
if (color[k] !== undefined) {
result[k] = fn(k, color[k]);
}
return result;
}, {});
});
var culori = function culori(color) {

@@ -689,3 +1033,3 @@ return rgb(color);

Object.assign(culori, colors, {
var methods = /*#__PURE__*/Object.freeze({
hsl: hsl,

@@ -696,11 +1040,24 @@ hsv: hsv,

rgb: rgb,
lab: lab$1,
lch: lch$1,
css: css,
convert: convert,
prepare: prepare,
round: round,
parse: parse,
round: round
parseNumber: parseNumber,
parseNamed: parseNamed,
parseHex: parseHex,
parseRgb: parseRgb,
parseHsl: parseHsl,
parseHwb: parseHwb,
named: named_colors,
map: map,
default: culori
});
Object.assign(culori, methods);
return culori;
})));

@@ -1,1 +0,1 @@

!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):e.culori=r()}(this,function(){"use strict";function e(e){var r=e.h,a=e.s,n=e.l,t=e.a,o=n+a*(n<.5?n:1-n),i=o-2*(o-n)*Math.abs(r/60%2-1),s=void 0;switch(Math.floor(r/60)){case 0:s={r:o,g:i,b:2*n-o};break;case 1:s={r:i,g:o,b:2*n-o};break;case 2:s={r:2*n-o,g:o,b:i};break;case 3:s={r:2*n-o,g:i,b:o};break;case 4:s={r:i,g:2*n-o,b:o};break;case 5:s={r:o,g:2*n-o,b:i}}return s.mode="rgb",void 0!==t&&(s.a=t),s}function r(e){var r=e.r,a=e.g,n=e.b,t=e.a,o=Math.max(r,a,n),i=Math.min(r,a,n),s={mode:"hsl",s:o===i?0:(o-i)/(1-Math.abs(o+i-1)),l:.5*(o+i)};return o-i!=0&&(s.h=60*(o===r?(a-n)/(o-i)+6*(a<n):o===a?(n-r)/(o-i)+2:(r-a)/(o-i)+4)),void 0!==t&&(s.a=t),s}var a=function(a,n){if(void 0!==a){if(a.mode===n)return a;var t,o,i,s,d,u,l,g,b=void 0;switch(a.mode){case"rgb":b=a;break;case"hsl":b=e(a);break;case"hsv":b=function(e){var r=e.h,a=e.s,n=e.v,t=e.a,o=Math.abs(r/60%2-1),i=void 0;switch(Math.floor(r/60)){case 0:i={r:n,g:n*(1-a*o),b:n*(1-a)};break;case 1:i={r:n*(1-a*o),g:n,b:n*(1-a)};break;case 2:i={r:n*(1-a),g:n,b:n*(1-a*o)};break;case 3:i={r:n*(1-a),g:n*(1-a*o),b:n};break;case 4:i={r:n*(1-a*o),g:n*(1-a),b:n};break;case 5:i={r:n,g:n*(1-a),b:n*(1-a*o)}}return i.mode="rgb",void 0!==t&&(i.a=t),i}(a);break;case"hsi":b=function(e){var r=e.h,a=e.s,n=e.i,t=e.a,o=Math.abs(r/60%2-1),i=void 0;switch(Math.floor(r/60)){case 0:i={r:n*(1+a*(3/(2-o)-1)),g:n*(1+a*(3*(1-o)/(2-o)-1)),b:n*(1-a)};break;case 1:i={r:n*(1+a*(3*(1-o)/(2-o)-1)),g:n*(1+a*(3/(2-o)-1)),b:n*(1-a)};break;case 2:i={r:n*(1-a),g:n*(1+a*(3/(2-o)-1)),b:n*(1+a*(3*(1-o)/(2-o)-1))};break;case 3:i={r:n*(1-a),g:n*(1+a*(3*(1-o)/(2-o)-1)),b:n*(1+a*(3/(2-o)-1))};break;case 4:i={r:n*(1+a*(3*(1-o)/(2-o)-1)),g:n*(1-a),b:n*(1+a*(3/(2-o)-1))};break;case 5:i={r:n*(1+a*(3/(2-o)-1)),g:n*(1-a),b:n*(1+a*(3*(1-o)/(2-o)-1))}}return i.mode="rgb",void 0!==t&&(i.a=t),i}(a);break;default:return}switch(n){case"rgb":return b;case"hsl":return r(b);case"hsv":return o=(t=b).r,i=t.g,s=t.b,d=t.a,u=Math.max(o,i,s),l=Math.min(o,i,s),g={mode:"hsv",s:0===u?0:1-l/u,v:u},u-l!=0&&(g.h=60*(u===o?(i-s)/(u-l)+6*(i<s):u===i?(s-o)/(u-l)+2:(o-i)/(u-l)+4)),void 0!==d&&(g.a=d),g;case"hsi":return function(e){var r=e.r,a=e.g,n=e.b,t=e.a,o=Math.max(r,a,n),i=Math.min(r,a,n),s={mode:"hsi",s:r+a+n===0?0:1-3*i/(r+a+n),i:(r+a+n)/3};return o-i!=0&&(s.h=60*(o===r?(a-n)/(o-i)+6*(a<n):o===a?(n-r)/(o-i)+2:(r-a)/(o-i)+4)),void 0!==t&&(s.a=t),s}(b);default:return}}},n=function(e,r){if("number"==typeof e)return 3===r?{mode:"rgb",r:(e>>8&15|e>>4&240)/255,g:(e>>4&15|240&e)/255,b:(15&e|e<<4&240)/255}:4===r?{mode:"rgb",r:(e>>12&15|e>>8&240)/255,g:(e>>8&15|e>>4&240)/255,b:(e>>4&15|240&e)/255,a:(15&e|e<<4&240)/255}:6===r?{mode:"rgb",r:(e>>16&255)/255,g:(e>>8&255)/255,b:(255&e)/255}:8===r?{mode:"rgb",r:(e>>24&255)/255,g:(e>>16&255)/255,b:(e>>8&255)/255,a:(255&e)/255}:void 0},t={aliceblue:15792383,antiquewhite:16444375,aqua:65535,aquamarine:8388564,azure:15794175,beige:16119260,bisque:16770244,black:0,blanchedalmond:16772045,blue:255,blueviolet:9055202,brown:10824234,burlywood:14596231,cadetblue:6266528,chartreuse:8388352,chocolate:13789470,coral:16744272,cornflowerblue:6591981,cornsilk:16775388,crimson:14423100,cyan:65535,darkblue:139,darkcyan:35723,darkgoldenrod:12092939,darkgray:11119017,darkgreen:25600,darkgrey:11119017,darkkhaki:12433259,darkmagenta:9109643,darkolivegreen:5597999,darkorange:16747520,darkorchid:10040012,darkred:9109504,darksalmon:15308410,darkseagreen:9419919,darkslateblue:4734347,darkslategray:3100495,darkslategrey:3100495,darkturquoise:52945,darkviolet:9699539,deeppink:16716947,deepskyblue:49151,dimgray:6908265,dimgrey:6908265,dodgerblue:2003199,firebrick:11674146,floralwhite:16775920,forestgreen:2263842,fuchsia:16711935,gainsboro:14474460,ghostwhite:16316671,gold:16766720,goldenrod:14329120,gray:8421504,green:32768,greenyellow:11403055,grey:8421504,honeydew:15794160,hotpink:16738740,indianred:13458524,indigo:4915330,ivory:16777200,khaki:15787660,lavender:15132410,lavenderblush:16773365,lawngreen:8190976,lemonchiffon:16775885,lightblue:11393254,lightcoral:15761536,lightcyan:14745599,lightgoldenrodyellow:16448210,lightgray:13882323,lightgreen:9498256,lightgrey:13882323,lightpink:16758465,lightsalmon:16752762,lightseagreen:2142890,lightskyblue:8900346,lightslategray:7833753,lightslategrey:7833753,lightsteelblue:11584734,lightyellow:16777184,lime:65280,limegreen:3329330,linen:16445670,magenta:16711935,maroon:8388608,mediumaquamarine:6737322,mediumblue:205,mediumorchid:12211667,mediumpurple:9662683,mediumseagreen:3978097,mediumslateblue:8087790,mediumspringgreen:64154,mediumturquoise:4772300,mediumvioletred:13047173,midnightblue:1644912,mintcream:16121850,mistyrose:16770273,moccasin:16770229,navajowhite:16768685,navy:128,oldlace:16643558,olive:8421376,olivedrab:7048739,orange:16753920,orangered:16729344,orchid:14315734,palegoldenrod:15657130,palegreen:10025880,paleturquoise:11529966,palevioletred:14381203,papayawhip:16773077,peachpuff:16767673,peru:13468991,pink:16761035,plum:14524637,powderblue:11591910,purple:8388736,rebeccapurple:6697881,red:16711680,rosybrown:12357519,royalblue:4286945,saddlebrown:9127187,salmon:16416882,sandybrown:16032864,seagreen:3050327,seashell:16774638,sienna:10506797,silver:12632256,skyblue:8900331,slateblue:6970061,slategray:7372944,slategrey:7372944,snow:16775930,springgreen:65407,steelblue:4620980,tan:13808780,teal:32896,thistle:14204888,tomato:16737095,turquoise:4251856,violet:15631086,wheat:16113331,white:16777215,whitesmoke:16119285,yellow:16776960,yellowgreen:10145074},o=function(e){return"string"==typeof e&&n(t[e.toLowerCase()],6)||void 0},i=function(e){return e.toString().replace(/^\/|\/$/g,"")},s=/([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)/,d=/([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)%/,u=i(d),l="(?:"+i(d)+"|"+i(s)+")",g="(?:"+i(s)+"(deg|grad|rad|turn)|"+i(s)+")",b=new RegExp("^rgba?\\(\\s*"+l+"\\s*,\\s*"+l+"\\s*,\\s*"+l+"\\s*(?:,\\s*"+l+"\\s*)?\\)$"),c=new RegExp("^rgba?\\(\\s*"+l+"\\s+"+l+"\\s+"+l+"\\s*(?:\\/\\s*"+l+"\\s*)?\\)$"),h=new RegExp("^hsla?\\(\\s*"+g+"\\s*,\\s*"+u+"\\s*,\\s*"+u+"\\s*(?:,\\s*"+l+"\\s*)?\\)$"),m=new RegExp("^hsla?\\(\\s*"+g+"\\s+"+u+"\\s+"+u+"\\s*(?:\\/\\s*"+l+"\\s*)?\\)$"),f=/^#?([0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{4}|[0-9a-f]{3})$/i,v=new RegExp("^hwb\\(\\s*"+g+"\\s+"+u+"\\s+"+u+"\\s*(?:\\/\\s*"+l+"\\s*)?\\)$"),p=function(e){var r;return(r=e.match(f))?n(parseInt(r[1],16),r[1].length):void 0},y=function(e){var r=e.match(b)||e.match(c);if(r){var a={mode:"rgb",r:void 0===r[1]?r[2]/255:r[1]/100,g:void 0===r[3]?r[4]/255:r[3]/100,b:void 0===r[5]?r[6]/255:r[5]/100};return void 0!==r[7]?a.a=r[7]/100:void 0!==r[8]&&(a.a=+r[8]),a}},k=function(e,r){switch(r){case"deg":return+e;case"rad":return e/Math.PI*180;case"grad":return e/10*9;case"turn":return 360*e}},w=function(a){if("string"==typeof a){var n=a.match(h)||a.match(m);if(n){var t={mode:"hsl",h:void 0===n[3]?k(n[1],n[2]):+n[3],s:n[4]/100,l:n[5]/100};return void 0!==n[6]?t.a=n[6]/100:void 0!==n[7]&&(t.a=n[7]/255),r(e(t))}}},M=function(e){if("string"==typeof e){var r=e.match(v);if(r){var a={mode:"hwb",h:void 0===r[3]?k(r[1],r[2]):+r[3],w:r[4]/100,b:r[5]/100};if(a.w+a.b>1){var n=a.w+a.b;a.w/=n,a.b/=n}return void 0!==r[6]?a.a=r[6]/100:void 0!==r[7]&&(a.a=r[7]/255),a}}},x=function(e){return p(e)||y(e)||w(e)||o(e)||("transparent"===e?n(0,8):void 0)||M(e)};Object.assign(x,{number:n,named:o,hex:p,rgb:y,hsl:w,hwb:M});var j="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},q=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var a=arguments[r];for(var n in a)Object.prototype.hasOwnProperty.call(a,n)&&(e[n]=a[n])}return e},E=function(e,r){return"object"!==(void 0===e?"undefined":j(e))?x(e):void 0===e.mode?q({},e,{mode:r}):e},S=function(e){return a(E(e,"rgb"),"rgb")},$=function(e,r){return Math.round(e*(r=Math.pow(10,r)))/r};var O={named:t},R=function(e){return S(e)};return Object.assign(R,O,{hsl:function(e){return a(E(e,"hsl"),"hsl")},hsv:function(e){return a(E(e,"hsv"),"hsv")},hsi:function(e){return a(E(e,"hsi"),"hsi")},hwb:function(e){return a(E(e,"hwb"),"hwb")},rgb:S,css:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"rgb",a=S(e),n=Math.round(255*a.r),t=Math.round(255*a.g),o=Math.round(255*a.b);return"hex"===r?"#"+(1<<24|n<<16|t<<8|o).toString(16).slice(1):"rgb"===r?void 0===a.a||1===a.a?"rgb("+n+", "+t+", "+o+")":"rgba("+n+", "+t+", "+o+", "+a.a+")":void 0},convert:a,prepare:E,parse:x,round:function e(r){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:4;if(void 0!==r)return"number"==typeof r?$(r,a):"string"==typeof r?r:"object"===(void 0===r?"undefined":j(r))?Object.keys(r).reduce(function(a,n){return a[n]=e(r[n]),a},{}):void 0}}),R});
!function(e,a){"object"==typeof exports&&"undefined"!=typeof module?module.exports=a():"function"==typeof define&&define.amd?define(a):e.culori=a()}(this,function(){"use strict";function e(e){var a=e.h,r=e.s,n=e.l,t=e.alpha,o=n+r*(n<.5?n:1-n),i=o-2*(o-n)*Math.abs(a/60%2-1),l=void 0;switch(Math.floor(a/60)){case 0:l={r:o,g:i,b:2*n-o};break;case 1:l={r:i,g:o,b:2*n-o};break;case 2:l={r:2*n-o,g:o,b:i};break;case 3:l={r:2*n-o,g:i,b:o};break;case 4:l={r:i,g:2*n-o,b:o};break;case 5:l={r:o,g:2*n-o,b:i}}return l.mode="rgb",void 0!==t&&(l.alpha=t),l}function a(e){var a=e.h,r=e.s,n=e.v,t=e.alpha,o=Math.abs(a/60%2-1),i=void 0;switch(Math.floor(a/60)){case 0:i={r:n,g:n*(1-r*o),b:n*(1-r)};break;case 1:i={r:n*(1-r*o),g:n,b:n*(1-r)};break;case 2:i={r:n*(1-r),g:n,b:n*(1-r*o)};break;case 3:i={r:n*(1-r),g:n*(1-r*o),b:n};break;case 4:i={r:n*(1-r*o),g:n*(1-r),b:n};break;case 5:i={r:n,g:n*(1-r),b:n*(1-r*o)}}return i.mode="rgb",void 0!==t&&(i.alpha=t),i}function r(e){var a=e.r,r=e.g,n=e.b,t=e.alpha,o=Math.max(a,r,n),i=Math.min(a,r,n),l={mode:"hsl",s:o===i?0:(o-i)/(1-Math.abs(o+i-1)),l:.5*(o+i)};return o-i!=0&&(l.h=60*(o===a?(r-n)/(o-i)+6*(r<n):o===r?(n-a)/(o-i)+2:(a-r)/(o-i)+4)),void 0!==t&&(l.alpha=t),l}function n(e){var a=e.r,r=e.g,n=e.b,t=e.alpha,o=Math.max(a,r,n),i=Math.min(a,r,n),l={mode:"hsv",s:0===o?0:1-i/o,v:o};return o-i!=0&&(l.h=60*(o===a?(r-n)/(o-i)+6*(r<n):o===r?(n-a)/(o-i)+2:(a-r)/(o-i)+4)),void 0!==t&&(l.alpha=t),l}var t=function(e){var a=e.l,r=e.a,n=e.b,t=e.alpha,o={mode:"lch",l:a,c:Math.sqrt(r*r+n*n),h:180*Math.atan2(n,r)/Math.PI};return void 0!==t&&(o.alpha=t),o},o=function(e){var a=e.l,r=e.c,n=e.h,t=e.alpha,o={mode:"lab",l:a,a:r*Math.cos(n/180*Math.PI),b:r*Math.sin(n/180*Math.PI)};return void 0!==t&&(o.alpha=t),o},i=Math.pow(29,3)/Math.pow(3,3),l=Math.pow(6,3)/Math.pow(29,3),h=function(e){return e>.0031308?1.055*Math.pow(e,1/2.4)-.055:12.92*e},s=function(e){var a,r,n,t,o=function(e){var a=e.x,r=e.y,n=e.z;return{r:h(3.2404542*a-1.5371385*r-.4985314*n),g:h(-.969266*a+1.8760108*r+.041556*n),b:h(.0556434*a-.2040259*r+1.0572252*n)}}(function(e){var a=e.x,r=e.y,n=e.z;return{x:.9555766*a-.0230393*r+.0631636*n,y:-.0282895*a+1.0099416*r+.0210077*n,z:.0122982*a-.020483*r+1.3299098*n}}((r=(a=e).l,n=a.a/500+(r+16)/116,t=(r+16)/116-a.b/200,{x:.9642*(Math.pow(n,3)>l?Math.pow(n,3):(116*n-16)/i),y:1*(r>8?Math.pow((r+16)/116,3):r/i),z:.8249*(Math.pow(t,3)>l?Math.pow(t,3):(116*t-16)/i)})));return o.mode="rgb",void 0!==e.alpha&&(o.alpha=e.alpha),o},u=function(e){return e<.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},d=Math.pow(29,3)/Math.pow(3,3),b=Math.pow(6,3)/Math.pow(29,3),c=function(e){return e>b?Math.cbrt(e):(d*e+16)/116},g=function(e){var a,r,n,t,o=function(e){var a=e.x,r=e.y,n=e.z,t=c(a/.9642),o=c(r/1);return{l:116*o-16,a:500*(t-o),b:200*(o-c(n/.8249))}}(function(e){var a=e.x,r=e.y,n=e.z;return{x:1.0478112*a+.0228866*r-.050127*n,y:.0295424*a+.9904844*r-.0170491*n,z:-.0092345*a+.0150436*r+.7521316*n}}((r=(a=e).r,n=a.g,t=a.b,{x:u(.4124564*r+.3575761*n+.1804375*t),y:u(.2126729*r+.7151522*n+.072175*t),z:u(.0193339*r+.119192*n+.9503041*t)})));return o.mode="lab",void 0!==e.alpha&&(o.alpha=e.alpha),o},p={hsi:{rgb:function(e){var a=e.h,r=e.s,n=e.i,t=e.alpha,o=Math.abs(a/60%2-1),i=void 0;switch(Math.floor(a/60)){case 0:i={r:n*(1+r*(3/(2-o)-1)),g:n*(1+r*(3*(1-o)/(2-o)-1)),b:n*(1-r)};break;case 1:i={r:n*(1+r*(3*(1-o)/(2-o)-1)),g:n*(1+r*(3/(2-o)-1)),b:n*(1-r)};break;case 2:i={r:n*(1-r),g:n*(1+r*(3/(2-o)-1)),b:n*(1+r*(3*(1-o)/(2-o)-1))};break;case 3:i={r:n*(1-r),g:n*(1+r*(3*(1-o)/(2-o)-1)),b:n*(1+r*(3/(2-o)-1))};break;case 4:i={r:n*(1+r*(3*(1-o)/(2-o)-1)),g:n*(1-r),b:n*(1+r*(3/(2-o)-1))};break;case 5:i={r:n*(1+r*(3/(2-o)-1)),g:n*(1-r),b:n*(1+r*(3*(1-o)/(2-o)-1))}}return i.mode="rgb",void 0!==t&&(i.alpha=t),i}},hsl:{rgb:e},hsv:{rgb:a},hwb:{rgb:function(e){var r=e.h,n=e.w,t=e.b,o=e.alpha;if(n+t>1){var i=n+t;n/=i,t/=i}return a({h:r,s:1===t?1:1-n/(1-t),v:1-t,alpha:o})}},lab:{lch:t,rgb:s},lch:{lab:o,rgb:function(e){return o(s(e))}},rgb:{hsi:function(e){var a=e.r,r=e.g,n=e.b,t=e.alpha,o=Math.max(a,r,n),i=Math.min(a,r,n),l={mode:"hsi",s:a+r+n===0?0:1-3*i/(a+r+n),i:(a+r+n)/3};return o-i!=0&&(l.h=60*(o===a?(r-n)/(o-i)+6*(r<n):o===r?(n-a)/(o-i)+2:(a-r)/(o-i)+4)),void 0!==t&&(l.alpha=t),l},hsl:r,hsv:n,hwb:function(e){var a=n(e);if(void 0!==a){var r={mode:"hwb",w:(1-a.s)*a.v,b:1-a.v};return void 0!==a.h&&(r.h=a.h),void 0!==a.alpha&&(r.alpha=a.alpha),r}},lab:g,lch:function(e){return g(t(e))}}},v=function(e,a){return void 0!==e?e.mode===a?e:p[e.mode][a]?p[e.mode][a](e):"rgb"===mode?p[e.mode].rgb(e):p.rgb[a](p[e.mode].rgb(e)):void 0},f=function(e,a){if("number"==typeof e)return 3===a?{mode:"rgb",r:(e>>8&15|e>>4&240)/255,g:(e>>4&15|240&e)/255,b:(15&e|e<<4&240)/255}:4===a?{mode:"rgb",r:(e>>12&15|e>>8&240)/255,g:(e>>8&15|e>>4&240)/255,b:(e>>4&15|240&e)/255,alpha:(15&e|e<<4&240)/255}:6===a?{mode:"rgb",r:(e>>16&255)/255,g:(e>>8&255)/255,b:(255&e)/255}:8===a?{mode:"rgb",r:(e>>24&255)/255,g:(e>>16&255)/255,b:(e>>8&255)/255,alpha:(255&e)/255}:void 0},m={aliceblue:15792383,antiquewhite:16444375,aqua:65535,aquamarine:8388564,azure:15794175,beige:16119260,bisque:16770244,black:0,blanchedalmond:16772045,blue:255,blueviolet:9055202,brown:10824234,burlywood:14596231,cadetblue:6266528,chartreuse:8388352,chocolate:13789470,coral:16744272,cornflowerblue:6591981,cornsilk:16775388,crimson:14423100,cyan:65535,darkblue:139,darkcyan:35723,darkgoldenrod:12092939,darkgray:11119017,darkgreen:25600,darkgrey:11119017,darkkhaki:12433259,darkmagenta:9109643,darkolivegreen:5597999,darkorange:16747520,darkorchid:10040012,darkred:9109504,darksalmon:15308410,darkseagreen:9419919,darkslateblue:4734347,darkslategray:3100495,darkslategrey:3100495,darkturquoise:52945,darkviolet:9699539,deeppink:16716947,deepskyblue:49151,dimgray:6908265,dimgrey:6908265,dodgerblue:2003199,firebrick:11674146,floralwhite:16775920,forestgreen:2263842,fuchsia:16711935,gainsboro:14474460,ghostwhite:16316671,gold:16766720,goldenrod:14329120,gray:8421504,green:32768,greenyellow:11403055,grey:8421504,honeydew:15794160,hotpink:16738740,indianred:13458524,indigo:4915330,ivory:16777200,khaki:15787660,lavender:15132410,lavenderblush:16773365,lawngreen:8190976,lemonchiffon:16775885,lightblue:11393254,lightcoral:15761536,lightcyan:14745599,lightgoldenrodyellow:16448210,lightgray:13882323,lightgreen:9498256,lightgrey:13882323,lightpink:16758465,lightsalmon:16752762,lightseagreen:2142890,lightskyblue:8900346,lightslategray:7833753,lightslategrey:7833753,lightsteelblue:11584734,lightyellow:16777184,lime:65280,limegreen:3329330,linen:16445670,magenta:16711935,maroon:8388608,mediumaquamarine:6737322,mediumblue:205,mediumorchid:12211667,mediumpurple:9662683,mediumseagreen:3978097,mediumslateblue:8087790,mediumspringgreen:64154,mediumturquoise:4772300,mediumvioletred:13047173,midnightblue:1644912,mintcream:16121850,mistyrose:16770273,moccasin:16770229,navajowhite:16768685,navy:128,oldlace:16643558,olive:8421376,olivedrab:7048739,orange:16753920,orangered:16729344,orchid:14315734,palegoldenrod:15657130,palegreen:10025880,paleturquoise:11529966,palevioletred:14381203,papayawhip:16773077,peachpuff:16767673,peru:13468991,pink:16761035,plum:14524637,powderblue:11591910,purple:8388736,rebeccapurple:6697881,red:16711680,rosybrown:12357519,royalblue:4286945,saddlebrown:9127187,salmon:16416882,sandybrown:16032864,seagreen:3050327,seashell:16774638,sienna:10506797,silver:12632256,skyblue:8900331,slateblue:6970061,slategray:7372944,slategrey:7372944,snow:16775930,springgreen:65407,steelblue:4620980,tan:13808780,teal:32896,thistle:14204888,tomato:16737095,turquoise:4251856,violet:15631086,wheat:16113331,white:16777215,whitesmoke:16119285,yellow:16776960,yellowgreen:10145074},w=function(e){return"string"==typeof e&&f(m[e.toLowerCase()],6)||void 0},y=(/([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)/+"").replace(/^\/|\/$/g,""),k=y+"%",M="(?:"+y+"%|"+y+")",x="(?:"+y+"(deg|grad|rad|turn)|"+y+")",$="\\s*,\\s*",E="\\s+",R=new RegExp("^rgba?\\(\\s*"+y+$+y+$+y+"\\s*(?:,\\s*"+M+"\\s*)?\\)$"),q=new RegExp("^rgba?\\(\\s*"+k+$+k+$+k+"\\s*(?:,\\s*"+M+"\\s*)?\\)$"),z=new RegExp("^rgba?\\(\\s*"+y+E+y+E+y+"\\s*(?:\\/\\s*"+M+"\\s*)?\\)$"),j=new RegExp("^rgba?\\(\\s*"+k+E+k+E+k+"\\s*(?:\\/\\s*"+M+"\\s*)?\\)$"),O=new RegExp("^hsla?\\(\\s*"+x+$+k+$+k+"\\s*(?:,\\s*"+M+"\\s*)?\\)$"),S=new RegExp("^hsla?\\(\\s*"+x+E+k+E+k+"\\s*(?:\\/\\s*"+M+"\\s*)?\\)$"),I=/^#?([0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{4}|[0-9a-f]{3})$/i,P=new RegExp("^hwb\\(\\s*"+x+E+k+E+k+"\\s*(?:\\/\\s*"+M+"\\s*)?\\)$"),H=new RegExp("^lab\\(\\s*"+y+E+y+E+y+"\\s*(?:\\/\\s*"+M+"\\s*)?\\)$"),N=new RegExp("^lch\\(\\s*"+y+E+y+E+x+"\\s*(?:\\/\\s*"+M+"\\s*)?\\)$"),C=new RegExp("^gray\\(\\s*"+y+"()()\\s*(?:\\/\\s*"+M+"\\s*)?\\)$"),L=function(e){var a;return(a=e.match(I))?f(parseInt(a[1],16),a[1].length):void 0},A=function(e){var a=void 0,r=void 0;if(a=e.match(R)||e.match(z))r={mode:"rgb",r:a[1]/255,g:a[2]/255,b:a[3]/255};else{if(!(a=e.match(q)||e.match(j)))return;r={mode:"rgb",r:a[1]/100,g:a[2]/100,b:a[3]/100}}return void 0!==a[4]?r.alpha=a[4]/100:void 0!==a[5]&&(r.alpha=+a[5]),r},B=function(e,a){switch(a){case"deg":return+e;case"rad":return e/Math.PI*180;case"grad":return e/10*9;case"turn":return 360*e}},D=function(a){if("string"==typeof a){var n=a.match(O)||a.match(S);if(n){var t={mode:"hsl",h:void 0===n[3]?B(n[1],n[2]):+n[3],s:n[4]/100,l:n[5]/100};return void 0!==n[6]?t.alpha=n[6]/100:void 0!==n[7]&&(t.alpha=n[7]/255),r(e(t))}}},F=function(e){if("string"==typeof e){var a=e.match(P);if(a){var r={mode:"hwb",h:void 0===a[3]?B(a[1],a[2]):+a[3],w:a[4]/100,b:a[5]/100};if(r.w+r.b>1){var n=r.w+r.b;r.w/=n,r.b/=n}return void 0!==a[6]?r.alpha=a[6]/100:void 0!==a[7]&&(r.alpha=a[7]/255),r}}},G=function(e){return L(e)||A(e)||D(e)||w(e)||("transparent"===e?f(0,8):void 0)||F(e)||function(e){var a=void 0,r=void 0;if(a=e.match(H))r={mode:"lab",l:+a[1],a:+a[2],b:+a[3]};else{if(!(a=e.match(C)))return;r={mode:"lab",l:+a[1],a:0,b:0}}return void 0!==a[4]?r.alpha=a[4]/100:void 0!==a[5]&&(r.alpha=+a[5]),r}(e)||function(e){var a=e.match(N);if(a){var r={mode:"lch",l:+a[1],c:Math.max(0,+a[2]),h:void 0===a[5]?B(a[3],a[4]):+a[5]};return void 0!==a[6]?r.alpha=a[6]/100:void 0!==a[7]&&(r.alpha=+a[7]),r}}(e)},J="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},K=Object.assign||function(e){for(var a=1;a<arguments.length;a++){var r=arguments[a];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},Q=function(e,a){return"object"!==(void 0===e?"undefined":J(e))?G(e):void 0===e.mode?K({},e,{mode:a}):e},T=function(e){return v(Q(e,"rgb"),"rgb")},U=function(e,a){return Math.round(e*(a=Math.pow(10,a)))/a};var V={rgb:["r","g","b","alpha"],hsl:["h","s","l","alpha"],hsv:["h","s","v","alpha"],hsi:["h","s","i","alpha"],lab:["l","a","b","alpha"],lch:["l","c","h","alpha"],hwb:["h","w","b","alpha"]},W=function(e){return T(e)},X=Object.freeze({hsl:function(e){return v(Q(e,"hsl"),"hsl")},hsv:function(e){return v(Q(e,"hsv"),"hsv")},hsi:function(e){return v(Q(e,"hsi"),"hsi")},hwb:function(e){return v(Q(e,"hwb"),"hwb")},rgb:T,lab:function(e){return v(Q(e,"lab"),"lab")},lch:function(e){return v(Q(e,"lch"),"lch")},css:function(e){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"rgb",r=T(e),n=Math.round(255*r.r),t=Math.round(255*r.g),o=Math.round(255*r.b);return"hex"===a?"#"+(1<<24|n<<16|t<<8|o).toString(16).slice(1):"rgb"===a?void 0===r.alpha||1===r.alpha?"rgb("+n+", "+t+", "+o+")":"rgba("+n+", "+t+", "+o+", "+r.alpha+")":void 0},convert:v,prepare:Q,round:function e(a){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:4;if(void 0!==a)return"number"==typeof a?U(a,r):"string"==typeof a?a:"object"===(void 0===a?"undefined":J(a))?Object.keys(a).reduce(function(r,n){return r[n]=e(a[n]),r},{}):void 0},parse:G,parseNumber:f,parseNamed:w,parseHex:L,parseRgb:A,parseHsl:D,parseHwb:F,named:m,map:function(e,a){return V[e.mode].reduce(function(r,n){return void 0!==e[n]&&(r[n]=a(n,e[n])),r},{})},default:W});return Object.assign(W,X),W});
// Based on: https://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
function from_hsl (_ref) {
function hsi_to_rgb (_ref) {
var h = _ref.h,
s = _ref.s,
i = _ref.i,
alpha = _ref.alpha;
var f = Math.abs(h / 60 % 2 - 1);
var res = void 0;
switch (Math.floor(h / 60)) {
case 0:
res = {
r: i * (1 + s * (3 / (2 - f) - 1)),
g: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
b: i * (1 - s)
};
break;
case 1:
res = {
r: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
g: i * (1 + s * (3 / (2 - f) - 1)),
b: i * (1 - s)
};
break;
case 2:
res = {
r: i * (1 - s),
g: i * (1 + s * (3 / (2 - f) - 1)),
b: i * (1 + s * (3 * (1 - f) / (2 - f) - 1))
};
break;
case 3:
res = {
r: i * (1 - s),
g: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
b: i * (1 + s * (3 / (2 - f) - 1))
};
break;
case 4:
res = {
r: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
g: i * (1 - s),
b: i * (1 + s * (3 / (2 - f) - 1))
};
break;
case 5:
res = {
r: i * (1 + s * (3 / (2 - f) - 1)),
g: i * (1 - s),
b: i * (1 + s * (3 * (1 - f) / (2 - f) - 1))
};
break;
}
res.mode = 'rgb';
if (alpha !== undefined) res.alpha = alpha;
return res;
}
// Based on: https://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
function hsl_to_rgb (_ref) {
var h = _ref.h,
s = _ref.s,
l = _ref.l,
a = _ref.a;
alpha = _ref.alpha;

@@ -27,3 +87,3 @@ var m1 = l + s * (l < 0.5 ? l : 1 - l);

res.mode = 'rgb';
if (a !== undefined) res.a = a;
if (alpha !== undefined) res.alpha = alpha;
return res;

@@ -34,7 +94,7 @@ }

function from_hsv (_ref) {
function hsv_to_rgb (_ref) {
var h = _ref.h,
s = _ref.s,
v = _ref.v,
a = _ref.a;
alpha = _ref.alpha;

@@ -58,63 +118,53 @@ var f = Math.abs(h / 60 % 2 - 1);

res.mode = 'rgb';
if (a !== undefined) res.a = a;
if (alpha !== undefined) res.alpha = alpha;
return res;
}
// Based on: https://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
/*
HWB to RGB converter
--------------------
function from_hsi (_ref) {
References:
* https://drafts.csswg.org/css-color/#hwb-to-rgb
* https://en.wikipedia.org/wiki/HWB_color_model
* http://alvyray.com/Papers/CG/HWB_JGTv208.pdf
*/
function hwb_to_rgb (_ref) {
var h = _ref.h,
s = _ref.s,
i = _ref.i,
a = _ref.a;
w = _ref.w,
b = _ref.b,
alpha = _ref.alpha;
var f = Math.abs(h / 60 % 2 - 1);
var res = void 0;
switch (Math.floor(h / 60)) {
case 0:
res = {
r: i * (1 + s * (3 / (2 - f) - 1)),
g: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
b: i * (1 - s)
};
break;
case 1:
res = {
r: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
g: i * (1 + s * (3 / (2 - f) - 1)),
b: i * (1 - s)
};
break;
case 2:
res = {
r: i * (1 - s),
g: i * (1 + s * (3 / (2 - f) - 1)),
b: i * (1 + s * (3 * (1 - f) / (2 - f) - 1))
};
break;
case 3:
res = {
r: i * (1 - s),
g: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
b: i * (1 + s * (3 / (2 - f) - 1))
};
break;
case 4:
res = {
r: i * (1 + s * (3 * (1 - f) / (2 - f) - 1)),
g: i * (1 - s),
b: i * (1 + s * (3 / (2 - f) - 1))
};
break;
case 5:
res = {
r: i * (1 + s * (3 / (2 - f) - 1)),
g: i * (1 - s),
b: i * (1 + s * (3 * (1 - f) / (2 - f) - 1))
};
break;
// normalize w + b to 1
if (w + b > 1) {
var s = w + b;
w /= s;
b /= s;
}
return hsv_to_rgb({
h: h,
s: b === 1 ? 1 : 1 - w / (1 - b),
v: 1 - b,
alpha: alpha
});
}
res.mode = 'rgb';
if (a !== undefined) res.a = a;
// Based on: https://en.wikipedia.org/wiki/HSL_and_HSV#Formal_derivation
function rgb_to_hsi (_ref) {
var r = _ref.r,
g = _ref.g,
b = _ref.b,
alpha = _ref.alpha;
var M = Math.max(r, g, b),
m = Math.min(r, g, b);
var res = {
mode: 'hsi',
s: r + g + b === 0 ? 0 : 1 - 3 * m / (r + g + b),
i: (r + g + b) / 3
};
if (M - m !== 0) res.h = (M === r ? (g - b) / (M - m) + (g < b) * 6 : M === g ? (b - r) / (M - m) + 2 : (r - g) / (M - m) + 4) * 60;
if (alpha !== undefined) res.alpha = alpha;
return res;

@@ -125,9 +175,8 @@ }

function to_hsl (_ref) {
function rgb_to_hsl (_ref) {
var r = _ref.r,
g = _ref.g,
b = _ref.b,
a = _ref.a;
alpha = _ref.alpha;
// if (flags & IS_CULORI && flags & IS_HSL) return arguments[0];
var M = Math.max(r, g, b),

@@ -141,3 +190,3 @@ m = Math.min(r, g, b);

if (M - m !== 0) res.h = (M === r ? (g - b) / (M - m) + (g < b) * 6 : M === g ? (b - r) / (M - m) + 2 : (r - g) / (M - m) + 4) * 60;
if (a !== undefined) res.a = a;
if (alpha !== undefined) res.alpha = alpha;
return res;

@@ -148,9 +197,8 @@ }

function to_hsv (_ref) {
function rgb_to_hsv (_ref) {
var r = _ref.r,
g = _ref.g,
b = _ref.b,
a = _ref.a;
alpha = _ref.alpha;
// if (flags & IS_CULORI && flags & IS_HSV) return arguments[0];
var M = Math.max(r, g, b),

@@ -164,66 +212,257 @@ m = Math.min(r, g, b);

if (M - m !== 0) res.h = (M === r ? (g - b) / (M - m) + (g < b) * 6 : M === g ? (b - r) / (M - m) + 2 : (r - g) / (M - m) + 4) * 60;
if (a !== undefined) res.a = a;
if (alpha !== undefined) res.alpha = alpha;
return res;
}
// Based on: https://en.wikipedia.org/wiki/HSL_and_HSV#Formal_derivation
/*
RGB to HWB converter
--------------------
function to_hsi (_ref) {
var r = _ref.r,
g = _ref.g,
b = _ref.b,
a = _ref.a;
References:
* https://drafts.csswg.org/css-color/#hwb-to-rgb
* https://en.wikipedia.org/wiki/HWB_color_model
* http://alvyray.com/Papers/CG/HWB_JGTv208.pdf
*/
// if (flags & IS_CULORI && flags & IS_HSI) return arguments[0];
var M = Math.max(r, g, b),
m = Math.min(r, g, b);
function rgb_to_hwb (rgba) {
var hsv = rgb_to_hsv(rgba);
if (hsv === undefined) return undefined;
var res = {
mode: 'hsi',
s: r + g + b === 0 ? 0 : 1 - 3 * m / (r + g + b),
i: (r + g + b) / 3
mode: 'hwb',
w: (1 - hsv.s) * hsv.v,
b: 1 - hsv.v
};
if (M - m !== 0) res.h = (M === r ? (g - b) / (M - m) + (g < b) * 6 : M === g ? (b - r) / (M - m) + 2 : (r - g) / (M - m) + 4) * 60;
if (a !== undefined) res.a = a;
if (hsv.h !== undefined) res.h = hsv.h;
if (hsv.alpha !== undefined) res.alpha = hsv.alpha;
return res;
}
var convert = function convert(color, mode) {
/*
References:
* https://drafts.csswg.org/css-color/#lab-to-lch
* https://drafts.csswg.org/css-color/#color-conversion-code
*/
var lab_to_lch = (function (_ref) {
var l = _ref.l,
a = _ref.a,
b = _ref.b,
alpha = _ref.alpha;
if (color === undefined) {
return undefined;
}
var res = {
mode: 'lch',
l: l,
c: Math.sqrt(a * a + b * b),
h: Math.atan2(b, a) * 180 / Math.PI
};
if (alpha !== undefined) res.alpha = alpha;
return res;
});
// If the color's in the same mode as needed, just return the color.
if (color.mode === mode) {
return color;
}
/*
References:
* https://drafts.csswg.org/css-color/#lch-to-lab
* https://drafts.csswg.org/css-color/#color-conversion-code
*/
var lch_to_lab = (function (_ref) {
var l = _ref.l,
c = _ref.c,
h = _ref.h,
alpha = _ref.alpha;
// Otherwise convert it to RGB, which is the intermediary format
// for converting between formats.
var rgb = void 0;
switch (color.mode) {
case 'rgb':
rgb = color;break;
case 'hsl':
rgb = from_hsl(color);break;
case 'hsv':
rgb = from_hsv(color);break;
case 'hsi':
rgb = from_hsi(color);break;
default:
return undefined;
var res = {
mode: 'lab',
l: l,
a: c * Math.cos(h / 180 * Math.PI),
b: c * Math.sin(h / 180 * Math.PI)
};
if (alpha !== undefined) res.alpha = alpha;
return res;
});
// D50 white
var Xn = 0.9642;
var Yn = 1.0000;
var Zn = 0.8249;
var k = Math.pow(29, 3) / Math.pow(3, 3);
var e = Math.pow(6, 3) / Math.pow(29, 3);
var lab_to_xyz = (function (_ref) {
var l = _ref.l,
a = _ref.a,
b = _ref.b;
var fx = a / 500 + (l + 16) / 116;
var fy = (l + 16) / 116 - b / 200;
return {
x: (Math.pow(fx, 3) > e ? Math.pow(fx, 3) : (116 * fx - 16) / k) * Xn,
y: (l > 8 ? Math.pow((l + 16) / 116, 3) : l / k) * Yn,
z: (Math.pow(fy, 3) > e ? Math.pow(fy, 3) : (116 * fy - 16) / k) * Zn
};
});
// D50 -> D65
var xyz_d50_to_d65 = (function (_ref) {
var x = _ref.x,
y = _ref.y,
z = _ref.z;
return {
x: x * 0.9555766 - y * 0.0230393 + z * 0.0631636,
y: x * -0.0282895 + y * 1.0099416 + z * 0.0210077,
z: x * 0.0122982 - y * 0.0204830 + z * 1.3299098
};
});
/*
CIE XYZ values to sRGB.
References:
* https://drafts.csswg.org/css-color/#color-conversion-code
* http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/
var lrgb_to_rgb = function lrgb_to_rgb(c) {
return c > 0.0031308 ? 1.055 * Math.pow(c, 1 / 2.4) - 0.055 : 12.92 * c;
};
var xyz_to_rgb = (function (_ref) {
var x = _ref.x,
y = _ref.y,
z = _ref.z;
return {
r: lrgb_to_rgb(x * 3.2404542 - y * 1.5371385 - 0.4985314 * z),
g: lrgb_to_rgb(x * -0.9692660 + y * 1.8760108 + 0.0415560 * z),
b: lrgb_to_rgb(x * 0.0556434 - y * 0.2040259 + 1.0572252 * z)
};
});
var lab_to_rgb = (function (lab) {
var res = xyz_to_rgb(xyz_d50_to_d65(lab_to_xyz(lab)));
res.mode = 'rgb';
if (lab.alpha !== undefined) res.alpha = lab.alpha;
return res;
});
/*
Convert sRGB values to CIE XYZ
using sRGB’s own white, D65 (no chromatic adaptation)
References:
* https://drafts.csswg.org/css-color/#color-conversion-code
* http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/
var rgb_to_lrgb = function rgb_to_lrgb(c) {
return c < 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
};
var rgb_to_xyz = (function (_ref) {
var r = _ref.r,
g = _ref.g,
b = _ref.b;
return {
x: rgb_to_lrgb(0.4124564 * r + 0.3575761 * g + 0.1804375 * b),
y: rgb_to_lrgb(0.2126729 * r + 0.7151522 * g + 0.0721750 * b),
z: rgb_to_lrgb(0.0193339 * r + 0.1191920 * g + 0.9503041 * b)
};
});
// D65 -> D50
var xyz_d65_to_d50 = (function (_ref) {
var x = _ref.x,
y = _ref.y,
z = _ref.z;
return {
x: x * 1.0478112 + y * 0.0228866 - z * 0.0501270,
y: x * 0.0295424 + y * 0.9904844 - z * 0.0170491,
z: x * -0.0092345 + y * 0.0150436 + z * 0.7521316
};
});
// D50 white
var Xn$1 = 0.9642;
var Yn$1 = 1.0000;
var Zn$1 = 0.8249;
var k$1 = Math.pow(29, 3) / Math.pow(3, 3);
var e$1 = Math.pow(6, 3) / Math.pow(29, 3);
var f = function f(value) {
return value > e$1 ? Math.cbrt(value) : (k$1 * value + 16) / 116;
};
var xyz_to_lab = (function (_ref) {
var x = _ref.x,
y = _ref.y,
z = _ref.z;
var f0 = f(x / Xn$1);
var f1 = f(y / Yn$1);
var f2 = f(z / Zn$1);
return {
l: 116 * f1 - 16,
a: 500 * (f0 - f1),
b: 200 * (f1 - f2)
};
});
var rgb_to_lab = (function (rgb) {
var res = xyz_to_lab(xyz_d65_to_d50(rgb_to_xyz(rgb)));
res.mode = 'lab';
if (rgb.alpha !== undefined) res.alpha = rgb.alpha;
return res;
});
var converters = {
hsi: { rgb: hsi_to_rgb },
hsl: { rgb: hsl_to_rgb },
hsv: { rgb: hsv_to_rgb },
hwb: { rgb: hwb_to_rgb },
lab: {
lch: lab_to_lch,
rgb: lab_to_rgb
},
lch: {
lab: lch_to_lab,
rgb: function rgb(c) {
return lch_to_lab(lab_to_rgb(c));
}
},
rgb: {
hsi: rgb_to_hsi,
hsl: rgb_to_hsl,
hsv: rgb_to_hsv,
hwb: rgb_to_hwb,
lab: rgb_to_lab,
lch: function lch(c) {
return rgb_to_lab(lab_to_lch(c));
}
}
};
switch (mode) {
case 'rgb':
return rgb;
case 'hsl':
return to_hsl(rgb);
case 'hsv':
return to_hsv(rgb);
case 'hsi':
return to_hsi(rgb);
default:
return undefined;
}};
var convert = (function (color, target_mode) {
return color !== undefined ?
// if the color's mode corresponds to our target mode
color.mode === target_mode ?
// then just return the color
color
// otherwise check to see if we have a dedicated
// converter for the target mode
: converters[color.mode][target_mode] ?
// and return its result...
converters[color.mode][target_mode](color)
// ...otherwise pass through RGB as an intermediary step.
// if the target mode is RGB...
: mode === 'rgb' ?
// just return the RGB
converters[color.mode].rgb(color)
// otherwise convert color.mode -> RGB -> target_mode
: converters.rgb[target_mode](converters[color.mode].rgb(color)) : undefined;
});

@@ -251,3 +490,3 @@ var parseNumber = (function (color, len) {

b: (color >> 4 & 0xF | color & 0xF0) / 255,
a: (color & 0xF | color << 4 & 0xF0) / 255
alpha: (color & 0xF | color << 4 & 0xF0) / 255
};

@@ -273,3 +512,3 @@ }

b: (color >> 8 & 0xFF) / 255,
a: (color & 0xFF) / 255
alpha: (color & 0xFF) / 255
};

@@ -279,3 +518,3 @@ }

var named = {
var named_colors = {
aliceblue: 0xf0f8ff,

@@ -435,38 +674,61 @@ antiquewhite: 0xfaebd7,

var parseNamed = (function (color) {
return typeof color === 'string' && parseNumber(named[color.toLowerCase()], 6) || undefined;
return typeof color === 'string' && parseNumber(named_colors[color.toLowerCase()], 6) || undefined;
});
// converts a regexp to string
// (does not cover all cases, but for our purposes it works)
var s = function s(r) {
return r.toString().replace(/^\/|\/$/g, '');
};
/*
Basic building blocks for color regexes
---------------------------------------
// matches a number
var n = /([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)/;
These regexes are expressed as strings
to be interpolated in the color regexes.
*/
// matches a percentage
var p = /([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)%/;
var sp = s(p);
var num = (/([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)/ + '').replace(/^\/|\/$/g, ''); // number
var per = num + '%'; // percentage
var alpha = '(?:' + num + '%|' + num + ')'; // alpha-value
var hue = '(?:' + num + '(deg|grad|rad|turn)|' + num + ')'; // hue
var c = '\\s*,\\s*'; // comma
var s = '\\s+'; // space
// matches a percentage or number
var pn = '(?:' + s(p) + '|' + s(n) + ')';
// hue can be a number or an angle (number + angle unit)
var hue = '(?:' + s(n) + '(deg|grad|rad|turn)|' + s(n) + ')';
/*
rgb() regular expressions.
Reference: https://drafts.csswg.org/css-color/#rgb-functions
*/
var rgb_num_old = new RegExp('^rgba?\\(\\s*' + num + c + num + c + num + '\\s*(?:,\\s*' + alpha + '\\s*)?\\)$');
var rgb_per_old = new RegExp('^rgba?\\(\\s*' + per + c + per + c + per + '\\s*(?:,\\s*' + alpha + '\\s*)?\\)$');
var rgb_num_new = new RegExp('^rgba?\\(\\s*' + num + s + num + s + num + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
var rgb_per_new = new RegExp('^rgba?\\(\\s*' + per + s + per + s + per + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
// Reference:
// https://drafts.csswg.org/css-color/#rgb-functions
var rgb_legacy = new RegExp('^rgba?\\(\\s*' + pn + '\\s*,\\s*' + pn + '\\s*,\\s*' + pn + '\\s*(?:,\\s*' + pn + '\\s*)?\\)$');
var rgb_current = new RegExp('^rgba?\\(\\s*' + pn + '\\s+' + pn + '\\s+' + pn + '\\s*(?:\\/\\s*' + pn + '\\s*)?\\)$');
/*
hsl() regular expressions.
Reference: https://drafts.csswg.org/css-color/#the-hsl-notation
*/
var hsl_old = new RegExp('^hsla?\\(\\s*' + hue + c + per + c + per + '\\s*(?:,\\s*' + alpha + '\\s*)?\\)$');
var hsl_new = new RegExp('^hsla?\\(\\s*' + hue + s + per + s + per + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
// Reference:
// https://drafts.csswg.org/css-color/#the-hsl-notation
var hsl_legacy = new RegExp('^hsla?\\(\\s*' + hue + '\\s*,\\s*' + sp + '\\s*,\\s*' + sp + '\\s*(?:,\\s*' + pn + '\\s*)?\\)$');
var hsl_current = new RegExp('^hsla?\\(\\s*' + hue + '\\s+' + sp + '\\s+' + sp + '\\s*(?:\\/\\s*' + pn + '\\s*)?\\)$');
/*
hexadecimal regular expressions.
*/
var hex = /^#?([0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{4}|[0-9a-f]{3})$/i;
var hwb = new RegExp('^hwb\\(\\s*' + hue + '\\s+' + sp + '\\s+' + sp + '\\s*(?:\\/\\s*' + pn + '\\s*)?\\)$');
/*
hwb() regular expressions.
Reference: https://drafts.csswg.org/css-color/#the-hwb-notation
*/
var hwb = new RegExp('^hwb\\(\\s*' + hue + s + per + s + per + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
/*
lab() and lch() regular expressions.
Reference: https://drafts.csswg.org/css-color/#lab-colors
*/
var lab = new RegExp('^lab\\(\\s*' + num + s + num + s + num + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
var lch = new RegExp('^lch\\(\\s*' + num + s + num + s + hue + '\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
/*
gray() regular expressions.
Reference: https://drafts.csswg.org/css-color/#grays
*/
var gray = new RegExp('^gray\\(\\s*' + num + '()()\\s*(?:\\/\\s*' + alpha + '\\s*)?\\)$');
var parseHex = (function (color) {

@@ -478,17 +740,30 @@ var match;

var parseRgb = (function (color) {
var match = color.match(rgb_legacy) || color.match(rgb_current);
if (!match) return;
var res = {
mode: 'rgb',
r: match[1] === undefined ? match[2] / 255 : match[1] / 100,
g: match[3] === undefined ? match[4] / 255 : match[3] / 100,
b: match[5] === undefined ? match[6] / 255 : match[5] / 100
};
if (match[7] !== undefined) {
res.a = match[7] / 100;
} else if (match[8] !== undefined) {
res.a = +match[8];
var match = void 0,
res = void 0;
if (match = color.match(rgb_num_old) || color.match(rgb_num_new)) {
res = {
mode: 'rgb',
r: match[1] / 255,
g: match[2] / 255,
b: match[3] / 255
};
} else if (match = color.match(rgb_per_old) || color.match(rgb_per_new)) {
res = {
mode: 'rgb',
r: match[1] / 100,
g: match[2] / 100,
b: match[3] / 100
};
} else {
return undefined;
}
if (match[4] !== undefined) {
res.alpha = match[4] / 100;
} else if (match[5] !== undefined) {
res.alpha = +match[5];
}
return res;

@@ -512,3 +787,3 @@ });

if (typeof color !== 'string') return;
var match = color.match(hsl_legacy) || color.match(hsl_current);
var match = color.match(hsl_old) || color.match(hsl_new);
if (!match) return;

@@ -522,8 +797,8 @@ var res = {

if (match[6] !== undefined) {
res.a = match[6] / 100;
res.alpha = match[6] / 100;
} else if (match[7] !== undefined) {
res.a = match[7] / 255;
res.alpha = match[7] / 255;
}
// TODO better way to normalize than via rgb?
return to_hsl(from_hsl(res));
return rgb_to_hsl(hsl_to_rgb(res));
});

@@ -550,5 +825,5 @@

if (match[6] !== undefined) {
res.a = match[6] / 100;
res.alpha = match[6] / 100;
} else if (match[7] !== undefined) {
res.a = match[7] / 255;
res.alpha = match[7] / 255;
}

@@ -558,15 +833,61 @@ return res;

var parseLab = (function (color) {
var match = void 0,
res = void 0;
if (match = color.match(lab)) {
res = {
mode: 'lab',
l: +match[1],
a: +match[2],
b: +match[3]
};
} else if (match = color.match(gray)) {
res = {
mode: 'lab',
l: +match[1],
a: 0,
b: 0
};
} else {
return undefined;
}
if (match[4] !== undefined) {
res.alpha = match[4] / 100;
} else if (match[5] !== undefined) {
res.alpha = +match[5];
}
return res;
});
var parseLch = (function (color) {
var match = color.match(lch);
if (!match) {
return undefined;
}
var res = {
mode: 'lch',
l: +match[1],
c: Math.max(0, +match[2]),
h: match[5] === undefined ? hue$1(match[3], match[4]) : +match[5]
};
if (match[6] !== undefined) {
res.alpha = match[6] / 100;
} else if (match[7] !== undefined) {
res.alpha = +match[7];
}
return res;
});
var parse = function parse(color) {
return parseHex(color) || parseRgb(color) || parseHsl(color) || parseNamed(color) || (color === 'transparent' ? parseNumber(0x00000000, 8) : undefined) || parseHwb(color);
return parseHex(color) || parseRgb(color) || parseHsl(color) || parseNamed(color) || (color === 'transparent' ? parseNumber(0x00000000, 8) : undefined) || parseHwb(color) || parseLab(color) || parseLch(color);
};
Object.assign(parse, {
number: parseNumber,
named: parseNamed,
hex: parseHex,
rgb: parseRgb,
hsl: parseHsl,
hwb: parseHwb
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {

@@ -616,2 +937,10 @@ return typeof obj;

var lab$1 = (function (color) {
return convert(prepare(color, 'lab'), 'lab');
});
var lch$1 = (function (color) {
return convert(prepare(color, 'lch'), 'lch');
});
// From: https://github.com/d3/d3-format/issues/32

@@ -661,3 +990,3 @@

if (format === 'rgb') {
if (color['a'] === undefined || color['a'] === 1) {
if (color.alpha === undefined || color.alpha === 1) {
// opaque color

@@ -667,3 +996,3 @@ return 'rgb(' + r + ', ' + g + ', ' + b + ')';

// transparent color
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + color['a'] + ')';
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + color.alpha + ')';
}

@@ -673,6 +1002,21 @@ }

var colors = {
named: named
};
var components = {};
components.rgb = ['r', 'g', 'b', 'alpha'];
components.hsl = ['h', 's', 'l', 'alpha'];
components.hsv = ['h', 's', 'v', 'alpha'];
components.hsi = ['h', 's', 'i', 'alpha'];
components.lab = ['l', 'a', 'b', 'alpha'];
components.lch = ['l', 'c', 'h', 'alpha'];
components.hwb = ['h', 'w', 'b', 'alpha'];
var map = (function (color, fn) {
return components[color.mode].reduce(function (result, k) {
if (color[k] !== undefined) {
result[k] = fn(k, color[k]);
}
return result;
}, {});
});
var culori = function culori(color) {

@@ -682,15 +1026,3 @@ return rgb(color);

Object.assign(culori, colors, {
hsl: hsl,
hsv: hsv,
hsi: hsi,
hwb: hwb$1,
rgb: rgb,
css: css,
convert: convert,
prepare: prepare,
parse: parse,
round: round
});
export default culori;
export { hsl, hsv, hsi, hwb$1 as hwb, rgb, lab$1 as lab, lch$1 as lch, css, convert, prepare, round, parse, parseNumber, parseNamed, parseHex, parseRgb, parseHsl, parseHwb, named_colors as named, map };

@@ -1,11 +0,19 @@

# Version 0.2.2
## Culori 0.2.x
Published to NPM in UMD format (normal + minified) + ES6 modules.
### 0.2.5
# Version 0.2.0
Added support for the [HWB](https://drafts.csswg.org/css-color/#the-hwb-notation) color space.
### 0.2.2
[Published to NPM](https://npmjs.org/package/culori) in UMD format (normal + minified) + ES6 modules.
### 0.2.0
First stab at a stable API.
# Version 0.1.0
## Culori 0.1.x
### 0.1.0
Initial sketches.
{
"name": "culori",
"version": "0.2.5",
"version": "0.2.6",
"main": "build/culori.js",

@@ -5,0 +5,0 @@ "module": "build/index.js",

@@ -5,4 +5,6 @@ # Culori

## Use cases
## Why I built this
There are already [several excellent libraries](#about-the-project) out there for manipulating colors in JavaScript.
This library aims to provide a simple API to:

@@ -35,4 +37,4 @@

* ✓ [HWB](https://drafts.csswg.org/css-color/#the-hwb-notation)
* [LAB and LCH](https://drafts.csswg.org/css-color/#lab-colors)
* [Grays](https://drafts.csswg.org/css-color/#grays)
* ✓ [LAB and LCH](https://drafts.csswg.org/css-color/#lab-colors)
* ✓ [Grays](https://drafts.csswg.org/css-color/#grays)

@@ -95,3 +97,3 @@ Additionally, it supports:

When passed a culori object:
When passed a Culori object:

@@ -109,3 +111,3 @@ * if it's a __HSL__ object, it just returns it back

When passed a culori object:
When passed a Culori object:

@@ -123,3 +125,3 @@ * if it's a __HSV__ object, it just returns it back

When passed a culori object:
When passed a Culori object:

@@ -164,10 +166,10 @@ * if it's a __HSI__ object, it just returns it back

## Prior art
## About the project
This library is indebted to these projects:
Culori is written by [Dan Burzo](http://danburzo.ro) and is released under the [MIT License](./LICENSE).
* [chroma.js](https://github.com/gka/chroma.js)
* [d3-color](https://github.com/d3/d3-color)
* [TinyColor](https://github.com/bgrins/TinyColor)
It builds upon the ideas of two thoroughly documented and time-tested projects: [chroma.js](https://github.com/gka/chroma.js) by [Gregor Aisch](https://driven-by-data.net/) and [d3-color](https://github.com/d3/d3-color) by [Mike Bostock](https://bost.ocks.org/mike/).
You may also want to look at [TinyColor](https://github.com/bgrins/TinyColor) by [Brian Grinstead](http://briangrinstead.com), [color](https://github.com/Qix-/color) by [Heather Arthur](https://github.com/Qix-/), and [color.js](https://github.com/brehaut/color-js) by Andrew Brehaut et al.
## Benchmarks

@@ -180,3 +182,3 @@

* [HSL and HSV](https://en.wikipedia.org/wiki/HSL_and_HSV) on Wikipedia
* [CSS Color Module Level 4](https://drafts.csswg.org/css-color/)
* [CSS Color Module Level 4][css4-colors]
* [CSSOM standard serialization](https://drafts.csswg.org/cssom/#serialize-a-css-component-value)

@@ -183,0 +185,0 @@

@@ -14,3 +14,3 @@ const rollup = require('rollup');

{
input: "index.js",
input: "src/culori.js",
output: {

@@ -26,3 +26,3 @@ file: 'build/culori.js',

{
input: "index.js",
input: "src/culori.js",
output: {

@@ -38,3 +38,3 @@ file: 'build/culori.min.js',

{
input: 'index.js',
input: 'src/index.js',
output: {

@@ -41,0 +41,0 @@ file: 'build/index.js',

@@ -1,40 +0,55 @@

import from_hsl from '../converters/from_hsl';
import from_hsv from '../converters/from_hsv';
import from_hsi from '../converters/from_hsi';
import to_hsl from '../converters/to_hsl';
import to_hsv from '../converters/to_hsv';
import to_hsi from '../converters/to_hsi';
import hsi_to_rgb from '../converters/hsi_to_rgb';
import hsl_to_rgb from '../converters/hsl_to_rgb';
import hsv_to_rgb from '../converters/hsv_to_rgb';
import hwb_to_rgb from '../converters/hwb_to_rgb';
import rgb_to_hsi from '../converters/rgb_to_hsi';
import rgb_to_hsl from '../converters/rgb_to_hsl';
import rgb_to_hsv from '../converters/rgb_to_hsv';
import rgb_to_hwb from '../converters/rgb_to_hwb';
import lab_to_lch from '../converters/lab_to_lch';
import lch_to_lab from '../converters/lch_to_lab';
import lab_to_rgb from '../converters/lab_to_rgb';
import rgb_to_lab from '../converters/rgb_to_lab';
const convert = (color, mode) => {
if (color === undefined) {
return undefined;
const converters = {
hsi: { rgb: hsi_to_rgb },
hsl: { rgb: hsl_to_rgb },
hsv: { rgb: hsv_to_rgb },
hwb: { rgb: hwb_to_rgb },
lab: {
lch: lab_to_lch,
rgb: lab_to_rgb
},
lch: {
lab: lch_to_lab,
rgb: c => lch_to_lab(lab_to_rgb(c))
},
rgb: {
hsi: rgb_to_hsi,
hsl: rgb_to_hsl,
hsv: rgb_to_hsv,
hwb: rgb_to_hwb,
lab: rgb_to_lab,
lch: c => rgb_to_lab(lab_to_lch(c))
}
// If the color's in the same mode as needed, just return the color.
if (color.mode === mode) {
return color;
}
// Otherwise convert it to RGB, which is the intermediary format
// for converting between formats.
let rgb;
switch(color.mode) {
case 'rgb': rgb = color; break;
case 'hsl': rgb = from_hsl(color); break;
case 'hsv': rgb = from_hsv(color); break;
case 'hsi': rgb = from_hsi(color); break;
default: return undefined;
}
switch(mode) {
case 'rgb': return rgb;
case 'hsl': return to_hsl(rgb);
case 'hsv': return to_hsv(rgb);
case 'hsi': return to_hsi(rgb);
default: return undefined;
};
};
export default convert;
export default (color, target_mode) =>
color !== undefined ?
// if the color's mode corresponds to our target mode
color.mode === target_mode ?
// then just return the color
color
// otherwise check to see if we have a dedicated
// converter for the target mode
: converters[color.mode][target_mode] ?
// and return its result...
converters[color.mode][target_mode](color)
// ...otherwise pass through RGB as an intermediary step.
// if the target mode is RGB...
: mode === 'rgb' ?
// just return the RGB
converters[color.mode].rgb(color)
// otherwise convert color.mode -> RGB -> target_mode
: converters.rgb[target_mode](converters[color.mode].rgb(color))
: undefined;

@@ -17,3 +17,3 @@ import rgb from './rgb';

if (format === 'rgb') {
if (color['a'] === undefined || color['a'] === 1) {
if (color.alpha === undefined || color.alpha === 1) {
// opaque color

@@ -23,5 +23,5 @@ return `rgb(${r}, ${g}, ${b})`;

// transparent color
return `rgba(${r}, ${g}, ${b}, ${ color['a'] })`;
return `rgba(${r}, ${g}, ${b}, ${ color.alpha })`;
}
}
}

@@ -7,2 +7,4 @@ import parseNumber from '../parsers/number';

import parseHwb from '../parsers/hwb';
import parseLab from '../parsers/lab';
import parseLch from '../parsers/lch';

@@ -16,15 +18,19 @@ const parse = color => {

(color === 'transparent' ? parseNumber(0x00000000, 8) : undefined) ||
parseHwb(color)
parseHwb(color) ||
parseLab(color) ||
parseLch(color)
);
};
Object.assign(parse, {
number: parseNumber,
named: parseNamed,
hex: parseHex,
rgb: parseRgb,
hsl: parseHsl,
hwb: parseHwb
});
export {
parseNumber,
parseNamed,
parseHex,
parseRgb,
parseHsl,
parseHwb,
parseLab,
parseLch
};
export default parse;

@@ -1,32 +0,6 @@

import hsl from './api/hsl';
import hsv from './api/hsv';
import hsi from './api/hsi';
import hwb from './api/hwb';
import rgb from './api/rgb';
import css from './api/css';
import parse from './api/parse';
import round from './api/round';
import convert from './api/convert';
import prepare from './api/prepare';
import colors from './api/colors/index';
import * as methods from './index';
import culori from './index';
const culori = color => rgb(color);
Object.assign(culori, methods);
Object.assign(
culori,
colors,
{
hsl,
hsv,
hsi,
hwb,
rgb,
css,
convert,
prepare,
parse,
round
}
);
export default culori;

@@ -1,10 +0,10 @@

import { hsl_legacy, hsl_current } from '../util/regex';
import { hsl_old, hsl_new } from '../util/regex';
import { hue } from '../util/hue';
import from_hsl from '../converters/from_hsl';
import to_hsl from '../converters/to_hsl';
import hsl_to_rgb from '../converters/hsl_to_rgb';
import rgb_to_hsl from '../converters/rgb_to_hsl';
export default color => {
if (typeof color !== 'string') return;
let match = color.match(hsl_legacy) || color.match(hsl_current);
let match = color.match(hsl_old) || color.match(hsl_new);
if (!match) return;

@@ -18,8 +18,8 @@ let res = {

if (match[6] !== undefined) {
res.a = match[6] / 100;
res.alpha = match[6] / 100;
} else if (match[7] !== undefined) {
res.a = match[7] / 255;
res.alpha = match[7] / 255;
}
// TODO better way to normalize than via rgb?
return to_hsl(from_hsl(res));
return rgb_to_hsl(hsl_to_rgb(res));
}

@@ -23,7 +23,7 @@ import { hwb } from '../util/regex';

if (match[6] !== undefined) {
res.a = match[6] / 100;
res.alpha = match[6] / 100;
} else if (match[7] !== undefined) {
res.a = match[7] / 255;
res.alpha = match[7] / 255;
}
return res;
}

@@ -22,3 +22,3 @@ export default (color, len) => {

b: ((color >> 4 & 0xF) | (color & 0xF0)) / 255,
a: ((color & 0xF) | (color << 4 & 0xF0)) / 255
alpha: ((color & 0xF) | (color << 4 & 0xF0)) / 255
};

@@ -44,5 +44,5 @@ }

b: (color >> 8 & 0xFF) / 255,
a: (color & 0xFF) / 255
alpha: (color & 0xFF) / 255
};
}
}
import {
rgb_legacy,
rgb_current
rgb_num_old,
rgb_num_new,
rgb_per_old,
rgb_per_new
} from '../util/regex';
export default color => {
var match = color.match(rgb_legacy) || color.match(rgb_current);
if (!match) return;
let res = {
mode: 'rgb',
r: match[1] === undefined ? match[2] / 255 : match[1] / 100,
g: match[3] === undefined ? match[4] / 255 : match[3] / 100,
b: match[5] === undefined ? match[6] / 255 : match[5] / 100
};
let match, res;
if (match[7] !== undefined) {
res.a = match[7] / 100;
} else if (match[8] !== undefined) {
res.a = +match[8];
if (match = color.match(rgb_num_old) || color.match(rgb_num_new)) {
res = {
mode: 'rgb',
r: match[1] / 255,
g: match[2] / 255,
b: match[3] / 255
};
} else if (match = color.match(rgb_per_old) || color.match(rgb_per_new)) {
res = {
mode: 'rgb',
r: match[1] / 100,
g: match[2] / 100,
b: match[3] / 100
};
} else {
return undefined;
}
if (match[4] !== undefined) {
res.alpha = match[4] / 100;
} else if (match[5] !== undefined) {
res.alpha = +match[5];
}
return res;
};

@@ -1,31 +0,69 @@

// converts a regexp to string
// (does not cover all cases, but for our purposes it works)
const s = r => r.toString().replace(/^\/|\/$/g, '');
/*
Basic building blocks for color regexes
---------------------------------------
// matches a number
const n = /([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)/;
These regexes are expressed as strings
to be interpolated in the color regexes.
*/
// matches a percentage
const p = /([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)%/;
const sp = s(p);
const num = (/([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)/ + '').replace(/^\/|\/$/g, ''); // number
const per = `${num}%`; // percentage
const alpha = `(?:${num}%|${num})`; // alpha-value
const hue = `(?:${num}(deg|grad|rad|turn)|${num})`; // hue
const c = `\\s*,\\s*`; // comma
const s = `\\s+`; // space
// matches a percentage or number
const pn = `(?:${s(p)}|${s(n)})`;
/*
rgb() regular expressions.
Reference: https://drafts.csswg.org/css-color/#rgb-functions
*/
const rgb_num_old = new RegExp(`^rgba?\\(\\s*${num}${c}${num}${c}${num}\\s*(?:,\\s*${alpha}\\s*)?\\)$`);
const rgb_per_old = new RegExp(`^rgba?\\(\\s*${per}${c}${per}${c}${per}\\s*(?:,\\s*${alpha}\\s*)?\\)$`);
const rgb_num_new = new RegExp(`^rgba?\\(\\s*${num}${s}${num}${s}${num}\\s*(?:\\/\\s*${alpha}\\s*)?\\)$`);
const rgb_per_new = new RegExp(`^rgba?\\(\\s*${per}${s}${per}${s}${per}\\s*(?:\\/\\s*${alpha}\\s*)?\\)$`);
// hue can be a number or an angle (number + angle unit)
const hue = `(?:${s(n)}(deg|grad|rad|turn)|${s(n)})`;
/*
hsl() regular expressions.
Reference: https://drafts.csswg.org/css-color/#the-hsl-notation
*/
const hsl_old = new RegExp(`^hsla?\\(\\s*${hue}${c}${per}${c}${per}\\s*(?:,\\s*${alpha}\\s*)?\\)$`);
const hsl_new = new RegExp(`^hsla?\\(\\s*${hue}${s}${per}${s}${per}\\s*(?:\\/\\s*${alpha}\\s*)?\\)$`);
// Reference:
// https://drafts.csswg.org/css-color/#rgb-functions
export const rgb_legacy = new RegExp(`^rgba?\\(\\s*${pn}\\s*,\\s*${pn}\\s*,\\s*${pn}\\s*(?:,\\s*${pn}\\s*)?\\)$`);
export const rgb_current = new RegExp(`^rgba?\\(\\s*${pn}\\s+${pn}\\s+${pn}\\s*(?:\\/\\s*${pn}\\s*)?\\)$`);
/*
hexadecimal regular expressions.
*/
const hex = /^#?([0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{4}|[0-9a-f]{3})$/i;
// Reference:
// https://drafts.csswg.org/css-color/#the-hsl-notation
export const hsl_legacy = new RegExp(`^hsla?\\(\\s*${hue}\\s*,\\s*${sp}\\s*,\\s*${sp}\\s*(?:,\\s*${pn}\\s*)?\\)$`);
export const hsl_current = new RegExp(`^hsla?\\(\\s*${hue}\\s+${sp}\\s+${sp}\\s*(?:\\/\\s*${pn}\\s*)?\\)$`);
/*
hwb() regular expressions.
Reference: https://drafts.csswg.org/css-color/#the-hwb-notation
*/
const hwb = new RegExp(`^hwb\\(\\s*${hue}${s}${per}${s}${per}\\s*(?:\\/\\s*${alpha}\\s*)?\\)$`);
export const hex = /^#?([0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{4}|[0-9a-f]{3})$/i;
/*
lab() and lch() regular expressions.
Reference: https://drafts.csswg.org/css-color/#lab-colors
*/
const lab = new RegExp(`^lab\\(\\s*${num}${s}${num}${s}${num}\\s*(?:\\/\\s*${alpha}\\s*)?\\)$`);
const lch = new RegExp(`^lch\\(\\s*${num}${s}${num}${s}${hue}\\s*(?:\\/\\s*${alpha}\\s*)?\\)$`);
export const hwb = new RegExp(`^hwb\\(\\s*${hue}\\s+${sp}\\s+${sp}\\s*(?:\\/\\s*${pn}\\s*)?\\)$`);
/*
gray() regular expressions.
Reference: https://drafts.csswg.org/css-color/#grays
*/
const gray = new RegExp(`^gray\\(\\s*${num}()()\\s*(?:\\/\\s*${alpha}\\s*)?\\)$`);
export {
rgb_num_old,
rgb_num_new,
rgb_per_old,
rgb_per_new,
hsl_old,
hsl_new,
hex,
hwb,
lab,
lch,
gray
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc