Comparing version 0.2.7 to 0.2.8
@@ -767,4 +767,4 @@ (function (global, factory) { | ||
h: match[3] === undefined ? hue$1(match[1], match[2]) : +match[3], | ||
s: match[4] / 100, | ||
l: match[5] / 100 | ||
s: Math.min(Math.max(0, match[4] / 100), 1), | ||
l: Math.min(Math.max(0, match[5] / 100), 1) | ||
}; | ||
@@ -776,4 +776,3 @@ if (match[6] !== undefined) { | ||
} | ||
// TODO better way to normalize than via rgb? | ||
return rgb_to_hsl(hsl_to_rgb(res)); | ||
return res; | ||
}); | ||
@@ -967,3 +966,3 @@ | ||
// transparent color | ||
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + color.alpha + ')'; | ||
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + round(color.alpha, 2) + ')'; | ||
} | ||
@@ -984,15 +983,19 @@ } | ||
var map = (function (color, fn) { | ||
return components[color.mode].reduce(function (result, k) { | ||
if (color[k] !== undefined) { | ||
result[k] = fn(k, color[k]); | ||
var c = components[color.mode].reduce(function (result, k) { | ||
var res = fn(k, color[k]); | ||
if (res !== undefined) { | ||
result[k] = res; | ||
} | ||
return result; | ||
}, {}); | ||
c.mode = color.mode; | ||
return c; | ||
}); | ||
var interpolate = function interpolate(a, b, t) { | ||
return (t - a) / (b - a); | ||
return a === undefined || b === undefined ? undefined : a + t * (b - a); | ||
}; | ||
var interpolate$1 = (function (colors) { | ||
var mode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'rgb'; | ||
@@ -1002,8 +1005,15 @@ if (colors.length < 2) { | ||
} | ||
var arr = colors; | ||
// let arr = colors.map(c => convert(prepare(color, mode), mode)); | ||
var classes = colors.length - 1; | ||
var arr = colors.map(function (color) { | ||
return convert(prepare(color, mode), mode); | ||
}); | ||
return function (t) { | ||
var cls = t * (colors.length - 1), | ||
i = Math.floor(cls); | ||
return interpolate(arr[i], arr[i + 1], cls - i); | ||
var cls = Math.min(Math.max(0, t), 1) * classes, | ||
i = Math.floor(cls), | ||
colorA = arr[i], | ||
colorB = arr[i + 1], | ||
tt = (cls - i) / classes; | ||
return map({ mode: mode }, function (k) { | ||
return i === classes ? colorA[k] : interpolate(colorA[k], colorB[k], tt); | ||
}); | ||
}; | ||
@@ -1010,0 +1020,0 @@ }); |
@@ -1,1 +0,1 @@ | ||
!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;(a%=360)<0&&(a+=360);var 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;(a%=360)<0&&(a+=360);var 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.1338561*a-1.6168667*r-.4906146*n),g:h(-.9787684*a+1.9161415*r+.033454*n),b:h(.0719453*a-.2289914*r+1.4052427*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))}}((r=(a=e).r,n=a.g,t=a.b,{x:.4360747*(r=u(r))+.3850649*(n=u(n))+.1430804*(t=u(t)),y:.2225045*r+.7168786*n+.0606169*t,z:.0139322*r+.0971045*n+.7141733*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;(a%=360)<0&&(a+=360);var 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*)?\\)$"),j=new RegExp("^rgba?\\(\\s*"+y+E+y+E+y+"\\s*(?:\\/\\s*"+M+"\\s*)?\\)$"),z=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(j))r={mode:"rgb",r:a[1]/255,g:a[2]/255,b:a[3]/255};else{if(!(a=e.match(q)||e.match(z)))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},{})},interpolate:function(e){if(!(e.length<2)){var a=e;return function(r){var n=r*(e.length-1),t=Math.floor(n);return function(e,a,r){return(r-e)/(a-e)}(a[t],a[t+1],n-t)}}},swatches:function(e){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2;if(!(a<2)){for(var r=[],n=0;n<a;n++)r.push(e(n/(a-1)));return r}},default:W});return Object.assign(W,X),W}); | ||
!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.v,t=e.alpha;(a%=360)<0&&(a+=360);var 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 a(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 r=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},n=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},t=Math.pow(29,3)/Math.pow(3,3),o=Math.pow(6,3)/Math.pow(29,3),i=function(e){return e>.0031308?1.055*Math.pow(e,1/2.4)-.055:12.92*e},l=function(e){var a,r,n,l,h=function(e){var a=e.x,r=e.y,n=e.z;return{r:i(3.1338561*a-1.6168667*r-.4906146*n),g:i(-.9787684*a+1.9161415*r+.033454*n),b:i(.0719453*a-.2289914*r+1.4052427*n)}}((r=(a=e).l,n=a.a/500+(r+16)/116,l=(r+16)/116-a.b/200,{x:.9642*(Math.pow(n,3)>o?Math.pow(n,3):(116*n-16)/t),y:1*(r>8?Math.pow((r+16)/116,3):r/t),z:.8249*(Math.pow(l,3)>o?Math.pow(l,3):(116*l-16)/t)}));return h.mode="rgb",void 0!==e.alpha&&(h.alpha=e.alpha),h},h=function(e){return e<.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},s=Math.pow(29,3)/Math.pow(3,3),u=Math.pow(6,3)/Math.pow(29,3),d=function(e){return e>u?Math.cbrt(e):(s*e+16)/116},b=function(e){var a,r,n,t,o=function(e){var a=e.x,r=e.y,n=e.z,t=d(a/.9642),o=d(r/1);return{l:116*o-16,a:500*(t-o),b:200*(o-d(n/.8249))}}((r=(a=e).r,n=a.g,t=a.b,{x:.4360747*(r=h(r))+.3850649*(n=h(n))+.1430804*(t=h(t)),y:.2225045*r+.7168786*n+.0606169*t,z:.0139322*r+.0971045*n+.7141733*t}));return o.mode="lab",void 0!==e.alpha&&(o.alpha=e.alpha),o},g={hsi:{rgb:function(e){var a=e.h,r=e.s,n=e.i,t=e.alpha;(a%=360)<0&&(a+=360);var 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:function(e){var a=e.h,r=e.s,n=e.l,t=e.alpha;(a%=360)<0&&(a+=360);var 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}},hsv:{rgb:e},hwb:{rgb:function(a){var r=a.h,n=a.w,t=a.b,o=a.alpha;if(n+t>1){var i=n+t;n/=i,t/=i}return e({h:r,s:1===t?1:1-n/(1-t),v:1-t,alpha:o})}},lab:{lch:r,rgb:l},lch:{lab:n,rgb:function(e){return n(l(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: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:"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},hsv:a,hwb:function(e){var r=a(e);if(void 0!==r){var n={mode:"hwb",w:(1-r.s)*r.v,b:1-r.v};return void 0!==r.h&&(n.h=r.h),void 0!==r.alpha&&(n.alpha=r.alpha),n}},lab:b,lch:function(e){return b(r(e))}}},c=function(e,a){return void 0!==e?e.mode===a?e:g[e.mode][a]?g[e.mode][a](e):"rgb"===mode?g[e.mode].rgb(e):g.rgb[a](g[e.mode].rgb(e)):void 0},p=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},v={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},f=function(e){return"string"==typeof e&&p(v[e.toLowerCase()],6)||void 0},m=(/([+-]?\d*\.?\d+(?:[eE][+-]?\d+)?)/+"").replace(/^\/|\/$/g,""),w=m+"%",y="(?:"+m+"%|"+m+")",k="(?:"+m+"(deg|grad|rad|turn)|"+m+")",M="\\s*,\\s*",x="\\s+",$=new RegExp("^rgba?\\(\\s*"+m+M+m+M+m+"\\s*(?:,\\s*"+y+"\\s*)?\\)$"),E=new RegExp("^rgba?\\(\\s*"+w+M+w+M+w+"\\s*(?:,\\s*"+y+"\\s*)?\\)$"),R=new RegExp("^rgba?\\(\\s*"+m+x+m+x+m+"\\s*(?:\\/\\s*"+y+"\\s*)?\\)$"),q=new RegExp("^rgba?\\(\\s*"+w+x+w+x+w+"\\s*(?:\\/\\s*"+y+"\\s*)?\\)$"),j=new RegExp("^hsla?\\(\\s*"+k+M+w+M+w+"\\s*(?:,\\s*"+y+"\\s*)?\\)$"),z=new RegExp("^hsla?\\(\\s*"+k+x+w+x+w+"\\s*(?:\\/\\s*"+y+"\\s*)?\\)$"),O=/^#?([0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{4}|[0-9a-f]{3})$/i,S=new RegExp("^hwb\\(\\s*"+k+x+w+x+w+"\\s*(?:\\/\\s*"+y+"\\s*)?\\)$"),I=new RegExp("^lab\\(\\s*"+m+x+m+x+m+"\\s*(?:\\/\\s*"+y+"\\s*)?\\)$"),P=new RegExp("^lch\\(\\s*"+m+x+m+x+k+"\\s*(?:\\/\\s*"+y+"\\s*)?\\)$"),H=new RegExp("^gray\\(\\s*"+m+"()()\\s*(?:\\/\\s*"+y+"\\s*)?\\)$"),N=function(e){var a;return(a=e.match(O))?p(parseInt(a[1],16),a[1].length):void 0},C=function(e){var a=void 0,r=void 0;if(a=e.match($)||e.match(R))r={mode:"rgb",r:a[1]/255,g:a[2]/255,b:a[3]/255};else{if(!(a=e.match(E)||e.match(q)))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},L=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}},A=function(e){if("string"==typeof e){var a=e.match(j)||e.match(z);if(a){var r={mode:"hsl",h:void 0===a[3]?L(a[1],a[2]):+a[3],s:Math.min(Math.max(0,a[4]/100),1),l:Math.min(Math.max(0,a[5]/100),1)};return void 0!==a[6]?r.alpha=a[6]/100:void 0!==a[7]&&(r.alpha=a[7]/255),r}}},B=function(e){if("string"==typeof e){var a=e.match(S);if(a){var r={mode:"hwb",h:void 0===a[3]?L(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}}},D=function(e){return N(e)||C(e)||A(e)||f(e)||("transparent"===e?p(0,8):void 0)||B(e)||function(e){var a=void 0,r=void 0;if(a=e.match(I))r={mode:"lab",l:+a[1],a:+a[2],b:+a[3]};else{if(!(a=e.match(H)))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(P);if(a){var r={mode:"lch",l:+a[1],c:Math.max(0,+a[2]),h:void 0===a[5]?L(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)},F="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},G=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},J=function(e,a){return"object"!==(void 0===e?"undefined":F(e))?D(e):void 0===e.mode?G({},e,{mode:a}):e},K=function(e){return c(J(e,"rgb"),"rgb")},Q=function(e,a){return Math.round(e*(a=Math.pow(10,a)))/a};function T(e){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:4;if(void 0!==e)return"number"==typeof e?Q(e,a):"string"==typeof e?e:"object"===(void 0===e?"undefined":F(e))?Object.keys(e).reduce(function(a,r){return a[r]=T(e[r]),a},{}):void 0}var U={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"]},V=function(e,a){var r=U[e.mode].reduce(function(r,n){var t=a(n,e[n]);return void 0!==t&&(r[n]=t),r},{});return r.mode=e.mode,r},W=function(e){return K(e)},X=Object.freeze({hsl:function(e){return c(J(e,"hsl"),"hsl")},hsv:function(e){return c(J(e,"hsv"),"hsv")},hsi:function(e){return c(J(e,"hsi"),"hsi")},hwb:function(e){return c(J(e,"hwb"),"hwb")},rgb:K,lab:function(e){return c(J(e,"lab"),"lab")},lch:function(e){return c(J(e,"lch"),"lch")},css:function(e){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"rgb",r=K(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+", "+T(r.alpha,2)+")":void 0},convert:c,prepare:J,round:T,parse:D,parseNumber:p,parseNamed:f,parseHex:N,parseRgb:C,parseHsl:A,parseHwb:B,named:v,map:V,interpolate:function(e){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"rgb";if(!(e.length<2)){var r=e.length-1,n=e.map(function(e){return c(J(e,a),a)});return function(e){var t=Math.min(Math.max(0,e),1)*r,o=Math.floor(t),i=n[o],l=n[o+1],h=(t-o)/r;return V({mode:a},function(e){return o===r?i[e]:(a=i[e],n=l[e],void 0===a||void 0===n?void 0:a+h*(n-a));var a,n})}}},swatches:function(e){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2;if(!(a<2)){for(var r=[],n=0;n<a;n++)r.push(e(n/(a-1)));return r}},default:W});return Object.assign(W,X),W}); |
@@ -761,4 +761,4 @@ // Based on: https://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB | ||
h: match[3] === undefined ? hue$1(match[1], match[2]) : +match[3], | ||
s: match[4] / 100, | ||
l: match[5] / 100 | ||
s: Math.min(Math.max(0, match[4] / 100), 1), | ||
l: Math.min(Math.max(0, match[5] / 100), 1) | ||
}; | ||
@@ -770,4 +770,3 @@ if (match[6] !== undefined) { | ||
} | ||
// TODO better way to normalize than via rgb? | ||
return rgb_to_hsl(hsl_to_rgb(res)); | ||
return res; | ||
}); | ||
@@ -961,3 +960,3 @@ | ||
// transparent color | ||
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + color.alpha + ')'; | ||
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + round(color.alpha, 2) + ')'; | ||
} | ||
@@ -978,15 +977,19 @@ } | ||
var map = (function (color, fn) { | ||
return components[color.mode].reduce(function (result, k) { | ||
if (color[k] !== undefined) { | ||
result[k] = fn(k, color[k]); | ||
var c = components[color.mode].reduce(function (result, k) { | ||
var res = fn(k, color[k]); | ||
if (res !== undefined) { | ||
result[k] = res; | ||
} | ||
return result; | ||
}, {}); | ||
c.mode = color.mode; | ||
return c; | ||
}); | ||
var interpolate = function interpolate(a, b, t) { | ||
return (t - a) / (b - a); | ||
return a === undefined || b === undefined ? undefined : a + t * (b - a); | ||
}; | ||
var interpolate$1 = (function (colors) { | ||
var mode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'rgb'; | ||
@@ -996,8 +999,15 @@ if (colors.length < 2) { | ||
} | ||
var arr = colors; | ||
// let arr = colors.map(c => convert(prepare(color, mode), mode)); | ||
var classes = colors.length - 1; | ||
var arr = colors.map(function (color) { | ||
return convert(prepare(color, mode), mode); | ||
}); | ||
return function (t) { | ||
var cls = t * (colors.length - 1), | ||
i = Math.floor(cls); | ||
return interpolate(arr[i], arr[i + 1], cls - i); | ||
var cls = Math.min(Math.max(0, t), 1) * classes, | ||
i = Math.floor(cls), | ||
colorA = arr[i], | ||
colorB = arr[i + 1], | ||
tt = (cls - i) / classes; | ||
return map({ mode: mode }, function (k) { | ||
return i === classes ? colorA[k] : interpolate(colorA[k], colorB[k], tt); | ||
}); | ||
}; | ||
@@ -1004,0 +1014,0 @@ }); |
{ | ||
"name": "culori", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"main": "build/culori.js", | ||
@@ -5,0 +5,0 @@ "module": "build/index.js", |
@@ -22,5 +22,5 @@ import rgb from './rgb'; | ||
// transparent color | ||
return `rgba(${r}, ${g}, ${b}, ${ color.alpha })`; | ||
return `rgba(${r}, ${g}, ${b}, ${ round(color.alpha, 2) })`; | ||
} | ||
} | ||
} |
import convert from './convert'; | ||
import prepare from './prepare'; | ||
import map from './map'; | ||
const interpolate = (a, b, t) => (t - a) / (b - a); | ||
const interpolate = (a, b, t) => a === undefined || b === undefined ? undefined : a + t * (b - a); | ||
@@ -10,8 +11,12 @@ export default (colors, mode = 'rgb') => { | ||
} | ||
let arr = colors; | ||
// let arr = colors.map(c => convert(prepare(color, mode), mode)); | ||
let classes = colors.length - 1; | ||
let arr = colors.map(color => convert(prepare(color, mode), mode)); | ||
return t => { | ||
let cls = t * (colors.length - 1), i = Math.floor(cls); | ||
return interpolate(arr[i], arr[i+1], cls - i); | ||
let cls = Math.min(Math.max(0, t), 1) * classes, | ||
i = Math.floor(cls), | ||
colorA = arr[i], | ||
colorB = arr[i + 1], | ||
tt = (cls - i) / classes; | ||
return map({ mode: mode }, k => i === classes ? colorA[k] : interpolate(colorA[k], colorB[k], tt)); | ||
}; | ||
}; |
@@ -11,10 +11,15 @@ const components = {}; | ||
export default (color, fn) => | ||
components[color.mode].reduce( | ||
export default (color, fn) => { | ||
let c = components[color.mode].reduce( | ||
(result, k) => { | ||
if (color[k] !== undefined) { | ||
result[k] = fn(k, color[k]); | ||
let res = fn(k, color[k]); | ||
if (res !== undefined) { | ||
result[k] = res; | ||
} | ||
return result; | ||
}, {} | ||
); | ||
}, | ||
{} | ||
); | ||
c.mode = color.mode; | ||
return c; | ||
} |
import { hsl_old, hsl_new } from '../util/regex'; | ||
import { hue } from '../util/hue'; | ||
import hsl_to_rgb from '../converters/hsl_to_rgb'; | ||
import rgb_to_hsl from '../converters/rgb_to_hsl'; | ||
export default color => { | ||
@@ -14,4 +11,4 @@ if (typeof color !== 'string') return; | ||
h: match[3] === undefined ? hue(match[1], match[2]) : +match[3], | ||
s: match[4] / 100, | ||
l: match[5] / 100 | ||
s: Math.min(Math.max(0, match[4] / 100), 1), | ||
l: Math.min(Math.max(0, match[5] / 100), 1) | ||
}; | ||
@@ -23,4 +20,3 @@ if (match[6] !== undefined) { | ||
} | ||
// TODO better way to normalize than via rgb? | ||
return rgb_to_hsl(hsl_to_rgb(res)); | ||
return res; | ||
} |
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
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
99615
53
2802