UNPKG

cyberalien-color

Version:

Color library

10 lines (9 loc) 17.3 kB
/** * This file is part of the cyberalien-color package. * * (c) Vjacheslav Trushkin <cyberalien@gmail.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ "use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();!function(a){var b={};!function(){var a={silver:[192,192,192],gray:[128,128,128],white:[255,255,255],maroon:[128,0,0],red:[255,0,0],purple:[128,0,128],fuchsia:[255,0,255],green:[0,128,0],lime:[0,255,0],olive:[128,128,0],yellow:[255,255,0],navy:[0,0,128],blue:[0,0,255],teal:[0,128,128],aqua:[0,255,255]},c={antiquewhite:[250,235,215],aqua:[0,255,255],aquamarine:[127,255,212],azure:[240,255,255],beige:[245,245,220],bisque:[255,228,196],black:[0,0,0],blanchedalmond:[255,235,205],blue:[0,0,255],blueviolet:[138,43,226],brown:[165,42,42],burlywood:[222,184,135],cadetblue:[95,158,160],chartreuse:[127,255,0],chocolate:[210,105,30],coral:[255,127,80],cornflowerblue:[100,149,237],cornsilk:[255,248,220],crimson:[220,20,60],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgoldenrod:[184,134,11],darkgray:[169,169,169],darkgreen:[0,100,0],darkgrey:[169,169,169],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkseagreen:[143,188,143],darkslateblue:[72,61,139],darkslategray:[47,79,79],darkslategrey:[47,79,79],darkturquoise:[0,206,209],darkviolet:[148,0,211],deeppink:[255,20,147],deepskyblue:[0,191,255],dimgray:[105,105,105],dimgrey:[105,105,105],dodgerblue:[30,144,255],firebrick:[178,34,34],floralwhite:[255,250,240],forestgreen:[34,139,34],fuchsia:[255,0,255],gainsboro:[220,220,220],ghostwhite:[248,248,255],gold:[255,215,0],goldenrod:[218,165,32],gray:[128,128,128],green:[0,128,0],greenyellow:[173,255,47],grey:[128,128,128],honeydew:[240,255,240],hotpink:[255,105,180],indianred:[205,92,92],indigo:[75,0,130],ivory:[255,255,240],khaki:[240,230,140],lavender:[230,230,250],lavenderblush:[255,240,245],lawngreen:[124,252,0],lemonchiffon:[255,250,205],lightblue:[173,216,230],lightcoral:[240,128,128],lightcyan:[224,255,255],lightgoldenrodyellow:[250,250,210],lightgray:[211,211,211],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightsalmon:[255,160,122],lightseagreen:[32,178,170],lightskyblue:[135,206,250],lightslategray:[119,136,153],lightslategrey:[119,136,153],lightsteelblue:[176,196,222],lightyellow:[255,255,224],lime:[0,255,0],limegreen:[50,205,50],linen:[250,240,230],magenta:[255,0,255],maroon:[128,0,0],mediumaquamarine:[102,205,170],mediumblue:[0,0,205],mediumorchid:[186,85,211],mediumpurple:[147,112,219],mediumseagreen:[60,179,113],mediumslateblue:[123,104,238],mediumspringgreen:[0,250,154],mediumturquoise:[72,209,204],mediumvioletred:[199,21,133],midnightblue:[25,25,112],mintcream:[245,255,250],mistyrose:[255,228,225],moccasin:[255,228,181],navajowhite:[255,222,173],navy:[0,0,128],oldlace:[253,245,230],olive:[128,128,0],olivedrab:[107,142,35],orange:[255,165,0],orangered:[255,69,0],orchid:[218,112,214],palegoldenrod:[238,232,170],palegreen:[152,251,152],paleturquoise:[175,238,238],palevioletred:[219,112,147],papayawhip:[255,239,213],peachpuff:[255,218,185],peru:[205,133,63],pink:[255,192,203],plum:[221,160,221],powderblue:[176,224,230],purple:[128,0,128],red:[255,0,0],rosybrown:[188,143,143],royalblue:[65,105,225],saddlebrown:[139,69,19],salmon:[250,128,114],sandybrown:[244,164,96],seagreen:[46,139,87],seashell:[255,245,238],sienna:[160,82,45],silver:[192,192,192],skyblue:[135,206,235],slateblue:[106,90,205],slategray:[112,128,144],slategrey:[112,128,144],snow:[255,250,250],springgreen:[0,255,127],steelblue:[70,130,180],tan:[210,180,140],teal:[0,128,128],thistle:[216,191,216],tomato:[255,99,71],turquoise:[64,224,208],violet:[238,130,238],wheat:[245,222,179],white:[255,255,255],whitesmoke:[245,245,245],yellow:[255,255,0],yellowgreen:[154,205,50],rebeccapurple:[102,51,153]},d={base:a,extended:c,all:Object.assign({},a,c)};b.Keywords=d}(),function(){var a=b.Keywords,c=function(){function b(){_classCallCheck(this,b),this._rgb=this._rgbRounded=this._hsl=this._hslRounded=this._luminance=null,this._alpha=1}return _createClass(b,[{key:"setRGB",value:function(a,b,c,d){return this._setRGBArray([a,b,c,1],void 0!==d&&d)}},{key:"setRGBA",value:function(a,b,c,d,e){return this._setRGBArray([a,b,c,d],void 0!==e&&e)}},{key:"setRGBArray",value:function(a,b){return this._setRGBArray(a.slice(0),"boolean"==typeof b&&b),void 0!==b&&b instanceof Array&&(this._rgbRounded=b.slice(0)),this}},{key:"setHSL",value:function(a,b,c,d){return this._setHSLArray([a,b,c,1],void 0!==d&&d)}},{key:"setHSLA",value:function(a,b,c,d,e){return this._setHSLArray([a,b,c,d],void 0!==e&&e)}},{key:"setHSLArray",value:function(a,b){return this._setHSLArray(a.slice(0),"boolean"==typeof b&&b),void 0!==b&&b instanceof Array&&(this._hslRounded=b.slice(0)),this}},{key:"setAlpha",value:function(a){return this._alpha=a,this}},{key:"setRed",value:function(a,b){return this._setRGB(0,a,void 0!==b&&b)}},{key:"setGreen",value:function(a,b){return this._setRGB(1,a,void 0!==b&&b)}},{key:"setBlue",value:function(a,b){return this._setRGB(2,a,void 0!==b&&b)}},{key:"setHue",value:function(a,b){return this._setHSL(0,a,void 0!==b&&b)}},{key:"setSaturation",value:function(a,b){return this._setHSL(1,a,void 0!==b&&b)}},{key:"setLightness",value:function(a,b){return this._setHSL(2,a,void 0!==b&&b)}},{key:"mix",value:function(a,b){if(b=void 0===b?50:b,b<=0)return this;if(b>=100)return this.reset(),this._setRGBArray(a.getRGBA()),this;null===this._rgb&&this._convertToRGB();var c=a.getRGBA(),d=b/100,e=1-d;return this._setRGBArray([this._rgb[0]*e+c[0]*d,this._rgb[1]*e+c[1]*d,this._rgb[2]*e+c[2]*d,this._alpha*e+c[3]*d])}},{key:"getRGB",value:function(a){return null===this._rgb&&this._convertToRGB(),a&&null===this._rgbRounded&&this._roundRGB(),a?this._rgbRounded.slice(0):this._rgb.slice(0)}},{key:"getRGBA",value:function(a){var b=this.getRGB(a);return b.push(this._alpha),b}},{key:"getHSL",value:function(a){return null===this._hsl&&this._convertToHSL(),a&&null===this._hslRounded&&this._roundHSL(),a?this._hslRounded.slice(0):this._hsl.slice(0)}},{key:"getHSLA",value:function(a){var b=this.getHSL(a);return b.push(this._alpha),b}},{key:"getAlpha",value:function(){return this._alpha}},{key:"getRed",value:function(a){return this._getRGB(0,a)}},{key:"getGreen",value:function(a){return this._getRGB(1,a)}},{key:"getBlue",value:function(a){return this._getRGB(2,a)}},{key:"getHue",value:function(a){return this._getHSL(0,a)}},{key:"getSaturation",value:function(a){return this._getHSL(1,a)}},{key:"getLightness",value:function(a){return this._getHSL(2,a)}},{key:"getLuminance",value:function(){if(null!==this._luminance)return this._luminance;var a=[];null===this._rgb&&this._convertToRGB();for(var b=0;b<3;b++){var c=this._rgb[b]/255;a[b]=c<.03928?c/12.92:Math.pow((c+.055)/1.055,2.4)}return this._luminance=.2126*a[0]+.7152*a[1]+.0722*a[2],this._luminance}},{key:"getContrast",value:function(a){var b=this.getLuminance()+.05,c=("number"==typeof a?a:a.getLuminance())+.05;return b>c?b/c:c/b}},{key:"reset",value:function(){return this._rgb=this._rgbRounded=this._hsl=this._hslRounded=this._luminance=null,this._alpha=1,this}},{key:"normalize",value:function(){return null!==this._rgb&&(this._rgb=[this._rgb[0]<0?0:this._rgb[0]>255?255:this._rgb[0],this._rgb[1]<0?0:this._rgb[1]>255?255:this._rgb[1],this._rgb[2]<0?0:this._rgb[2]>255?255:this._rgb[2]],null!==this._rgbRounded&&(this._rgbRounded=[this._rgbRounded[0]<0?0:this._rgbRounded[0]>255?255:this._rgbRounded[0],this._rgbRounded[1]<0?0:this._rgbRounded[1]>255?255:this._rgbRounded[1],this._rgbRounded[2]<0?0:this._rgbRounded[2]>255?255:this._rgbRounded[2]])),null!==this._hsl&&(this._hsl=[this._hsl[0]<0?this._hsl[0]%360+360:this._hsl[0]>=360?this._hsl[0]%360:this._hsl[0],this._hsl[1]<0?0:this._hsl[1]>100?100:this._hsl[1],this._hsl[2]<0?0:this._hsl[2]>100?100:this._hsl[2]],null!==this._hslRounded&&(this._hslRounded=[this._hslRounded[0]<0?this._hslRounded[0]%360+360:this._hslRounded[0]>=360?this._hslRounded[0]%360:this._hslRounded[0],this._hslRounded[1]<0?0:this._hslRounded[1]>100?100:this._hslRounded[1],this._hslRounded[2]<0?0:this._hslRounded[2]>100?100:this._hslRounded[2]])),this._alpha=this._alpha<0?0:this._alpha>1?1:this._alpha,this._luminance=null,this}},{key:"clone",value:function(){var a=new b;return null!==this._rgb?a.setRGBArray(this._rgb,this._rgbRounded):a.setHSLArray(this._hsl,this._hslRounded),this._alpha<1&&a.setAlpha(this._alpha),a}},{key:"toKeyword",value:function(b,c){var d=void 0,e=void 0,f=void 0,g=void 0,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0,m=void 0,n=void 0;if(b=void 0===b||b,c=void 0===c||c,0===this._alpha)return"transparent";e=c?a.all:a.base,null===this._rgb&&this._convertToRGB(),d=this._rgb,f=!1,g=b?1e3:1,h=b?256:1;for(j in e){for(i=e[j],k=0,m=0,n=0;n<3&&(l=Math.abs(i[n]-d[n]),k+=l,!(k>g));n++)m=Math.max(m,l);if(0===k)return j;b&&k<g?(f=j,g=k,h=m):b&&k===g&&m<h&&(f=j,h=m)}return f}},{key:"toHex",value:function(a){return this._getHexValue(void 0!==a&&a,!1)}},{key:"toIEHex",value:function(a){return this._getHexValue(void 0!==a&&a,!0)}},{key:"toRGBString",value:function(a,b,c){return this.toString({format:"rgb",compress:void 0!==a&&a,ignoreAlpha:void 0!==b&&b,alphaPrecision:void 0===c?2:c})}},{key:"toHSLString",value:function(a,b,c,d){return this.toString({format:"hsl",compress:void 0!==a&&a,ignoreAlpha:void 0!==b&&b,alphaPrecision:void 0===c?2:c,roundPrecision:void 0===d?2:d})}},{key:"toString",value:function(a){var c=void 0,d=void 0===a?{}:a,e=void 0===d.format?"auto":d.format,f=void 0!==d.ignoreAlpha&&d.ignoreAlpha,g=void 0===d.roundPrecision?2:d.roundPrecision,h=void 0===d.alphaPrecision?2:d.alphaPrecision,i=void 0!==d.compress&&d.compress,j=f?1:b._round(this._alpha,h),k=i?",":", ";switch(e){case"auto":if(null===this._rgb&&this._convertToRGB(),null===this._rgbRounded&&this._roundRGB(),g>0&&!b._equalColors(this._rgb,this._rgbRounded)){e="hsl";break}return 1===j?this._getHexValue(i,!1):(c="rgba("+this._rgbRounded[0]+k+this._rgbRounded[1]+k+this._rgbRounded[2]+k+j+")",i?b._compressString(c):c);case"rgb":case"rgba":return null===this._rgb&&this._convertToRGB(),null===this._rgbRounded&&this._roundRGB(),c=(1===j&&"rgba"!==e?"rgb(":"rgba(")+this._rgbRounded[0]+k+this._rgbRounded[1]+k+this._rgbRounded[2]+(1===j&&"rgba"!==e?"":k+j)+")",i?b._compressString(c):c;case"hex":return this._getHexValue(i,!1);case"iehex":return this._getHexValue(i,!0)}return null===this._hsl&&this._convertToHSL(),c=(1===j&&"hsla"!==e?"hsl(":"hsla(")+b._round(this._hsl[0],g)+k+b._round(this._hsl[1],g)+"%"+k+b._round(this._hsl[2],g)+"%"+(1===j&&"hsla"!==e?"":k+j)+")",i?b._compressString(c):c}},{key:"_setRGBArray",value:function(a,b){if(4===a.length)this._alpha=a.pop();else{if(3!==a.length)return null;this._alpha=1}return this._resetSpaces("rgb"),this._rgb=a,this._rgbRounded=b?a.slice(0):null,this}},{key:"_setHSLArray",value:function(a,b){if(4===a.length)this._alpha=a.pop();else{if(3!==a.length)return null;this._alpha=1}return this._resetSpaces("hsl"),this._hsl=a,this._hslRounded=b?a.slice(0):null,this}},{key:"_setRGB",value:function(a,b,c){return null===this._rgb&&this._convertToRGB(),this._resetSpaces("rgb"),this._rgb[a]=b,null!==this._rgbRounded&&(c||Math.round(b)===b?this._rgbRounded[a]=b:this._rgbRounded=null),this}},{key:"_setHSL",value:function(a,b,c){return null===this._hsl&&this._convertToHSL(),this._resetSpaces("hsl"),this._hsl[a]=b,null!==this._hslRounded&&(c||Math.round(b)===b?this._hslRounded[a]=b:this._hslRounded=null),this}},{key:"_getRGB",value:function(a,b){return null===this._rgb&&this._convertToRGB(),b&&null===this._rgbRounded&&this._roundRGB(),b?this._rgbRounded[a]:this._rgb[a]}},{key:"_getHSL",value:function(a,b){return null===this._hsl&&this._convertToHSL(),b&&null===this._hslRounded&&this._roundHSL(),b?this._hslRounded[a]:this._hsl[a]}},{key:"_roundRGB",value:function(){var a=Math.round(this._rgb[0]),b=Math.round(this._rgb[1]),c=Math.round(this._rgb[2]);this._rgbRounded=[a<0?0:a>255?255:a,b<0?0:b>255?255:b,c<0?0:c>255?255:c]}},{key:"_roundHSL",value:function(){var a=Math.round(this._hsl[0]),b=Math.round(this._hsl[1]),c=Math.round(this._hsl[2]);this._hslRounded=[a<0?a%360+360:a>=360?a%360:a,b<0?0:b>100?100:b,c<0?0:c>100?100:c]}},{key:"_convertToRGB",value:function(){this._rgb=null===this._hsl?[255,0,0]:b._hsl2rgb(this._hsl[0],this._hsl[1],this._hsl[2],!1),this._rgbRounded=null}},{key:"_convertToHSL",value:function(){this._hsl=null===this._rgb?[0,100,50]:b._rgb2hsl(this._rgb[0],this._rgb[1],this._rgb[2],!1),this._hslRounded=null}},{key:"_resetSpaces",value:function(a){a=void 0===a?"":a,"rgb"!==a&&(this._rgb=this._rgbRounded=null),"hsl"!==a&&(this._hsl=this._hslRounded=null),this._luminance=null}},{key:"_getHexValue",value:function(a,c){null===this._rgb&&this._convertToRGB(),null===this._rgbRounded&&this._roundRGB();var d="#";if(c){var e=Math.max(Math.min(Math.round(255*this._alpha),255),0);d+=(e<16?"0":"")+e.toString(16)}for(var f=0;f<3;f++)d+=(this._rgbRounded[f]<16?"0":"")+this._rgbRounded[f].toString(16);return a?b._compressString(d):d}}],[{key:"keywords",value:function(){return a}},{key:"fromKeyword",value:function(c,d){if(d=void 0===d||d,c=c.toLowerCase(),"transparent"===c)return(new b).setRGBA(0,0,0,0,!0);var e=d?a.all:a.base;return void 0===e[c]?null:(new b).setRGBArray(e[c],!0)}},{key:"fromHex",value:function(a){var c=void 0,d=void 0,e=void 0,f=!1,g=0;if("#"===a.slice(0,1)&&(a=a.slice(1)),!/^[\da-f]+$/i.test(a))return null;switch(a.length){case 4:f=a.slice(0,1),f+=f,g++;case 3:c=a.slice(g,++g),d=a.slice(g,++g),e=a.slice(g,++g),c+=c,d+=d,e+=e;break;case 8:f=a.slice(0,2),g+=2;case 6:c=a.slice(g++,++g),d=a.slice(g++,++g),e=a.slice(g++,++g);break;default:return null}return(new b).setRGBA(parseInt(c,16),parseInt(d,16),parseInt(e,16),f===!1?1:parseInt(f,16)/255,!0)}},{key:"fromString",value:function(a){var c=void 0,d=void 0,e=void 0,f=void 0,g=void 0,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0,m=void 0,n=void 0;if(a.indexOf("(")===-1)return g=b.fromHex(a),null===g?b.fromKeyword(a):g;if(a=a.toLowerCase().replace(/\s+/g,""),")"!==a.slice(-1))return null;if(a=a.slice(0,a.length-1),c=a.split("(",2),2!==c.length||/[^\d.,%-]/.test(c[1]))return null;if(d=c[0],e=c[1].split(","),f=1,"a"===d.slice(-1)){if(4!==e.length)return null;f=parseFloat(e.pop()),f=isNaN(f)?0:f<0?0:f>1?1:f}else if(3!==e.length)return null;switch(d){case"rgb":case"rgba":return"%"===e[0].slice(-1)?"%"!==e[1].slice(-1)||"%"!==e[2].slice(-1)?null:(h=parseFloat(e[0]),i=parseFloat(e[1]),j=parseFloat(e[2]),(new b).setRGBA(isNaN(h)||h<0?0:h>100?255:2.55*h,isNaN(i)||i<0?0:i>100?255:2.55*i,isNaN(j)||j<0?0:j>100?255:2.55*j,f)):c[1].indexOf("%")!==-1?null:(h=parseInt(e[0]),i=parseInt(e[1]),j=parseInt(e[2]),(new b).setRGBA(isNaN(h)||h<0?0:h>255?255:h,isNaN(i)||i<0?0:i>255?255:i,isNaN(j)||j<0?0:j>255?255:j,f,!0));case"hsl":case"hsla":return e[0].indexOf("%")!==-1||"%"!==e[1].slice(-1)||"%"!==e[2].slice(-1)?null:(k=parseFloat(e[0]),l=parseFloat(e[1]),m=parseFloat(e[2]),n=c[1].indexOf(".")===-1,(new b).setHSLA(isNaN(k)?0:k<0?k%360+360:k>=360?k%360:k,isNaN(l)||l<0?0:l>100?100:l,isNaN(m)||m<0?0:m>100?100:m,f,n))}return null}},{key:"_normalize",value:function(a,b){return b=void 0===b?100:b,a<0?0:a>b?b:a}},{key:"_normalizeHue",value:function(a){return a<0?a%360+360:a>=360?a%360:a}},{key:"_rgb2hsl",value:function(a,b,c,d){var e=a/255,f=b/255,g=c/255,h=Math.min(e,Math.min(f,g)),i=Math.max(e,Math.max(f,g)),j=(i+h)/2,k=void 0,l=void 0,m=void 0;return d=void 0!==d&&d,i===h?k=l=0:(k=j<.5?(i-h)/(i+h):(i-h)/(2-i-h),m=i-h,i===e&&(l=(f-g)/m),i===f&&(l=2+(g-e)/m),i===g&&(l=4+(e-f)/m),l*=60,l<0&&(l+=360)),d?[Math.round(l),Math.round(100*k),Math.round(100*j)]:[l,100*k,100*j]}},{key:"_hsl2rgb",value:function(a,b,c,d){function e(a,b,c){return c=c<0?c%360+360:c>=360?c%360:c,c>=240?a:c<60?a+(b-a)*c/60:c<180?b:a+(b-a)*(240-c)/60}var f=void 0,g=void 0,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0,m=void 0;return d=void 0!==d&&d,h=a<0?a%360+360:a>=360?a%360:a,g=b<0?0:b>100?1:b/100,f=c<0?0:c>100?1:c/100,j=f<=.5?f*(1+g):f+g*(1-f),i=2*f-j,0===g&&0===h?(k=f,l=f,m=f):(k=e(i,j,h+120),l=e(i,j,h),m=e(i,j,h-120)),[d?Math.round(255*k):255*k,d?Math.round(255*l):255*l,d?Math.round(255*m):255*m]}},{key:"_round",value:function(a,b){return b=Math.pow(10,b),Math.round(a*b)/b}},{key:"_equalColors",value:function(a,b){if(!(a instanceof Array&&b instanceof Array))return!1;for(var c=0;c<3;c++)if(a[c]!==b[c])return!1;return!0}},{key:"_compressString",value:function(a){if("#"===a.slice(0,1)){var b=a.length-1;if(6!==b&&8!==b)return a;for(var c="",d="",e=b/2,f=0;f<e;f++)if(c+=a.slice(2*f+1,2*f+2),d+=a.slice(2*f+2,2*f+3),c!==d)return a;return"#"+c}return a.replace(" ","").replace("(0.","(.").replace(",0.",",.")}}]),b}();b.Color=c}(),a.CAColor=b.Color}("undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope?self:"undefined"!=typeof global?global:Function("return this;")());