colorjs.io
Version:
Color space agnostic color manipulation library
3 lines (2 loc) • 28.9 kB
JavaScript
var Color=function(t){"use strict";function e(t,e){let r=t.length;Array.isArray(t[0])||(t=[t]),Array.isArray(e[0])||(e=e.map(t=>[t]));let a=e[0].length,s=e[0].map((t,r)=>e.map(t=>t[r])),o=t.map(t=>s.map(e=>Array.isArray(t)?t.reduce((t,r,a)=>t+r*(e[a]||0),0):e.reduce((e,r)=>e+r*t,0)));return 1===r&&(o=o[0]),1===a?o.map(t=>t[0]):o}function r(t){return"string"===a(t)}function a(t){return(Object.prototype.toString.call(t).match(/^\[object\s+(.*?)\]$/)[1]||"").toLowerCase()}function s(t,...e){for(let r of e)if(r){let e=Object.getOwnPropertyDescriptors(r);Object.defineProperties(t,e)}return t}function o(t,e,r){let a=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,a)}function i(t,e,r=[0,1]){e=+e;let a=((r[1]||r[0]||1)+"").length,s=Math.max(0,e+1-a);return+t.toFixed(s)}function n(t){if(t.indexOf(".")>0){let[e,r]=t.split("."),a=Color.space(e);if(!(r in a.coords))throw new ReferenceError(`Color space "${a.name}" has no "${r}" coordinate.`);return[a,r]}}function c(t,e,r){let a=e.split("."),s=a.pop();if(t=a.reduceRight((t,e)=>t&&t[e],t))return void 0===r?t[s]:t[s]=r}var h=Object.freeze({__proto__:null,isString:r,type:a,extend:s,copyDescriptor:o,capitalize:function(t){return t?t[0].toUpperCase()+t.slice(1):t},toPrecision:i,parseCoord:n,value:c,multiplyMatrices:e});const l="undefined"!=typeof document;class p{constructor(...t){let e,a;if(t[0]&&"object"==typeof t[0]&&(t[0].space||t[0].spaceId)&&t[0].coords)a=t[0];else if(r(t[0])){if(l&&0===t[0].indexOf("--")){let t=arguments[1]&&1===arguments[1].nodeType?arguments[1]:document.documentElement;e=getComputedStyle(t).getPropertyValue(arguments[0])}else 1===t.length&&(e=t[0]);e&&(a=p.parse(e))}if(a)"spaceId"in a?this.spaceId=a.spaceId:this.space=a.space,this.coords=a.coords.slice(),this.alpha=a.alpha;else{let e,r,a;Array.isArray(t[0])?[e,r,a]=["sRGB",...t]:[e,r,a]=t,this.spaceId=e||"sRGB",this.coords=r?r.slice():[0,0,0],this.alpha=a}this.alpha=this.alpha<1?this.alpha:1;for(let t=0;t<this.coords.length;t++)"NaN"===this.coords[t]&&(this.coords[t]=NaN)}get space(){return p.spaces[this.spaceId]}set space(t){return this.spaceId=t}get spaceId(){return this._spaceId}set spaceId(t){let e=p.space(t);if(t=e.id,this.space&&e&&this.space!==e){this.coords=this[t];for(let t in this.space.instance)this.hasOwnProperty(t)&&delete this[t]}this._spaceId=t,s(this,this.space.instance)}get white(){return this.space.white||p.whites.D50}set(t,e){if(1===arguments.length&&"object"===a(arguments[0])){let t=arguments[0];for(let e in t)this.set(e,t[e])}else if("function"==typeof e){let r=c(this,t);c(this,t,e.call(this,r))}else c(this,t,e);return this}lighten(t=.25){let e=new p(this),r=e.lightness;return e.lightness=r*(1+t),e}darken(t=.25){let e=new p(this),r=e.lightness;return e.lightness=r*(1-t),e}distance(t,e="lab"){t=p.get(t);let r=this[(e=p.space(e)).id],a=t[e.id];return Math.sqrt(r.reduce((t,e,r)=>isNaN(e)||isNaN(a[r])?t:t+(a[r]-e)**2,0))}deltaE(t,e={}){r(e)&&(e={method:e});let{method:a=p.defaults.deltaE,...s}=e;return t=p.get(t),this["deltaE"+a]?this["deltaE"+a](t,s):this.deltaE76(t)}deltaE76(t){return this.distance(t,"lab")}get luminance(){return this.xyz.Y}set luminance(t){this.xyz.Y=t}contrast(t){t=p.get(t);let e=this.luminance,r=t.luminance;return r>e&&([e,r]=[r,e]),(e+.05)/(r+.05)}get uv(){let[t,e,r]=this.xyz,a=t+15*e+3*r;return[4*t/a,9*e/a]}get xy(){let[t,e,r]=this.xyz,a=t+e+r;return[t/a,e/a]}getCoords({inGamut:t,precision:e=p.defaults.precision}={}){let r=this.coords;if(t&&!this.inGamut()&&(r=this.toGamut(!0===t?void 0:t).coords),null!=e){let t=this.space.coords?Object.values(this.space.coords):[];r=r.map((r,a)=>i(r,e,t[a]))}return r}inGamut(t=this.space,e){return t=p.space(t),p.inGamut(t,this[t.id],e)}static inGamut(t,e,{epsilon:r=75e-6}={}){if((t=p.space(t)).inGamut)return t.inGamut(e);{if(!t.coords)return!0;let a=Object.values(t.coords);return e.every((t,e)=>{if(Number.isNaN(t))return!0;let[s,o]=a[e];return(void 0===s||t>=s-r)&&(void 0===o||t<=o+r)})}}toGamut({method:t=p.defaults.gamutMapping,space:e=this.space,inPlace:a}={}){if(r(arguments[0])&&(e=arguments[0]),e=p.space(e),this.inGamut(e,{epsilon:0}))return this;let s=this.to(e);if(t.indexOf(".")>0&&!this.inGamut(e)){let r=s.toGamut({method:"clip",space:e}),a=this.deltaE(r,{method:"2000"});if(this.deltaE(r,{method:"2000"})>2.3){let[r,o]=n(t),i=s.to(r),c=.001,h=r.coords[o][0],l=i[o],p=s.deltaE(i,{method:"2000"});for(;l-h>c&&p<a;){let t=i.toGamut({space:e,method:"clip"}),r=i.deltaE(t,{method:"2000"});if(p=s.deltaE(i,{method:"2000"}),r-2<c)h=i[o];else{if(Math.abs(r-2)<c)break;l=i[o]}i[o]=(l+h)/2}s=i.to(e)}else s=r}if("clip"===t||!s.inGamut(e,{epsilon:0})){let t=Object.values(e.coords);s.coords=s.coords.map((e,r)=>{let[a,s]=t[r];return void 0!==a&&(e=Math.max(a,e)),void 0!==s&&(e=Math.min(e,s)),e})}return e.id!==this.spaceId&&(s=s.to(this.space)),a?(this.coords=s.coords,this):s}clone(){return new p(this.spaceId,this.coords,this.alpha)}to(t,{inGamut:e}={}){let r=(t=p.space(t)).id,a=new p(r,this[r],this.alpha);return e&&a.toGamut({inPlace:!0}),a}toJSON(){return{spaceId:this.spaceId,coords:this.coords,alpha:this.alpha}}toString({precision:t=p.defaults.precision,format:e,commas:a,inGamut:s,name:o="color",fallback:i}={}){let n=this.alpha<1?` ${a?",":"/"} ${this.alpha}`:"",c=this.getCoords({inGamut:s,precision:t});if(c=c.map(t=>t||0),r(e)&&"%"===e){let r=t;(!Number.isInteger(t)||t>21)&&(r=21),e=t=>t.toLocaleString("en-US",{style:"percent",maximumSignificantDigits:r})}"function"==typeof e&&(c=c.map(e));let h=[...c];"color"===o&&h.unshift(this.space?this.space.cssId||this.space.id:"XYZ");let u=`${o}(${h.join(a?", ":" ")}${n})`;if(i){if(!l||!self.CSS||CSS.supports("color",u))return u=new String(u),u.color=this,u;let e=Array.isArray(i)?i.slice():p.defaults.fallbackSpaces;for(let r,a=0;r=e[a];a++)if(p.spaces[r]){let s=this.to(r);if(u=s.toString({precision:t}),CSS.supports("color",u))return u=new String(u),u.color=s,u;e===p.defaults.fallbackSpaces&&(e.splice(a,1),a--)}let r=this.to("srgb");u=new String(r.toString({commas:!0})),u.color=r}return u}equals(t){return t=p.get(t),this.spaceId===t.spaceId&&this.alpha===t.alpha&&this.coords.every((e,r)=>e===t.coords[r])}static chromaticAdaptation(t,r,a,s={}){if((t=t||p.whites.D50)===(r=r||p.whites.D50))return a;let o={W1:t,W2:r,XYZ:a,options:s};if(p.hooks.run("chromatic-adaptation-start",o),o.M||(o.W1===p.whites.D65&&o.W2===p.whites.D50?o.M=[[1.0478112,.0228866,-.050127],[.0295424,.9904844,-.0170491],[-.0092345,.0150436,.7521316]]:o.W1===p.whites.D50&&o.W2===p.whites.D65&&(o.M=[[.9555766,-.0230393,.0631636],[-.0282895,1.0099416,.0210077],[.0122982,-.020483,1.3299098]])),p.hooks.run("chromatic-adaptation-end",o),o.M)return e(o.M,o.XYZ);throw new TypeError("Only Bradford CAT with white points D50 and D65 supported for now.")}static parse(t){let e={str:t};if(p.hooks.run("parse-start",e),e.color)return e.color;if(e.parsed=p.parseFunction(e.str),p.hooks.run("parse-function-start",e),e.color)return e.color;for(let t of Object.values(p.spaces))if(t.parse){let r=t.parse(e.str,e.parsed);if(r)return r}let r=e.parsed&&e.parsed.name;if(!/^color|^rgb/.test(r)&&l&&document.head){let a=document.head.style.color;if(document.head.style.color="",document.head.style.color=t,document.head.style.color!==a){let s=getComputedStyle(document.head).color;document.head.style.color=a,s&&(t=s,e.parsed=p.parseFunction(s),r=e.parsed.name)}}if(e.parsed){if("rgb"===r||"rgba"===r){let t=e.parsed.args.map((t,e)=>e<3&&!t.percentage?t/255:+t);return{spaceId:"srgb",coords:t.slice(0,3),alpha:t[3]}}if("color"===r){let t=e.parsed.args.shift().toLowerCase(),r=Object.values(p.spaces).find(e=>(e.cssId||e.id)===t);if(r){let t=Object.keys(r.coords).length,a=e.parsed.rawArgs.indexOf("/")>0?e.parsed.args.pop():1,s=Array(t).fill(0);return s.forEach((t,r)=>s[r]=e.parsed.args[r]||0),{spaceId:r.id,coords:s,alpha:a}}throw new TypeError(`Color space ${t} not found. Missing a plugin?`)}}throw new TypeError(`Could not parse ${t} as a color. Missing a plugin?`)}static parseFunction(t){if(!t)return;t=t.trim();const e=/^-?[\d.]+$/;let r=t.match(/^([a-z]+)\((.+?)\)$/i);if(r){let t=r[2].match(/([-\w.]+(?:%|deg)?)/g);return t=t.map(t=>{if(/%$/.test(t)){let e=new Number(+t.slice(0,-1)/100);return e.percentage=!0,e}if(/deg$/.test(t)){let e=new Number(+t.slice(0,-3));return e.deg=!0,e}return e.test(t)?+t:t}),{name:r[1].toLowerCase(),rawName:r[1],rawArgs:r[2],args:t}}}static convert(t,e,r){if((e=p.space(e))===(r=p.space(r)))return t;t=t.map(t=>Number.isNaN(t)?0:t);let a=e.id,s=r.id;if(r.from&&r.from[a])return r.from[a](t);if(e.to&&e.to[s])return e.to[s](t);let o=e.toXYZ(t);return r.white!==e.white&&(o=p.chromaticAdaptation(e.white,r.white,o)),r.fromXYZ(o)}static get(t,...e){return t instanceof p?t:new p(t,...e)}static space(t){let e=a(t);if("string"===e){let e=p.spaces[t.toLowerCase()];if(!e)throw new TypeError(`No color space found with id = "${t}"`);return e}if(t&&"object"===e)return t;throw new TypeError(t+" is not a valid color space")}static defineSpace({id:t,inherits:e}){let r=p.spaces[t]=arguments[0];if(e){const t=["id","parse","instance","properties"];let a=p.spaces[e];for(let e in a)t.includes(e)||e in r||o(r,a,e)}let a=r.coords;if(r.properties&&s(p.prototype,r.properties),!r.fromXYZ&&!r.toXYZ){let t;if(r.from&&r.to){let e=new Set(Object.keys(r.from)),a=new Set(Object.keys(r.to)),s=[...e].filter(t=>{if(a.has(t)){let e=p.spaces[t];return e&&e.fromXYZ&&e.toXYZ}});s.length>0&&(t=p.spaces[s[0]])}if(!t)throw new ReferenceError(`No connection space found for ${r.name}.`);Object.assign(r,{fromXYZ(e){let r=t.fromXYZ(e);return this.from[t.id](r)},toXYZ(e){let r=this.to[t.id](e);return t.toXYZ(r)}})}let i=Object.keys(a);return Object.defineProperty(p.prototype,t,{get(){let e=p.convert(this.coords,this.spaceId,t);return self.Proxy?new Proxy(e,{has:(t,e)=>i.includes(e)||Reflect.has(t,e),get:(t,e,r)=>{let a=i.indexOf(e);return a>-1?t[a]:Reflect.get(t,e,r)},set:(e,r,a,s)=>{let o=i.indexOf(r);return r>-1&&(o=r),o>-1?(e[o]=a,this.coords=p.convert(e,t,this.spaceId),!0):Reflect.set(e,r,a,s)}}):e},set(e){this.coords=p.convert(e,t,this.spaceId)},configurable:!0,enumerable:!0}),r}static defineShortcut(t,e=p.prototype,r){r&&(p.shortcuts[t]=r),Object.defineProperty(e,t,{get(){return c(this,p.shortcuts[t])},set(e){return c(this,p.shortcuts[t],e)},configurable:!0,enumerable:!0})}static statify(t=[]){t=t||Object.getOwnPropertyNames(p.prototype);for(let t of Object.getOwnPropertyNames(p.prototype)){let e=Object.getOwnPropertyDescriptor(p.prototype,t);e.get||e.set||"function"!=typeof e.value||t in p||(p[t]=function(e,...r){return(e=p.get(e))[t](...r)})}}}Object.assign(p,{util:h,hooks:new class{add(t,e,r){if("string"==typeof arguments[0])(Array.isArray(t)?t:[t]).forEach((function(t){this[t]=this[t]||[],e&&this[t][r?"unshift":"push"](e)}),this);else for(var t in arguments[0])this.add(t,arguments[0][t],arguments[1])}run(t,e){this[t]=this[t]||[],this[t].forEach((function(t){t.call(e&&e.context?e.context:e,e)}))}},whites:{D50:[.96422,1,.82521],D65:[.95047,1,1.08883]},spaces:{},shortcuts:{lightness:"lch.lightness",chroma:"lch.chroma",hue:"lch.hue"},defaults:{gamutMapping:"lch.chroma",precision:5,deltaE:"76",fallbackSpaces:["p3","srgb"]}}),p.defineSpace({id:"xyz",name:"XYZ",coords:{X:[],Y:[],Z:[]},white:p.whites.D50,inGamut:t=>!0,toXYZ:t=>t,fromXYZ:t=>t});for(let t in p.shortcuts)p.defineShortcut(t);p.statify(),p.defineSpace({id:"lab",name:"Lab",coords:{L:[0,100],a:[-100,100],b:[-100,100]},inGamut:t=>!0,white:p.whites.D50,"ε":216/24389,"κ":24389/27,fromXYZ(t){const{"κ":e,"ε":r,white:a}=this;let s=t.map((t,e)=>t/a[e]).map(t=>t>r?Math.cbrt(t):(e*t+16)/116);return[116*s[1]-16,500*(s[0]-s[1]),200*(s[1]-s[2])]},toXYZ(t){const{"κ":e,"ε":r,white:a}=this;let s=[];return s[1]=(t[0]+16)/116,s[0]=t[1]/500+s[1],s[2]=s[1]-t[2]/200,[Math.pow(s[0],3)>r?Math.pow(s[0],3):(116*s[0]-16)/e,t[0]>e*r?Math.pow((t[0]+16)/116,3):t[0]/e,Math.pow(s[2],3)>r?Math.pow(s[2],3):(116*s[2]-16)/e].map((t,e)=>t*a[e])},parse(t,e=p.parseFunction(t)){if(e&&"lab"===e.name){let t=e.args[0];return t.percentage&&(e.args[0]=100*t),{spaceId:"lab",coords:e.args.slice(0,3),alpha:e.args.slice(3)[0]}}},instance:{toString({format:t,...e}={}){return t||(t=(t,e)=>0===e?t+"%":t),p.prototype.toString.call(this,{name:"lab",format:t,...e})}}});const u=[0,360];function d(t){return(t%360+360)%360}u.isAngle=!0,p.defineSpace({id:"lch",name:"LCH",coords:{lightness:[0,100],chroma:[0,150],hue:u},inGamut:t=>!0,white:p.whites.D50,from:{lab(t){let e,[r,a,s]=t;const o=5e-4;return e=Math.abs(a)<o&&Math.abs(s)<o?NaN:180*Math.atan2(s,a)/Math.PI,[r,Math.sqrt(a**2+s**2),d(e)]}},to:{lab(t){let[e,r,a]=t;return r<0&&(r=0),isNaN(a)&&(a=0),[e,r*Math.cos(a*Math.PI/180),r*Math.sin(a*Math.PI/180)]}},parse(t,e=p.parseFunction(t)){if(e&&"lch"===e.name){let t=e.args[0];return t.percentage&&(e.args[0]=100*t),{spaceId:"lch",coords:e.args.slice(0,3),alpha:e.args.slice(3)[0]}}},instance:{toString({format:t,...e}={}){return t||(t=(t,e)=>0===e?t+"%":t),p.prototype.toString.call(this,{name:"lch",format:t,...e})}}}),p.defineSpace({id:"srgb",name:"sRGB",coords:{red:[0,1],green:[0,1],blue:[0,1]},white:p.whites.D65,toLinear:t=>t.map((function(t){return t<.04045?t/12.92:Math.pow((t+.055)/1.055,2.4)})),toGamma:t=>t.map((function(t){return t>.0031308?1.055*Math.pow(t,1/2.4)-.055:12.92*t})),toXYZ_M:[[.4124564,.3575761,.1804375],[.2126729,.7151522,.072175],[.0193339,.119192,.9503041]],fromXYZ_M:[[3.2404542,-1.5371385,-.4985314],[-.969266,1.8760108,.041556],[.0556434,-.2040259,1.0572252]],toXYZ(t){return t=this.toLinear(t),e(this.toXYZ_M,t)},fromXYZ(t){return this.toGamma(e(this.fromXYZ_M,t))},properties:{toHex({alpha:t=!0,collapse:e=!0}={}){let r=this.to("srgb",{inGamut:!0}).coords;this.alpha<1&&t&&r.push(this.alpha),r=r.map(t=>Math.round(255*t));let a=e&&r.every(t=>t%17==0);return"#"+r.map(t=>a?(t/17).toString(16):t.toString(16).padStart(2,"0")).join("")},get hex(){return this.toHex()}},instance:{toString({inGamut:t=!0,commas:e,format:r="%",...a}={}){if(255===r)r=t=>255*t;else if("hex"===r)return this.toHex(arguments[0]);return p.prototype.toString.call(this,{inGamut:t,commas:e,format:r,name:"rgb"+(e&&this.alpha<1?"a":""),...a})}},parseHex(t){t.length<=5&&(t=t.replace(/[a-f0-9]/gi,"$&$&"));let e=[];return t.replace(/[a-f0-9]{2}/gi,t=>{e.push(parseInt(t,16)/255)}),{spaceId:"srgb",coords:e.slice(0,3),alpha:e.slice(3)[0]}}}),p.hooks.add("parse-start",t=>{let e=t.str;/^#([a-f0-9]{3,4}){1,2}$/i.test(e)&&(t.color=p.spaces.srgb.parseHex(e))}),p.defineSpace({id:"hsl",name:"HSL",coords:{hue:u,saturation:[0,100],lightness:[0,100]},inGamut(t){let e=this.to.srgb(t);return p.inGamut("srgb",e)},white:p.whites.D65,from:{srgb(t){t=t.map(t=>100*t);let e=Math.max.apply(Math,t),r=Math.min.apply(Math,t),[a,s,o]=t,[i,n,c]=[NaN,0,(r+e)/2],h=e-r;if(0!==h){switch(n=100*h/(100-Math.abs(2*c-100)),e){case a:i=(s-o)/h+(s<o?6:0);break;case s:i=(o-a)/h+2;break;case o:i=(a-s)/h+4}i*=60}return[i,n,c]}},to:{srgb(t){let[e,r,a]=t;function s(t){let s=(t+e/30)%12,o=r*Math.min(a,1-a);return a-o*Math.max(-1,Math.min(s-3,9-s,1))}return e%=360,e<0&&(e+=360),r/=100,a/=100,[s(0),s(8),s(4)]}},parse(t,e=p.parseFunction(t)){if(e&&/^hsla?$/.test(e.name)){let t=e.args;return t[1]*=100,t[2]*=100,{spaceId:"hsl",coords:t.slice(0,3),alpha:t[3]}}},instance:{toString({precision:t,commas:e,format:r,inGamut:a,...s}={}){return r||(r=(t,e)=>e>0?t+"%":t),p.prototype.toString.call(this,{inGamut:!0,commas:e,format:r,name:"hsl"+(e&&this.alpha<1?"a":""),...s})}}}),p.defineSpace({id:"hwb",name:"HWB",coords:{hue:u,whiteness:[0,100],blackness:[0,100]},inGamut(t){let e=this.to.srgb(t);return p.inGamut("srgb",e)},white:p.whites.D65,from:{srgb(t){let e=p.spaces.hsl.from.srgb(t)[0],r=Math.min(...t),a=1-Math.max(...t);return r*=100,a*=100,[e,r,a]},hsv(t){let[e,r,a]=t;return[e,a*(100-r)/100,100-a]},hsl(t){let e=p.spaces.hsv.from.hsl(t);return this.hsv(e)}},to:{srgb(t){let[e,r,a]=t;r/=100,a/=100;let s=r+a;if(s>=1){let t=r/s;return[t,t,t]}let o=p.spaces.hsl.to.srgb([e,100,50]);for(var i=0;i<3;i++)o[i]*=1-r-a,o[i]+=r;return o},hsv(t){let[e,r,a]=t;r/=100,a/=100;let s=r+a;return s>=1?[e,0,r/s]:[e,100-100*r/(100-a),100*(1-a)]},hsl(t){let e=p.spaces.hwb.to.hsv(t);return p.spaces.hsv.to.hsl(e)}},parse(t,e=p.parseFunction(t)){if(e&&/^hwba?$/.test(e.name)){let t=e.args;return t[1]*=100,t[2]*=100,{spaceId:"hwb",coords:t.slice(0,3),alpha:t[3]}}},instance:{toString({format:t,commas:e,inGamut:r,...a}={}){return t||(t=(t,e)=>e>0?t+"%":t),p.prototype.toString.call(this,{inGamut:!0,commas:!1,format:t,name:"hwb",...a})}}}),p.defineSpace({id:"hsv",name:"HSV",coords:{hue:u,saturation:[0,100],value:[0,100]},inGamut(t){let e=this.to.hsl(t);return p.spaces.hsl.inGamut(e)},white:p.whites.D65,from:{hsl(t){let[e,r,a]=t;r/=100,a/=100;let s=a+r*Math.min(a,1-a);return[e,0===s?0:200*(1-a/s),100*s]}},to:{hsl(t){let[e,r,a]=t;r/=100,a/=100;let s=100*a*(1-r/2);return[e,0===s||1===s?0:(a-s)/Math.min(s,1-s),s]}}}),p.defineSpace({inherits:"srgb",id:"p3",name:"P3",cssId:"display-p3",toXYZ_M:[[.4865709486482162,.26566769316909306,.1982172852343625],[.2289745640697488,.6917385218365064,.079286914093745],[0,.04511338185890264,1.043944368900976]],fromXYZ_M:[[2.493496911941425,-.9313836179191239,-.40271078445071684],[-.8294889695615747,1.7626640603183463,.023624685841943577],[.03584583024378447,-.07617238926804182,.9568845240076872]]}),p.defineSpace({inherits:"srgb",id:"a98rgb",name:"Adobe 98 RGB compatible",cssId:"a98-rgb",toLinear:t=>t.map(t=>Math.pow(Math.abs(t),563/256)*Math.sign(t)),toGamma:t=>t.map(t=>Math.pow(Math.abs(t),256/563)*Math.sign(t)),toXYZ_M:[[.5766690429101305,.1855582379065463,.1882286462349947],[.29734497525053605,.6273635662554661,.07529145849399788],[.02703136138641234,.07068885253582723,.9913375368376388]],fromXYZ_M:[[2.0415879038107465,-.5650069742788596,-.34473135077832956],[-.9692436362808795,1.8759675015077202,.04155505740717557],[.013444280632031142,-.11836239223101838,1.0151749943912054]]}),p.defineSpace({inherits:"srgb",id:"prophoto",name:"ProPhoto",cssId:"prophoto-rgb",white:p.whites.D50,toLinear:t=>t.map((function(t){return t<=16/512?t/16:Math.pow(t,1.8)})),toGamma:t=>t.map((function(t){return t>=1/512?Math.pow(t,1/1.8):16*t})),toXYZ_M:[[.7977604896723027,.13518583717574031,.0313493495815248],[.2880711282292934,.7118432178101014,8565396060525902e-20],[0,0,.8251046025104601]],fromXYZ_M:[[1.3457989731028281,-.25558010007997534,-.05110628506753401],[-.5446224939028347,1.5082327413132781,.02053603239147973],[0,0,1.2119675456389454]]}),p.defineSpace({inherits:"srgb",id:"rec2020",name:"REC.2020","α":1.09929682680944,"β":.018053968510807,toLinear(t){const{"α":e,"β":r}=this;return t.map((function(t){return t<4.5*r?t/4.5:Math.pow((t+e-1)/e,2.4)}))},toGamma(t){const{"α":e,"β":r}=this;return t.map((function(t){return t>r?e*Math.pow(t,1/2.4)-(e-1):4.5*t}))},toXYZ_M:[[.6369580483012914,.14461690358620832,.1688809751641721],[.2627002120112671,.6779980715188708,.05930171646986196],[0,.028072693049087428,1.060985057710791]],fromXYZ_M:[[1.716651187971268,-.355670783776392,-.25336628137366],[-.666684351832489,1.616481236634939,.0157685458139111],[.017639857445311,-.042770613257809,.942103121235474]]}),p.defineSpace({id:"absxyzd65",name:"Absolute XYZ D65",coords:{Xa:[0,9504.7],Ya:[0,1e4],Za:[0,10888.3]},white:p.whites.D65,Yw:203,inGamut:t=>!0,fromXYZ(t){const e=p.whites.D50,r=p.whites.D65;t=p.chromaticAdaptation(e,r,t);const{Yw:a}=this;return t.map((function(t){return Math.max(t*a,0)}))},toXYZ(t){const{Yw:e}=this;let r=t.map((function(t){return Math.max(t/e,0)}));const a=p.whites.D65,s=p.whites.D50;return p.chromaticAdaptation(a,s,r)}}),p.defineSpace({id:"jzazbz",cssid:"Jzazbz",name:"Jzazbz",coords:{Jz:[0,1],az:[-.5,.5],bz:[-.5,.5]},inGamut:t=>!0,white:p.whites.D65,b:1.15,g:.66,n:2610/16384,ninv:16384/2610,c1:.8359375,c2:2413/128,c3:18.6875,p:1.7*2523/32,pinv:32/(1.7*2523),d:-.56,d0:16295499532821565e-27,XYZtoCone_M:[[.41478972,.579999,.014648],[-.20151,1.120649,.0531008],[-.0166008,.2648,.6684799]],ConetoXYZ_M:[[1.9242264357876067,-1.0047923125953657,.037651404030618],[.35031676209499907,.7264811939316552,-.06538442294808501],[-.09098281098284752,-.3127282905230739,1.5227665613052603]],ConetoIab_M:[[.5,.5,0],[3.524,-4.066708,.542708],[.199076,1.096799,-1.295875]],IabtoCone_M:[[1,.1386050432715393,.05804731615611886],[.9999999999999999,-.1386050432715393,-.05804731615611886],[.9999999999999998,-.09601924202631895,-.8118918960560388]],fromXYZ(t){const{b:r,g:a,n:s,p:o,c1:i,c2:n,c3:c,d:h,d0:l,XYZtoCone_M:u,ConetoIab_M:d}=this;let[m,f,g]=p.spaces.absxyzd65.fromXYZ(t),M=e(u,[r*m-(r-1)*g,a*f-(a-1)*m,g]).map((function(t){return((i+n*(t/1e4)**s)/(1+c*(t/1e4)**s))**o})),[b,w,y]=e(d,M);return[(1+h)*b/(1+h*b)-l,w,y]},toXYZ(t){const{b:r,g:a,ninv:s,pinv:o,c1:i,c2:n,c3:c,d:h,d0:l,ConetoXYZ_M:u,IabtoCone_M:d}=this;let[m,f,g]=t,M=e(d,[(m+l)/(1+h-h*(m+l)),f,g]).map((function(t){return 1e4*((i-t**o)/(c*t**o-n))**s})),[b,w,y]=e(u,M),S=(b+(r-1)*y)/r,C=(w+(a-1)*S)/a;return p.spaces.absxyzd65.toXYZ([S,C,y])},parse(t,e=p.parseFunction(t)){if(e&&"jzabz"===e.name)return{spaceId:"jzazbz",coords:e.args.slice(0,3),alpha:e.args.slice(3)[0]}},instance:{toString({format:t,...e}={}){return p.prototype.toString.call(this,{name:"jzazbz",format:t,...e})}}}),p.defineSpace({id:"jzczhz",name:"JzCzHz",coords:{Jz:[0,1],chroma:[0,1],hue:u},inGamut:t=>!0,white:p.D65,from:{jzazbz(t){let e,[r,a,s]=t;const o=5e-6;return e=Math.abs(a)<o&&Math.abs(s)<o?NaN:180*Math.atan2(s,a)/Math.PI,[r,Math.sqrt(a**2+s**2),d(e)]}},to:{jzazbz:t=>[t[0],t[1]*Math.cos(t[2]*Math.PI/180),t[1]*Math.sin(t[2]*Math.PI/180)]},parse(t,e=p.parseFunction(t)){if(e&&"jzczhz"===e.name)return e.args[0],{spaceId:"jzczhz",coords:e.args.slice(0,3),alpha:e.args.slice(3)[0]}}}),p.spaces.rec2020,p.defineSpace({id:"ictcp",name:"ICTCP",coords:{I:[0,1],CT:[-.5,.5],CP:[-.5,.5]},inGamut:t=>!0,white:p.whites.D65,c1:.8359375,c2:2413/128,c3:18.6875,m1:2610/16384,m2:2523/32,im1:16384/2610,im2:32/2523,XYZtoLMS_M:[[.3592,.6976,-.0358],[-.1922,1.1004,.0755],[.007,.0749,.8434]],Rec2020toLMS_M:[[1688/4096,2146/4096,262/4096],[683/4096,2951/4096,462/4096],[99/4096,309/4096,3688/4096]],LMStoIPT_M:[[.5,.5,0],[6610/4096,-13613/4096,7003/4096],[17933/4096,-17390/4096,-543/4096]],IPTtoLMS_M:[[.9999888965628402,.008605050147287059,.11103437159861648],[1.00001110343716,-.008605050147287059,-.11103437159861648],[1.0000320633910054,.56004913547279,-.3206339100541203]],LMStoRec2020_M:[[3.437556893281401,-2.507211212509506,.06965431922810461],[-.7914286866564416,1.983837219874009,-.19240853321756743],[-.025646662911506475,-.09924024864394557,1.124886911555452]],LMStoXYZ_M:[[2.0701800566956137,-1.326456876103021,.20661600684785517],[.3649882500326575,.6804673628522352,-.04542175307585323],[-.04959554223893211,-.04942116118675749,1.1879959417328034]],fromXYZ(t){const{XYZtoLMS_M:r}=this;let[a,s,o]=p.spaces.absxyzd65.fromXYZ(t),i=e(r,[a,s,o]);return this.LMStoICtCp(i)},toXYZ(t){const{LMStoXYZ_M:r}=this;let a=e(r,this.ICtCptoLMS(t));return p.spaces.absxyzd65.toXYZ(a)},LMStoICtCp(t){const{LMStoIPT_M:r,c1:a,c2:s,c3:o,m1:i,m2:n}=this;return e(r,t.map((function(t){let e=a+s*(t/1e4)**i,r=1+o*(t/1e4)**i;return console.log({val:t,num:e,denom:r}),(e/r)**n})))},ICtCptoLMS(t){const{IPTtoLMS_M:r,c1:a,c2:s,c3:o,im1:i,im2:n}=this;return e(r,t).map((function(t){return 1e4*(Math.max(t**n-a,0)/(s-o*t**n))**i}))}});let m={range(...t){return p.range(this,...t)},mix(t,e=.5,r={}){"object"===a(e)&&([e,r]=[.5,e]);let{space:s,outputSpace:o}=r;return t=p.get(t),this.range(t,{space:s,outputSpace:o})(e)},steps(...t){return p.steps(this,...t)}};function f(t){return"function"===a(t)&&t.rangeArgs}function g(t,e,r){return isNaN(t)?e:isNaN(e)?t:t+(e-t)*r}return p.steps=function(t,e,r={}){let a;f(t)&&([a,r]=[t,e],[t,e]=a.rangeArgs.colors);let{maxDeltaE:s,steps:o=2,maxSteps:i=1e3,...n}=r;a||(t=p.get(t),e=p.get(e),a=p.range(t,e,n));let c=this.deltaE(e),h=s>0?Math.max(o,Math.ceil(c/s)+1):o,l=[];if(void 0!==i&&(h=Math.min(h,i)),1===h)l=[{p:.5,color:a(.5)}];else{let t=1/(h-1);l=Array.from({length:h},(e,r)=>{let s=r*t;return{p:s,color:a(s)}})}if(s>0){let t=l.reduce((t,e,r)=>0===r?0:Math.max(t,e.color.deltaE(l[r-1].color)),0);for(;t>s;){t=0;for(let e=1;e<l.length&&l.length<i;e++){let r=l[e-1],s=l[e],o=(s.p+r.p)/2,i=a(o);t=Math.max(t,i.deltaE(r.color),i.deltaE(s.color)),l.splice(e,0,{p:o,color:a(o)}),e++}}}return l=l.map(t=>t.color),l},p.range=function(t,e,r={}){if(f(t)){let[r,a]=[t,e];return p.range(...r.rangeArgs.colors,{...r.rangeArgs.options,...a})}let{space:a,outputSpace:s,progression:o,premultiplied:i}=r;t=new p(t),e=new p(e);let n={colors:[t,e],options:r};if(a=a?p.space(a):p.spaces[p.defaults.interpolationSpace]||t.space,s=s?p.space(s):t.space||a,t=t.to(a).toGamut(),e=e.to(a).toGamut(),a.coords.hue&&a.coords.hue.isAngle){let s=r.hue=r.hue||"shorter";[t[a.id].hue,e[a.id].hue]=function(t,e){if("raw"===t)return e;let[r,a]=e.map(d),s=a-r;return"increasing"===t?s<0&&(a+=360):"decreasing"===t?s>0&&(r+=360):"longer"===t?-180<s&&s<180&&(s>0?a+=360:r+=360):"shorter"===t&&(s>180?r+=360:s<-180&&(a+=360)),[r,a]}(s,[t[a.id].hue,e[a.id].hue])}return i&&(t.coords=t.coords.map(e=>e*t.alpha),e.coords=e.coords.map(t=>t*e.alpha)),Object.assign(r=>{r=o?o(r):r;let n=t.coords.map((t,a)=>g(t,e.coords[a],r)),c=g(t.alpha,e.alpha,r),h=new p(a,n,c);return i&&(h.coords=h.coords.map(t=>t/c)),s!==a&&(h=h.to(s)),h},{rangeArgs:n})},Object.assign(p.defaults,{interpolationSpace:"lab"}),Object.assign(p.prototype,m),p.statify(Object.keys(m)),p.prototype.deltaECMC=function(t,{l:e=2,c:r=1}={}){t=p.get(t);let[a,s,o]=this.lab,i=this.chroma,n=this.hue,[c,h,l]=t.lab,u=t.chroma;i<0&&(i=0),u<0&&(u=0);let d=a-c,m=i-u,f=s-h,g=o-l;const M=Math.PI/180;let b=f**2+g**2-m**2,w=.511;a>=16&&(w=.040975*a/(1+.01765*a));let y,S=.0638*i/(1+.0131*i)+.638;Number.isNaN(n)&&(n=0),y=n>=164&&n<=345?.56+Math.abs(.2*Math.cos((n+168)*M)):.36+Math.abs(.4*Math.cos((n+35)*M));let C=Math.pow(i,4),z=Math.sqrt(C/(C+1900)),I=(d/(e*w))**2;return I+=(m/(r*S))**2,I+=b/(S*(z*y+1-z))**2,Math.sqrt(I)},p.statify(["deltaECMC"]),p.prototype.deltaE2000=function(t,{kL:e=1,kC:r=1,kH:a=1}={}){t=p.get(t);let[s,o,i]=this.lab,n=this.chroma,[c,h,l]=t.lab,u=t.chroma;n<0&&(n=0),u<0&&(u=0);let d=(n+u)/2,m=Math.pow(d,7);const f=Math.pow(25,7);let g=.5*(1-Math.sqrt(m/(m+f))),M=(1+g)*o,b=(1+g)*h,w=Math.sqrt(M**2+i**2),y=Math.sqrt(b**2+l**2);const S=Math.PI,C=180/S,z=S/180;let I=0===M&&0===i?0:Math.atan2(i,M),Y=0===b&&0===l?0:Math.atan2(l,b);I<0&&(I+=2*S),Y<0&&(Y+=2*S),I*=C,Y*=C;let X,Z=c-s,N=y-w,_=Y-I,G=I+Y,x=Math.abs(_);0==w&&0==y?X=0:x<=180?X=_:_>180?X=_-360:_<-180?X=_+360:console.log("the unthinkable has happened");let j,v=2*Math.sqrt(y*w)*Math.sin(X*z/2),A=(s+c)/2,O=(w+y)/2,D=Math.pow(O,7);j=0==w&&0==y?G:x<=180?G/2:G<360?(G+360)/2:(G-360)/2;let E=(A-50)**2,P=1+.015*E/Math.sqrt(20+E),L=1+.045*O,k=1;k-=.17*Math.cos((j-30)*z),k+=.24*Math.cos(2*j*z),k+=.32*Math.cos((3*j+6)*z),k-=.2*Math.cos((4*j-63)*z);let T=1+.015*O*k,$=30*Math.exp(-1*((j-275)/25)**2),q=2*Math.sqrt(D/(D+f)),F=(Z/(e*P))**2;return F+=(N/(r*L))**2,F+=(v/(a*T))**2,F+=-1*Math.sin(2*$*z)*q*(N/(r*L))*(v/(a*T)),Math.sqrt(F)},p.statify(["deltaE2000"]),p.prototype.deltaEJz=function(t){t=p.get(t);let[e,r,a]=this.jzczhz,[s,o,i]=t.jzczhz,n=e-s,c=r-o;Number.isNaN(a)&&Number.isNaN(i)?(a=0,i=0):Number.isNaN(a)?a=i:Number.isNaN(i)&&(i=a);let h=a-i,l=2*Math.sqrt(r*o)*Math.sin(h*Math.PI/180);return Math.sqrt(n**2+c**2+l**2)},p.statify(["deltaEJz"]),p.CATs={},p.hooks.add("chromatic-adaptation-start",t=>{t.options.method&&(t.M=p.adapt(t.W1,t.W2,t.options.method))}),p.hooks.add("chromatic-adaptation-end",t=>{t.M||(t.M=p.adapt(t.W1,t.W2,t.options.method))}),p.defineCAT=function({id:t,toCone_M:e,fromCone_M:r}){p.CATs[t]=arguments[0]},p.adapt=function(t,r,a="Bradford"){let s=p.CATs[a],[o,i,n]=e(s.toCone_M,t),[c,h,l]=e(s.toCone_M,r),u=e([[c/o,0,0],[0,h/i,0],[0,0,l/n]],s.toCone_M);return e(s.fromCone_M,u)},p.defineCAT({id:"von Kries",toCone_M:[[.40024,.7076,-.08081],[-.2263,1.16532,.0457],[0,0,.91822]],fromCone_M:[[1.8599364,-1.1293816,.2198974],[.3611914,.6388125,-64e-7],[0,0,1.0890636]]}),p.defineCAT({id:"Bradford",toCone_M:[[.8951,.2664,-.1614],[-.7502,1.7135,.0367],[.0389,-.0685,1.0296]],fromCone_M:[[.9869929,-.1470543,.1599627],[.4323053,.5183603,.0492912],[-.0085287,.0400428,.9684867]]}),p.defineCAT({id:"CAT02",toCone_M:[[.7328,.4296,-.1624],[-.7036,1.6975,.0061],[.003,.0136,.9834]],fromCone_M:[[1.0961238,-.278869,.1827452],[.454369,.4735332,.0720978],[-.0096276,-.005698,1.0153256]]}),p.defineCAT({id:"CAT16",toCone_M:[[.401288,.650173,-.051461],[-.250268,1.204414,.045854],[-.002079,.048952,.953127]],fromCone_M:[[1.862067855087233,-1.011254630531685,.1491867754444518],[.3875265432361372,.6214474419314753,-.008973985167612518],[-.01584149884933386,-.03412293802851557,1.04996443687785]]}),Object.assign(p.whites,{A:[1.0985,1,.35585],C:[.98074,1,1.18232],D55:[.95682,1,.92149],D75:[.94972,1,1.22638],E:[1,1,1],F2:[.99186,1,.67393],F7:[.95041,1,1.08747],F11:[1.00962,1,.6435]}),t.default=p,t.util=h,t}({});
//# sourceMappingURL=color.min.js.map