-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.min.js
1 lines (1 loc) · 10.2 KB
/
index.min.js
1
"use strict";function _defineProperties(o,r){for(var t=0;t<r.length;t++){var e=r[t];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(o,e.key,e)}}function _createClass(o,r,t){return r&&_defineProperties(o.prototype,r),t&&_defineProperties(o,t),o}var Color=function(){function Color(o){var r=void 0===o?{color:[0,0,0],type:"rgb",precision:3,capitalize:!0}:o,t=r.color,e=r.type,o=r.precision,o=void 0===o?3:o,r=r.capitalize,r=void 0===r||r;this.updateColor(void 0===t?[0,0,0]:t,void 0===e?"rgb":e),this.precision=o,this.capitalize=r}return Color.prototype.updateColor=function updateColor(o,r){var t;if("string"!=typeof(r=void 0===r?"rgb":r))throw new TypeError("Parameter 2 must be of type string.");if(r=r.toLowerCase(),!Color.validTypes.includes(r))throw new TypeError("Parameter 2 '"+r+"' is not a valid type.");switch(r){case"rgb":t=Color.rgbToXyz(o);break;case"hsl":t=Color.rgbToXyz(Color.hslToRgb(o));break;case"hex":t=Color.rgbToXyz(Color.hexToRgb(o));break;case"lab":t=Color.labToXyz(o);break;case"lchab":t=Color.labToXyz(Color.lchABToLab(o));break;case"luv":t=Color.luvToXyz(o);break;case"lchuv":t=Color.luvToXyz(Color.lchUVToLuv(o));break;case"xyz":case"default":this._xyz=o,this._rgb=Color.xyzToRgb(this._xyz),this._hsl=Color.rgbToHsl(this._rgb),this._hex=Color.rgbToHex(this._rgb),this._lab=Color.xyzToLab(this._xyz),this._lchab=Color.labToLCHab(this._lab),this._luv=Color.xyzToLuv(this._xyz),this._lchuv=Color.luvToLCHuv(this._luv)}"xyz"!==r&&this.updateColor(t,"xyz")},Color.rgbToHsl=function rgbToHsl(o){var r=o[0]/255,t=o[1]/255,e=o[2]/255,n=Math.max(r,t,e),a=Math.min(r,t,e),i=(n+a)/2,l=0,o=0;return n!==a&&(l=i<.5?(n-a)/(n+a):(n-a)/(2-n-a),o=r===n?(t-e)/(n-a):t===n?2+(e-r)/(n-a):4+(r-t)/(n-a)),[(o=(o*=60)<0?o%360+360:o)+0,(l*=100)+0,(i*=100)+0]},Color.hslToRgb=function hslToRgb(o){var r=o[0],t=o[1],e=o[2];isFinite(r)||(r=0),isFinite(t)||(t=0),isFinite(e)||(e=0),r=r<0?r%360+360:r,e/=100,t/=100,r/=60;var o=(1-Math.abs(2*e-1))*t,t=o*(1-Math.abs(r%2-1)),n=e-o/2,t=r<1?[o,t,0]:r<2?[t,o,0]:r<3?[0,o,t]:r<4?[0,t,o]:r<5?[t,0,o]:[o,0,t];return t.map(function(o){return Math.round(255*(o+n))+0})},Color.rgbToHex=function rgbToHex(o){var r=o[0],t=o[1],e=o[2],o=function hexChar(o){o=o.toString(16);return 1===o.length?"0"+o:o};return"#"+o(r)+o(t)+o(e)},Color.hexToRgb=function hexToRgb(o){o=o.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(o,r,t,e){return r+r+t+t+e+e});o=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(o);return o?[parseInt(o[1],16),parseInt(o[2],16),parseInt(o[3],16)]:null},Color.rgbToXyz=function rgbToXyz(o){var r=o[0]/255,t=o[1]/255,e=o[2]/255,o=function invCompand(o){return o<=.04045?o/12.92:Math.pow((o+.055)/1.055,2.4)},r=o(r),t=o(t),e=o(e);return[100*(.4124*r+.3576*t+.1805*e)+0,100*(.2126*r+.7152*t+.0722*e)+0,100*(.0193*r+.1192*t+.9505*e)+0]},Color.xyzToRgb=function xyzToRgb(o){var r=o[0]/100,t=o[1]/100,e=o[2]/100,n=-.9689307147293197*r+1.8757560608852415*t+.041517523842953964*e,a=.055710120445510616*r+-.2040210505984867*t+1.0569959422543882*e,o=function compand(o){return o<=.0031308?12.92*o:1.055*Math.pow(o,1/2.4)-.055},e=o(3.2406254773200533*r-1.5372079722103187*t-.4986285986982479*e),n=o(n),a=o(a);return[Math.round(255*e)+0,Math.round(255*n)+0,Math.round(255*a)+0]},Color.xyzToLab=function xyzToLab(o){var r=o[0]/Color.d65[0],t=o[1]/Color.d65[1],e=o[2]/Color.d65[2],n=null!=Math.cbrt?Math.cbrt:function(o){return Math.pow(o,1/3)},o=function fwdTrans(o){return 216/24389<o?n(o):(24389/27*o+16)/116},r=o(r),t=o(t);return[116*t-16,0+500*(r-t),0+200*(t-o(e))]},Color.labToXyz=function labToXyz(o){var r=o[0],t=o[1],e=216/24389,n=24389/27,a=(r+16)/116,o=a-o[2]/200,a=t/500+a,a=Math.pow(a,3)>e?Math.pow(a,3):(116*a-16)/n,r=8<r?Math.pow((r+16)/116,3):r/n,n=Math.pow(o,3)>e?Math.pow(o,3):(116*o-16)/n;return[a*Color.d65[0]+0,r*Color.d65[1]+0,n*Color.d65[2]+0]},Color.labToLCHab=function labToLCHab(o){var r=o[1],t=Math.abs(o[2])<Color.maxZeroTolerance?0:o[2],e=Math.sqrt(r*r+t*t),r=0<=Math.atan2(t,r)?Math.atan2(t,r)/Math.PI*180:Math.atan2(t,r)/Math.PI*180+360;return[o[0]+0,e+0,0+r]},Color.lchABToLab=function lchABToLab(o){var r=o[1],t=o[2],e=r*Math.cos(t/180*Math.PI),t=r*Math.sin(t/180*Math.PI);return[o[0]+0,0+e,0+t]},Color.xyzToLuv=function xyzToLuv(o){var r=o[0],t=o[1],e=o[2],n=Color.d65[0],a=Color.d65[1],i=Color.d65[2],o=9*a/(n+15*a+3*i),n=4*n/(n+15*a+3*i);if(0===r&&0===t&&0===e)return[0,0,0];i=9*t/(r+15*t+3*e),e=4*r/(r+15*t+3*e),t/=a,a=null!=Math.cbrt?Math.cbrt:function(o){return Math.pow(o,1/3)},t=216/24389<t?116*a(t,1/3)-16:24389/27*t;return[0+t,0+13*t*(e-n),0+13*t*(i-o)]},Color.luvToXyz=function luvToXyz(o){var r=o[0],t=o[1],e=o[2],n=Color.d65[0],a=Color.d65[1],i=Color.d65[2],o=9*a/(n+15*a+3*i),n=4*n/(n+15*a+3*i),a=8<r?Math.pow((r+16)/116,3):r/(24389/27),i=-5*a,n=(52*r/(t+13*r*n)-1)/3||0,o=((a*(39*r/(e+13*r*o)-5)||0)-i)/(n- -1/3);return[100*o+0,100*a+0,100*(o*n+i)+0]},Color.luvToLCHuv=function luvToLCHuv(o){var r=o[0],t=Math.abs(o[1])<Color.maxZeroTolerance?0:o[1],e=Math.abs(o[2])<Color.maxZeroTolerance?0:o[2],o=Math.sqrt(t*t+e*e),t=180*Math.atan2(e,t)/Math.PI;return[r+0,o+0,(t=0<=t?t:t+360)+0]},Color.lchUVToLuv=function lchUVToLuv(o){var r=o[0],t=o[1],o=o[2]/180*Math.PI;return[r+0,0+t*Math.cos(o),0+t*Math.sin(o)]},Color.luminance=function luminance(o,r){if("string"!=typeof(r=void 0===r?"rgb":r))throw new TypeError("Parameter 2 must be of type string.");if(r=r.toLowerCase(),!Color.validTypes.includes(r))throw new TypeError("Parameter 2 '"+r+"' is not a valid type.");"rgb"!==r&&(o=new Color({color:o,type:r}).rgb),o=[].concat(o);for(var t=0;t<o.length;t++)o[t]/=255,o[t]<.03928?o[t]/=12.92:o[t]=Math.pow((o[t]+.055)/1.055,2.4);return.2126*o[0]+.7152*o[1]+.0722*o[2]},Color.random=function random(){return new Color({color:[255,255,255].map(function(o){return Math.round(o*Math.random())})})},Color.randomFromString=function randomFromString(o){function mb(o){for(var r=4294967295,t=0;t<o.length;t++){var e=4026531840&(r=(r<<4)+o.charCodeAt(t));0!=e&&(r^=e>>>24,r^=e)}return Math.abs(r)}var r=mb(o=void 0===o?"":o),t=mb(o+r);return new Color({color:[r%256,t%256,mb(o+t)%256]})},Color.randomOfType=function randomOfType(o){if("string"!=typeof(o=void 0===o?"rgb":o))throw new TypeError("Parameter 1 must be of type string.");if(o=o.toLowerCase(),!Color.validTypes.includes(o))throw new TypeError("Parameter 1 '"+o+"' is not a valid type.");return Color.random()[o]},Color.randomOfTypeFormatted=function randomOfTypeFormatted(o,r,t){if(void 0===r&&(r=!0),void 0===t&&(t=3),"string"!=typeof(o=void 0===o?"rgb":o))throw new TypeError("Parameter 1 must be of type string.");if(o=o.toLowerCase(),!Color.validTypes.includes(o))throw new TypeError("Parameter 1 '"+o+"' is not a valid type.");var e=Color.random();return e.capitalize=r,e.precision=t,e[o+"String"]},Color.contrastTextColor=function contrastTextColor(o,r){if("string"!=typeof(r=void 0===r?"rgb":r))throw new TypeError("Parameter 2 must be of type string.");if(r=r.toLowerCase(),!Color.validTypes.includes(r))throw new TypeError("Parameter 2 '"+r+"' is not a valid type.");var t=Color.contrastRatio(new Color({color:[255,255,255]}),new Color({color:o,type:r}));return Color.contrastRatio(new Color({color:[0,0,0]}),new Color({color:o,type:r}))<t?"#FFFFFF":"#000000"},Color.contrastRatio=function contrastRatio(o,r){if(!(o instanceof Color))throw new TypeError("Parameter 1 must be of type Color.");if(!(r instanceof Color))throw new TypeError("Parameter 2 must be of type Color.");o=Color.luminance(o.rgb)+.05,r=Color.luminance(r.rgb)+.05;return o<r?r/o:o/r},Color.blend=function blend(o,r,t,e){if(void 0===t&&(t="rgb"),void 0===e&&(e=.5),!(o instanceof Color))throw new TypeError("Parameter 1 must be of type Color.");if(!(r instanceof Color))throw new TypeError("Parameter 2 must be of type Color.");if("string"!=typeof t)throw new TypeError("Parameter 3 must be of type string.");if(t=t.toLowerCase(),!Color.validTypes.includes(t))throw new TypeError("Parameter 3 '"+t+"' is not a valid type.");var n=o[t="hex"===t?"rgb":t],a=r[t];return new Color({color:new Array(3).fill().map(function(o,r){return n[r]*e+a[r]*(1-e)}),type:t})},_createClass(Color,[{key:"rgb",get:function get(){return this._rgb},set:function set(o){this.updateColor(o,"rgb")}},{key:"rgbString",get:function get(){var o="RGB("+this.rgb.join(", ")+")";return this.capitalize?o.toUpperCase():o.toLowerCase()}},{key:"hsl",get:function get(){return this._hsl},set:function set(o){this.updateColor(o,"hsl")}},{key:"hslString",get:function get(){var r=this,o="HSL("+this.hsl.map(function(o){return o.toFixed(r.precision)}).join(", ")+")";return this.capitalize?o.toUpperCase():o.toLowerCase()}},{key:"hex",get:function get(){return this._hex},set:function set(o){this.updateColor(o,"hex")}},{key:"hexString",get:function get(){var o=this._hex;return this.capitalize?o.toUpperCase():o.toLowerCase()}},{key:"xyz",get:function get(){return this._xyz},set:function set(o){this.updateColor(o,"xyz")}},{key:"xyzString",get:function get(){var r=this,o="XYZ("+this.xyz.map(function(o){return o.toFixed(r.precision)}).join(", ")+")";return this.capitalize?o.toUpperCase():o.toLowerCase()}},{key:"lab",get:function get(){return this._lab},set:function set(o){this.updateColor(o,"lab")}},{key:"labString",get:function get(){var r=this,o="LAB("+this.lab.map(function(o){return o.toFixed(r.precision)}).join(", ")+")";return this.capitalize?o.toUpperCase():o.toLowerCase()}},{key:"lchab",get:function get(){return this._lchab},set:function set(o){this.updateColor(o,"lchab")}},{key:"lchabString",get:function get(){var r=this,o=this.lchab.map(function(o){return o.toFixed(r.precision)});return this.capitalize?"LCHab("+o.join(", ")+")":"lchAB("+o.join(", ")+")"}},{key:"luv",get:function get(){return this._luv},set:function set(o){this.updateColor(o,"luv")}},{key:"luvString",get:function get(){var r=this,o="LUV("+this.luv.map(function(o){return o.toFixed(r.precision)}).join(", ")+")";return this.capitalize?o.toUpperCase():o.toLowerCase()}},{key:"lchuv",get:function get(){return this._lchuv},set:function set(o){this.updateColor(o,"lchuv")}},{key:"lchuvString",get:function get(){var r=this,o=this.lchuv.map(function(o){return o.toFixed(r.precision)});return this.capitalize?"LCHuv("+o.join(", ")+")":"lchUV("+o.join(", ")+")"}}]),Color}();Color.validTypes=["rgb","hex","hsl","xyz","lab","lchab","luv","lchuv"],Color.d65=[95.05,100,108.9],Color.maxZeroTolerance=Math.pow(10,-12),module.exports=Color;