colorjs.io
Version:
Color space agnostic color manipulation library
3 lines (2 loc) • 29 kB
JavaScript
"use strict";function t(t,e){let r=t.length;Array.isArray(t[0])||(t=[t]),Array.isArray(e[0])||(e=e.map(t=>[t]));let s=e[0].length,a=e[0].map((t,r)=>e.map(t=>t[r])),o=t.map(t=>a.map(e=>Array.isArray(t)?t.reduce((t,r,s)=>t+r*(e[s]||0),0):e.reduce((e,r)=>e+r*t,0)));return 1===r&&(o=o[0]),1===s?o.map(t=>t[0]):o}function e(t){return"string"===r(t)}function r(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 a(t,e,r){let s=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,s)}function o(t,e,r=[0,1]){e=+e;let s=((r[1]||r[0]||1)+"").length,a=Math.max(0,e+1-s);return+t.toFixed(a)}function i(t){if(t.indexOf(".")>0){let[e,r]=t.split("."),s=Color.space(e);if(!(r in s.coords))throw new ReferenceError(`Color space "${s.name}" has no "${r}" coordinate.`);return[s,r]}}function n(t,e,r){let s=e.split("."),a=s.pop();if(t=s.reduceRight((t,e)=>t&&t[e],t))return void 0===r?t[a]:t[a]=r}Object.defineProperty(exports,"__esModule",{value:!0});var c=Object.freeze({__proto__:null,isString:e,type:r,extend:s,copyDescriptor:a,capitalize:function(t){return t?t[0].toUpperCase()+t.slice(1):t},toPrecision:o,parseCoord:i,value:n,multiplyMatrices:t});const h="undefined"!=typeof document;class l{constructor(...t){let r,s;if(t[0]&&"object"==typeof t[0]&&(t[0].space||t[0].spaceId)&&t[0].coords)s=t[0];else if(e(t[0])){if(h&&0===t[0].indexOf("--")){let t=arguments[1]&&1===arguments[1].nodeType?arguments[1]:document.documentElement;r=getComputedStyle(t).getPropertyValue(arguments[0])}else 1===t.length&&(r=t[0]);r&&(s=l.parse(r))}if(s)"spaceId"in s?this.spaceId=s.spaceId:this.space=s.space,this.coords=s.coords.slice(),this.alpha=s.alpha;else{let e,r,s;Array.isArray(t[0])?[e,r,s]=["sRGB",...t]:[e,r,s]=t,this.spaceId=e||"sRGB",this.coords=r?r.slice():[0,0,0],this.alpha=s}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 l.spaces[this.spaceId]}set space(t){return this.spaceId=t}get spaceId(){return this._spaceId}set spaceId(t){let e=l.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||l.whites.D50}set(t,e){if(1===arguments.length&&"object"===r(arguments[0])){let t=arguments[0];for(let e in t)this.set(e,t[e])}else if("function"==typeof e){let r=n(this,t);n(this,t,e.call(this,r))}else n(this,t,e);return this}lighten(t=.25){let e=new l(this),r=e.lightness;return e.lightness=r*(1+t),e}darken(t=.25){let e=new l(this),r=e.lightness;return e.lightness=r*(1-t),e}distance(t,e="lab"){t=l.get(t);let r=this[(e=l.space(e)).id],s=t[e.id];return Math.sqrt(r.reduce((t,e,r)=>isNaN(e)||isNaN(s[r])?t:t+(s[r]-e)**2,0))}deltaE(t,r={}){e(r)&&(r={method:r});let{method:s=l.defaults.deltaE,...a}=r;return t=l.get(t),this["deltaE"+s]?this["deltaE"+s](t,a):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=l.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,s=t+15*e+3*r;return[4*t/s,9*e/s]}get xy(){let[t,e,r]=this.xyz,s=t+e+r;return[t/s,e/s]}getCoords({inGamut:t,precision:e=l.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,s)=>o(r,e,t[s]))}return r}inGamut(t=this.space,e){return t=l.space(t),l.inGamut(t,this[t.id],e)}static inGamut(t,e,{epsilon:r=75e-6}={}){if((t=l.space(t)).inGamut)return t.inGamut(e);{if(!t.coords)return!0;let s=Object.values(t.coords);return e.every((t,e)=>{if(Number.isNaN(t))return!0;let[a,o]=s[e];return(void 0===a||t>=a-r)&&(void 0===o||t<=o+r)})}}toGamut({method:t=l.defaults.gamutMapping,space:r=this.space,inPlace:s}={}){if(e(arguments[0])&&(r=arguments[0]),r=l.space(r),this.inGamut(r,{epsilon:0}))return this;let a=this.to(r);if(t.indexOf(".")>0&&!this.inGamut(r)){let e=a.toGamut({method:"clip",space:r}),s=this.deltaE(e,{method:"2000"});if(this.deltaE(e,{method:"2000"})>2.3){let[e,o]=i(t),n=a.to(e),c=.001,h=e.coords[o][0],l=n[o],p=a.deltaE(n,{method:"2000"});for(;l-h>c&&p<s;){let t=n.toGamut({space:r,method:"clip"}),e=n.deltaE(t,{method:"2000"});if(p=a.deltaE(n,{method:"2000"}),e-2<c)h=n[o];else{if(Math.abs(e-2)<c)break;l=n[o]}n[o]=(l+h)/2}a=n.to(r)}else a=e}if("clip"===t||!a.inGamut(r,{epsilon:0})){let t=Object.values(r.coords);a.coords=a.coords.map((e,r)=>{let[s,a]=t[r];return void 0!==s&&(e=Math.max(s,e)),void 0!==a&&(e=Math.min(e,a)),e})}return r.id!==this.spaceId&&(a=a.to(this.space)),s?(this.coords=a.coords,this):a}clone(){return new l(this.spaceId,this.coords,this.alpha)}to(t,{inGamut:e}={}){let r=(t=l.space(t)).id,s=new l(r,this[r],this.alpha);return e&&s.toGamut({inPlace:!0}),s}toJSON(){return{spaceId:this.spaceId,coords:this.coords,alpha:this.alpha}}toString({precision:t=l.defaults.precision,format:r,commas:s,inGamut:a,name:o="color",fallback:i}={}){let n=this.alpha<1?` ${s?",":"/"} ${this.alpha}`:"",c=this.getCoords({inGamut:a,precision:t});if(c=c.map(t=>t||0),e(r)&&"%"===r){let e=t;(!Number.isInteger(t)||t>21)&&(e=21),r=t=>t.toLocaleString("en-US",{style:"percent",maximumSignificantDigits:e})}"function"==typeof r&&(c=c.map(r));let p=[...c];"color"===o&&p.unshift(this.space?this.space.cssId||this.space.id:"XYZ");let u=`${o}(${p.join(s?", ":" ")}${n})`;if(i){if(!h||!self.CSS||CSS.supports("color",u))return u=new String(u),u.color=this,u;let e=Array.isArray(i)?i.slice():l.defaults.fallbackSpaces;for(let r,s=0;r=e[s];s++)if(l.spaces[r]){let a=this.to(r);if(u=a.toString({precision:t}),CSS.supports("color",u))return u=new String(u),u.color=a,u;e===l.defaults.fallbackSpaces&&(e.splice(s,1),s--)}let r=this.to("srgb");u=new String(r.toString({commas:!0})),u.color=r}return u}equals(t){return t=l.get(t),this.spaceId===t.spaceId&&this.alpha===t.alpha&&this.coords.every((e,r)=>e===t.coords[r])}static chromaticAdaptation(e,r,s,a={}){if((e=e||l.whites.D50)===(r=r||l.whites.D50))return s;let o={W1:e,W2:r,XYZ:s,options:a};if(l.hooks.run("chromatic-adaptation-start",o),o.M||(o.W1===l.whites.D65&&o.W2===l.whites.D50?o.M=[[1.0478112,.0228866,-.050127],[.0295424,.9904844,-.0170491],[-.0092345,.0150436,.7521316]]:o.W1===l.whites.D50&&o.W2===l.whites.D65&&(o.M=[[.9555766,-.0230393,.0631636],[-.0282895,1.0099416,.0210077],[.0122982,-.020483,1.3299098]])),l.hooks.run("chromatic-adaptation-end",o),o.M)return t(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(l.hooks.run("parse-start",e),e.color)return e.color;if(e.parsed=l.parseFunction(e.str),l.hooks.run("parse-function-start",e),e.color)return e.color;for(let t of Object.values(l.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)&&h&&document.head){let s=document.head.style.color;if(document.head.style.color="",document.head.style.color=t,document.head.style.color!==s){let a=getComputedStyle(document.head).color;document.head.style.color=s,a&&(t=a,e.parsed=l.parseFunction(a),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(l.spaces).find(e=>(e.cssId||e.id)===t);if(r){let t=Object.keys(r.coords).length,s=e.parsed.rawArgs.indexOf("/")>0?e.parsed.args.pop():1,a=Array(t).fill(0);return a.forEach((t,r)=>a[r]=e.parsed.args[r]||0),{spaceId:r.id,coords:a,alpha:s}}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=l.space(e))===(r=l.space(r)))return t;t=t.map(t=>Number.isNaN(t)?0:t);let s=e.id,a=r.id;if(r.from&&r.from[s])return r.from[s](t);if(e.to&&e.to[a])return e.to[a](t);let o=e.toXYZ(t);return r.white!==e.white&&(o=l.chromaticAdaptation(e.white,r.white,o)),r.fromXYZ(o)}static get(t,...e){return t instanceof l?t:new l(t,...e)}static space(t){let e=r(t);if("string"===e){let e=l.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=l.spaces[t]=arguments[0];if(e){const t=["id","parse","instance","properties"];let s=l.spaces[e];for(let e in s)t.includes(e)||e in r||a(r,s,e)}let o=r.coords;if(r.properties&&s(l.prototype,r.properties),!r.fromXYZ&&!r.toXYZ){let t;if(r.from&&r.to){let e=new Set(Object.keys(r.from)),s=new Set(Object.keys(r.to)),a=[...e].filter(t=>{if(s.has(t)){let e=l.spaces[t];return e&&e.fromXYZ&&e.toXYZ}});a.length>0&&(t=l.spaces[a[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(o);return Object.defineProperty(l.prototype,t,{get(){let e=l.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 s=i.indexOf(e);return s>-1?t[s]:Reflect.get(t,e,r)},set:(e,r,s,a)=>{let o=i.indexOf(r);return r>-1&&(o=r),o>-1?(e[o]=s,this.coords=l.convert(e,t,this.spaceId),!0):Reflect.set(e,r,s,a)}}):e},set(e){this.coords=l.convert(e,t,this.spaceId)},configurable:!0,enumerable:!0}),r}static defineShortcut(t,e=l.prototype,r){r&&(l.shortcuts[t]=r),Object.defineProperty(e,t,{get(){return n(this,l.shortcuts[t])},set(e){return n(this,l.shortcuts[t],e)},configurable:!0,enumerable:!0})}static statify(t=[]){t=t||Object.getOwnPropertyNames(l.prototype);for(let t of Object.getOwnPropertyNames(l.prototype)){let e=Object.getOwnPropertyDescriptor(l.prototype,t);e.get||e.set||"function"!=typeof e.value||t in l||(l[t]=function(e,...r){return(e=l.get(e))[t](...r)})}}}Object.assign(l,{util:c,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"]}}),l.defineSpace({id:"xyz",name:"XYZ",coords:{X:[],Y:[],Z:[]},white:l.whites.D50,inGamut:t=>!0,toXYZ:t=>t,fromXYZ:t=>t});for(let t in l.shortcuts)l.defineShortcut(t);l.statify(),l.defineSpace({id:"lab",name:"Lab",coords:{L:[0,100],a:[-100,100],b:[-100,100]},inGamut:t=>!0,white:l.whites.D50,"ε":216/24389,"κ":24389/27,fromXYZ(t){const{"κ":e,"ε":r,white:s}=this;let a=t.map((t,e)=>t/s[e]).map(t=>t>r?Math.cbrt(t):(e*t+16)/116);return[116*a[1]-16,500*(a[0]-a[1]),200*(a[1]-a[2])]},toXYZ(t){const{"κ":e,"ε":r,white:s}=this;let a=[];return a[1]=(t[0]+16)/116,a[0]=t[1]/500+a[1],a[2]=a[1]-t[2]/200,[Math.pow(a[0],3)>r?Math.pow(a[0],3):(116*a[0]-16)/e,t[0]>e*r?Math.pow((t[0]+16)/116,3):t[0]/e,Math.pow(a[2],3)>r?Math.pow(a[2],3):(116*a[2]-16)/e].map((t,e)=>t*s[e])},parse(t,e=l.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),l.prototype.toString.call(this,{name:"lab",format:t,...e})}}});const p=[0,360];function u(t){return(t%360+360)%360}p.isAngle=!0,l.defineSpace({id:"lch",name:"LCH",coords:{lightness:[0,100],chroma:[0,150],hue:p},inGamut:t=>!0,white:l.whites.D50,from:{lab(t){let e,[r,s,a]=t;const o=5e-4;return e=Math.abs(s)<o&&Math.abs(a)<o?NaN:180*Math.atan2(a,s)/Math.PI,[r,Math.sqrt(s**2+a**2),u(e)]}},to:{lab(t){let[e,r,s]=t;return r<0&&(r=0),isNaN(s)&&(s=0),[e,r*Math.cos(s*Math.PI/180),r*Math.sin(s*Math.PI/180)]}},parse(t,e=l.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),l.prototype.toString.call(this,{name:"lch",format:t,...e})}}}),l.defineSpace({id:"srgb",name:"sRGB",coords:{red:[0,1],green:[0,1],blue:[0,1]},white:l.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(e){return e=this.toLinear(e),t(this.toXYZ_M,e)},fromXYZ(e){return this.toGamma(t(this.fromXYZ_M,e))},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 s=e&&r.every(t=>t%17==0);return"#"+r.map(t=>s?(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="%",...s}={}){if(255===r)r=t=>255*t;else if("hex"===r)return this.toHex(arguments[0]);return l.prototype.toString.call(this,{inGamut:t,commas:e,format:r,name:"rgb"+(e&&this.alpha<1?"a":""),...s})}},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]}}}),l.hooks.add("parse-start",t=>{let e=t.str;/^#([a-f0-9]{3,4}){1,2}$/i.test(e)&&(t.color=l.spaces.srgb.parseHex(e))}),l.defineSpace({id:"hsl",name:"HSL",coords:{hue:p,saturation:[0,100],lightness:[0,100]},inGamut(t){let e=this.to.srgb(t);return l.inGamut("srgb",e)},white:l.whites.D65,from:{srgb(t){t=t.map(t=>100*t);let e=Math.max.apply(Math,t),r=Math.min.apply(Math,t),[s,a,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 s:i=(a-o)/h+(a<o?6:0);break;case a:i=(o-s)/h+2;break;case o:i=(s-a)/h+4}i*=60}return[i,n,c]}},to:{srgb(t){let[e,r,s]=t;function a(t){let a=(t+e/30)%12,o=r*Math.min(s,1-s);return s-o*Math.max(-1,Math.min(a-3,9-a,1))}return e%=360,e<0&&(e+=360),r/=100,s/=100,[a(0),a(8),a(4)]}},parse(t,e=l.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:s,...a}={}){return r||(r=(t,e)=>e>0?t+"%":t),l.prototype.toString.call(this,{inGamut:!0,commas:e,format:r,name:"hsl"+(e&&this.alpha<1?"a":""),...a})}}}),l.defineSpace({id:"hwb",name:"HWB",coords:{hue:p,whiteness:[0,100],blackness:[0,100]},inGamut(t){let e=this.to.srgb(t);return l.inGamut("srgb",e)},white:l.whites.D65,from:{srgb(t){let e=l.spaces.hsl.from.srgb(t)[0],r=Math.min(...t),s=1-Math.max(...t);return r*=100,s*=100,[e,r,s]},hsv(t){let[e,r,s]=t;return[e,s*(100-r)/100,100-s]},hsl(t){let e=l.spaces.hsv.from.hsl(t);return this.hsv(e)}},to:{srgb(t){let[e,r,s]=t;r/=100,s/=100;let a=r+s;if(a>=1){let t=r/a;return[t,t,t]}let o=l.spaces.hsl.to.srgb([e,100,50]);for(var i=0;i<3;i++)o[i]*=1-r-s,o[i]+=r;return o},hsv(t){let[e,r,s]=t;r/=100,s/=100;let a=r+s;return a>=1?[e,0,r/a]:[e,100-100*r/(100-s),100*(1-s)]},hsl(t){let e=l.spaces.hwb.to.hsv(t);return l.spaces.hsv.to.hsl(e)}},parse(t,e=l.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,...s}={}){return t||(t=(t,e)=>e>0?t+"%":t),l.prototype.toString.call(this,{inGamut:!0,commas:!1,format:t,name:"hwb",...s})}}}),l.defineSpace({id:"hsv",name:"HSV",coords:{hue:p,saturation:[0,100],value:[0,100]},inGamut(t){let e=this.to.hsl(t);return l.spaces.hsl.inGamut(e)},white:l.whites.D65,from:{hsl(t){let[e,r,s]=t;r/=100,s/=100;let a=s+r*Math.min(s,1-s);return[e,0===a?0:200*(1-s/a),100*a]}},to:{hsl(t){let[e,r,s]=t;r/=100,s/=100;let a=100*s*(1-r/2);return[e,0===a||1===a?0:(s-a)/Math.min(a,1-a),a]}}}),l.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]]}),l.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]]}),l.defineSpace({inherits:"srgb",id:"prophoto",name:"ProPhoto",cssId:"prophoto-rgb",white:l.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]]}),l.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]]}),l.defineSpace({id:"absxyzd65",name:"Absolute XYZ D65",coords:{Xa:[0,9504.7],Ya:[0,1e4],Za:[0,10888.3]},white:l.whites.D65,Yw:203,inGamut:t=>!0,fromXYZ(t){const e=l.whites.D50,r=l.whites.D65;t=l.chromaticAdaptation(e,r,t);const{Yw:s}=this;return t.map((function(t){return Math.max(t*s,0)}))},toXYZ(t){const{Yw:e}=this;let r=t.map((function(t){return Math.max(t/e,0)}));const s=l.whites.D65,a=l.whites.D50;return l.chromaticAdaptation(s,a,r)}}),l.defineSpace({id:"jzazbz",cssid:"Jzazbz",name:"Jzazbz",coords:{Jz:[0,1],az:[-.5,.5],bz:[-.5,.5]},inGamut:t=>!0,white:l.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(e){const{b:r,g:s,n:a,p:o,c1:i,c2:n,c3:c,d:h,d0:p,XYZtoCone_M:u,ConetoIab_M:d}=this;let[m,f,g]=l.spaces.absxyzd65.fromXYZ(e),M=t(u,[r*m-(r-1)*g,s*f-(s-1)*m,g]).map((function(t){return((i+n*(t/1e4)**a)/(1+c*(t/1e4)**a))**o})),[b,w,y]=t(d,M);return[(1+h)*b/(1+h*b)-p,w,y]},toXYZ(e){const{b:r,g:s,ninv:a,pinv:o,c1:i,c2:n,c3:c,d:h,d0:p,ConetoXYZ_M:u,IabtoCone_M:d}=this;let[m,f,g]=e,M=t(d,[(m+p)/(1+h-h*(m+p)),f,g]).map((function(t){return 1e4*((i-t**o)/(c*t**o-n))**a})),[b,w,y]=t(u,M),S=(b+(r-1)*y)/r,C=(w+(s-1)*S)/s;return l.spaces.absxyzd65.toXYZ([S,C,y])},parse(t,e=l.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 l.prototype.toString.call(this,{name:"jzazbz",format:t,...e})}}}),l.defineSpace({id:"jzczhz",name:"JzCzHz",coords:{Jz:[0,1],chroma:[0,1],hue:p},inGamut:t=>!0,white:l.D65,from:{jzazbz(t){let e,[r,s,a]=t;const o=5e-6;return e=Math.abs(s)<o&&Math.abs(a)<o?NaN:180*Math.atan2(a,s)/Math.PI,[r,Math.sqrt(s**2+a**2),u(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=l.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]}}}),l.spaces.rec2020,l.defineSpace({id:"ictcp",name:"ICTCP",coords:{I:[0,1],CT:[-.5,.5],CP:[-.5,.5]},inGamut:t=>!0,white:l.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(e){const{XYZtoLMS_M:r}=this;let[s,a,o]=l.spaces.absxyzd65.fromXYZ(e),i=t(r,[s,a,o]);return this.LMStoICtCp(i)},toXYZ(e){const{LMStoXYZ_M:r}=this;let s=t(r,this.ICtCptoLMS(e));return l.spaces.absxyzd65.toXYZ(s)},LMStoICtCp(e){const{LMStoIPT_M:r,c1:s,c2:a,c3:o,m1:i,m2:n}=this;return t(r,e.map((function(t){let e=s+a*(t/1e4)**i,r=1+o*(t/1e4)**i;return console.log({val:t,num:e,denom:r}),(e/r)**n})))},ICtCptoLMS(e){const{IPTtoLMS_M:r,c1:s,c2:a,c3:o,im1:i,im2:n}=this;return t(r,e).map((function(t){return 1e4*(Math.max(t**n-s,0)/(a-o*t**n))**i}))}});let d={range(...t){return l.range(this,...t)},mix(t,e=.5,s={}){"object"===r(e)&&([e,s]=[.5,e]);let{space:a,outputSpace:o}=s;return t=l.get(t),this.range(t,{space:a,outputSpace:o})(e)},steps(...t){return l.steps(this,...t)}};function m(t){return"function"===r(t)&&t.rangeArgs}function f(t,e,r){return isNaN(t)?e:isNaN(e)?t:t+(e-t)*r}l.steps=function(t,e,r={}){let s;m(t)&&([s,r]=[t,e],[t,e]=s.rangeArgs.colors);let{maxDeltaE:a,steps:o=2,maxSteps:i=1e3,...n}=r;s||(t=l.get(t),e=l.get(e),s=l.range(t,e,n));let c=this.deltaE(e),h=a>0?Math.max(o,Math.ceil(c/a)+1):o,p=[];if(void 0!==i&&(h=Math.min(h,i)),1===h)p=[{p:.5,color:s(.5)}];else{let t=1/(h-1);p=Array.from({length:h},(e,r)=>{let a=r*t;return{p:a,color:s(a)}})}if(a>0){let t=p.reduce((t,e,r)=>0===r?0:Math.max(t,e.color.deltaE(p[r-1].color)),0);for(;t>a;){t=0;for(let e=1;e<p.length&&p.length<i;e++){let r=p[e-1],a=p[e],o=(a.p+r.p)/2,i=s(o);t=Math.max(t,i.deltaE(r.color),i.deltaE(a.color)),p.splice(e,0,{p:o,color:s(o)}),e++}}}return p=p.map(t=>t.color),p},l.range=function(t,e,r={}){if(m(t)){let[r,s]=[t,e];return l.range(...r.rangeArgs.colors,{...r.rangeArgs.options,...s})}let{space:s,outputSpace:a,progression:o,premultiplied:i}=r;t=new l(t),e=new l(e);let n={colors:[t,e],options:r};if(s=s?l.space(s):l.spaces[l.defaults.interpolationSpace]||t.space,a=a?l.space(a):t.space||s,t=t.to(s).toGamut(),e=e.to(s).toGamut(),s.coords.hue&&s.coords.hue.isAngle){let a=r.hue=r.hue||"shorter";[t[s.id].hue,e[s.id].hue]=function(t,e){if("raw"===t)return e;let[r,s]=e.map(u),a=s-r;return"increasing"===t?a<0&&(s+=360):"decreasing"===t?a>0&&(r+=360):"longer"===t?-180<a&&a<180&&(a>0?s+=360:r+=360):"shorter"===t&&(a>180?r+=360:a<-180&&(s+=360)),[r,s]}(a,[t[s.id].hue,e[s.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,s)=>f(t,e.coords[s],r)),c=f(t.alpha,e.alpha,r),h=new l(s,n,c);return i&&(h.coords=h.coords.map(t=>t/c)),a!==s&&(h=h.to(a)),h},{rangeArgs:n})},Object.assign(l.defaults,{interpolationSpace:"lab"}),Object.assign(l.prototype,d),l.statify(Object.keys(d)),l.prototype.deltaECMC=function(t,{l:e=2,c:r=1}={}){t=l.get(t);let[s,a,o]=this.lab,i=this.chroma,n=this.hue,[c,h,p]=t.lab,u=t.chroma;i<0&&(i=0),u<0&&(u=0);let d=s-c,m=i-u,f=a-h,g=o-p;const M=Math.PI/180;let b=f**2+g**2-m**2,w=.511;s>=16&&(w=.040975*s/(1+.01765*s));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)},l.statify(["deltaECMC"]),l.prototype.deltaE2000=function(t,{kL:e=1,kC:r=1,kH:s=1}={}){t=l.get(t);let[a,o,i]=this.lab,n=this.chroma,[c,h,p]=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+p**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===p?0:Math.atan2(p,b);I<0&&(I+=2*S),Y<0&&(Y+=2*S),I*=C,Y*=C;let X,Z=c-a,_=y-w,N=Y-I,x=I+Y,G=Math.abs(N);0==w&&0==y?X=0:G<=180?X=N:N>180?X=N-360:N<-180?X=N+360:console.log("the unthinkable has happened");let j,O=2*Math.sqrt(y*w)*Math.sin(X*z/2),v=(a+c)/2,A=(w+y)/2,D=Math.pow(A,7);j=0==w&&0==y?x:G<=180?x/2:x<360?(x+360)/2:(x-360)/2;let E=(v-50)**2,P=1+.015*E/Math.sqrt(20+E),L=1+.045*A,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*A*k,$=30*Math.exp(-1*((j-275)/25)**2),q=2*Math.sqrt(D/(D+f)),F=(Z/(e*P))**2;return F+=(_/(r*L))**2,F+=(O/(s*T))**2,F+=-1*Math.sin(2*$*z)*q*(_/(r*L))*(O/(s*T)),Math.sqrt(F)},l.statify(["deltaE2000"]),l.prototype.deltaEJz=function(t){t=l.get(t);let[e,r,s]=this.jzczhz,[a,o,i]=t.jzczhz,n=e-a,c=r-o;Number.isNaN(s)&&Number.isNaN(i)?(s=0,i=0):Number.isNaN(s)?s=i:Number.isNaN(i)&&(i=s);let h=s-i,p=2*Math.sqrt(r*o)*Math.sin(h*Math.PI/180);return Math.sqrt(n**2+c**2+p**2)},l.statify(["deltaEJz"]),l.CATs={},l.hooks.add("chromatic-adaptation-start",t=>{t.options.method&&(t.M=l.adapt(t.W1,t.W2,t.options.method))}),l.hooks.add("chromatic-adaptation-end",t=>{t.M||(t.M=l.adapt(t.W1,t.W2,t.options.method))}),l.defineCAT=function({id:t,toCone_M:e,fromCone_M:r}){l.CATs[t]=arguments[0]},l.adapt=function(e,r,s="Bradford"){let a=l.CATs[s],[o,i,n]=t(a.toCone_M,e),[c,h,p]=t(a.toCone_M,r),u=t([[c/o,0,0],[0,h/i,0],[0,0,p/n]],a.toCone_M);return t(a.fromCone_M,u)},l.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]]}),l.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]]}),l.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]]}),l.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(l.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]}),exports.default=l,exports.util=c;
//# sourceMappingURL=color.cjs.min.js.map