Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

better-color-tools

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-color-tools - npm Package Compare versions

Comparing version 0.12.0 to 0.12.1

6

CHANGELOG.md
# better-color-tools
## 0.12.1
### Patch Changes
- 2ab2559: Parse Lab and LCh as Oklab and Oklch
## 0.12.0

@@ -4,0 +10,0 @@

2

dist/index.min.js

@@ -1,1 +0,1 @@

function V(r,t=2){let e=r;for(;e.length<t;)e=`0${e}`;return e}function J(r){return r*(Math.PI/180)}function Q(r){return r*(180/Math.PI)}function c(r,t,e){return Math.min(Math.max(r,t),e)}function _(r,t){let[e,n,o,f]=t,a=f<1?`/${k(f,5)}`:"";switch(r){case"rgb":case"rgba":return f<1?`rgba(${Math.round(e*255)}, ${Math.round(n*255)}, ${Math.round(o*255)}, ${k(f,5)})`:`rgb(${Math.round(e*255)}, ${Math.round(n*255)}, ${Math.round(o*255)})`;case"oklab":case"oklch":return`${r}(${k(e*100,6)}% ${k(n,6)} ${k(o,6)}${a})`;default:return`color(${r} ${k(e,6)} ${k(n,6)} ${k(o,6)}${a})`}}function L(r,t){let e=[...r];for(let n=0;n<t.length;n++){let o=0;for(let f=0;f<t[n].length;f++)o+=r[f]*t[n][f];e[n]=o}return e}function k(r,t=2){let e=10**t;return Math.round(r*e)/e}var he=[[.4123907992659593,.357584339383878,.1804807884018343],[.2126390058715102,.715168678767756,.0721923153607337],[.0193308187155918,.119194779794626,.9505321522496607]],be=[[3.240969941904522,-1.537383177570094,-.4986107602930034],[-.9692436362808793,1.8759675015077202,.0415550574071756],[.0556300796969937,-.2039769588889766,1.0569715142428782]],me=[[.2104542553,.793617785,-.0040720468],[1.9779984951,-2.428592205,.4505937099],[.0259040371,.7827717662,-.808675766]],d=[[4.0767416621,-3.3077115913,.2309699292],[-1.2684380046,2.6097574011,-.3413193965],[-.0041960863,-.7034186147,1.707614701]],ge=[[.4122214708,.5363325363,.0514459929],[.2119034982,.6806995451,.1073969566],[.0883024619,.2817188376,.6299787005]],pe=[[1,.39633779217376774,.2158037580607588],[1,-.10556134232365633,-.0638541747717059],[1,-.08948418209496574,-1.2914855378640917]];function Te(r,t){let e=[1/0,1/0,1/0,1/0,1/0],n=1/0,o=1/0,f=1/0;-1.88170328*r-.80936493*t>1?(e=[1.19086277,1.76576728,.59662641,.75515197,.56771245],n=4.0767416621,o=-3.3077115913,f=.2309699292):1.81444104*r-1.19445276*t>1?(e=[.73956515,-.45954404,.08285427,.1254107,.14503204],n=-1.2684380046,o=2.6097574011,f=-.3413193965):(e=[1.35733652,-.00915799,-1.1513021,-.50559606,.00692167],n=-.0041960863,o=-.7034186147,f=1.707614701);let a=e[0]+e[1]*r+e[2]*t+e[3]*r*r+e[4]*r*t,s=.3963377774*r+.2158037573*t,i=-.1055613458*r-.0638541728*t,l=-.0894841775*r-1.291485548*t;{let x=1+a*s,b=1+a*i,h=1+a*l,g=x**3,E=b**3,T=h**3,y=3*s*x**2,M=3*i*b**2,v=3*l*h**2,$=6*s**2*x,u=6*i**2*b,R=6*l**2*h,w=n*g+o*E+f*T,I=n*y+o*M+f*v,N=n*$+o*u+f*R;a=a-w*I/(I*I-.5*w*N)}return a}function Me(r,t){let e=Te(r,t),[n,o,f]=q(P([1,e*r,e*t,1])),a=Math.cbrt(1/Math.max(n,o,f)),s=a*e;return{L:a,C:s}}function ke(r,t,e,n,o){let f=Me(r,t);if((e-o)*f.C-(f.L-o)*n<=0)return f.C*o/(n*f.L+f.C*(o-e));let a=f.C*(o-1)/(n*(f.L-1)+f.C*(o-e)),s=e-o,i=n,l=.3963377774*r+.2158037573*t,x=-.1055613458*r-.0638541728*t,b=-.0894841775*r-1.291485548*t,h=s+i*l,g=s+i*x,E=s+i*b,T=o*(1-a)+a*e,y=a*n,M=T+y*l,v=T+y*x,$=T+y*b,u=[[M**3,v**3,$**3],[3*h*M**2,3*g*v**2,3*E*$**2],[6*h**2*M,6*g**2*v,6*E**2*$]],R=d[0][0]*u[0][0]+d[0][1]*u[0][1]+d[0][2]*u[0][2]-1,w=d[0][0]*u[1][0]+d[0][1]*u[1][1]+d[0][2]*u[1][2],I=d[0][0]*u[2][0]+d[0][1]*u[2][1]+d[0][2]*u[2][2],N=w/(w*w-.5*R*I),A=N>=0?-R*N:1/0,Z=d[1][0]*u[0][0]+d[1][1]*u[0][1]+d[1][2]*u[0][2]-1,p=d[1][0]*u[1][0]+d[1][1]*u[1][1]+d[1][2]*u[1][2],Re=d[1][0]*u[2][0]+d[1][1]*u[2][1]+d[1][2]*u[2][2],ue=p/(p*p-.5*Z*Re),_e=ue>=0?-Z*ue:1/0,xe=d[2][0]*u[0][0]+d[2][1]*u[0][1]+d[2][2]*u[0][2]-1,U=d[2][0]*u[1][0]+d[2][1]*u[1][1]+d[2][2]*u[1][2],Be=d[2][0]*u[2][0]+d[2][1]*u[2][1]+d[2][2]*u[2][2],de=U/(U*U-.5*xe*Be),Ge=de>=0?-xe*de:1/0;return a+Math.min(A,_e,Ge)}function W(r){let t=Math.abs(r);return t<=.0031308?r*12.92:1.055*Math.pow(t,1/2.4)-.055}function ee(r){return Math.abs(r)<=.04045?r/12.92:((Math.abs(r)+.055)/1.055)**2.4}function H(r){let[t,e,n,o]=r;t=Math.abs(t%360);let f=e*(1-Math.abs(2*n-1)),a=f*(1-Math.abs(t/60%2-1)),s=0,i=0,l=0;0<=t&&t<60?(s=f,i=a):60<=t&&t<120?(s=a,i=f):120<=t&&t<180?(i=f,l=a):180<=t&&t<240?(i=a,l=f):240<=t&&t<300?(s=a,l=f):300<=t&&t<360&&(s=f,l=a);let x=n-f/2;return[s+x,i+x,l+x,o]}function te(r){let[t,e,n,o]=r;if(e+n>=1){let a=e/(e+n);return[a,a,a,o]}let f=H([t,100,50,o]);for(let a=0;a<3;a++)f[a]*=1-e-n,f[a]+=e;return f}function we(r,t=2e-4){let[e,n,o,f]=r,a=Math.abs(n)<t&&Math.abs(o)<t?0:Q(Math.atan2(o,n));for(;a<0;)a+=360;for(;a>=360;)a-=360;return[e,Math.sqrt(n**2+o**2),a,f]}function Le(r){let[t,e,n,o]=r;if(t===0)return[0,0,0,o];for(;n<0;)n+=360;for(;n>=360;)n-=360;let f=J(n);return[t,Math.cos(f)*e,Math.sin(f)*e,o]}function Oe(r){return L(r,me)}function q(r){let[t,e,n,o]=r,[f,a,s]=L([t**3,e**3,n**3,o],d);return[f,a,s,o]}function B(r){let[t,e,n,o]=r;return[W(t),W(e),W(n),o]}function re(r){let[t,e,n,o]=L(r,ge);return[Math.cbrt(t),Math.cbrt(e),Math.cbrt(n),o]}function ne(r){return L(r,he)}function P(r){return L(r,pe)}function F(r){let[t,e,n,o]=q(P(r));if(t>1.001||t<-.001||e>1.001||e<-.001||n>1.001||n<-.001){let[a,s,i]=r,l=Math.max(1e-5,Math.sqrt(s**2+i**2)),x=c(a,0,1),b=s/l,h=i/l,g=ke(b,h,a,l,x);return B(q(P([x*(1-g)+g*a,b*(g*l),h*(g*l),o])))}return B([t,e,n,o])}function D(r){return F(Le(r))}function G(r){let[t,e,n,o]=r;return[ee(t),ee(e),ee(n),o]}function z(r){return Oe(re(G(r)))}function S(r){return we(z(r))}function oe(r){return L(r,be)}function j(r,t){let[e,n,o,f]=S(r);return typeof t.lightness=="number"&&(t.mode==="relative"?e+=t.lightness:e=t.lightness),typeof t.chroma=="number"&&(t.mode==="relative"?n+=t.chroma:n=t.chroma),typeof t.hue=="number"&&(t.mode==="relative"?o+=t.hue:o=t.hue),typeof t.alpha=="number"&&(t.mode==="relative"?f+=t.alpha:f=t.alpha),D([e,n,o,f])}var ve={black:0,silver:12632256,gray:8421504,white:16777215,maroon:8388608,red:16711680,purple:8388736,fuchsia:16711935,green:32768,lime:65280,olive:8421376,yellow:16776960,navy:128,blue:255,teal:32896,aqua:65535,orange:16753920,aliceblue:15792383,antiquewhite:16444375,aquamarine:8388564,azure:15794175,beige:16119260,bisque:16770244,blanchedalmond:16772045,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,gainsboro:14474460,ghostwhite:16316671,gold:16766720,goldenrod:14329120,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,limegreen:3329330,linen:16445670,magenta:16711935,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,oldlace:16643558,olivedrab:7048739,orangered:16729344,orchid:14315734,palegoldenrod:15657130,palegreen:10025880,paleturquoise:11529966,palevioletred:14381203,papayawhip:16773077,peachpuff:16767673,peru:13468991,pink:16761035,plum:14524637,powderblue:11591910,rosybrown:12357519,royalblue:4286945,saddlebrown:9127187,salmon:16416882,sandybrown:16032864,seagreen:3050327,seashell:16774638,sienna:10506797,skyblue:8900331,slateblue:6970061,slategray:7372944,slategrey:7372944,snow:16775930,springgreen:65407,steelblue:4620980,tan:13808780,thistle:14204888,tomato:16737095,turquoise:4251856,violet:15631086,wheat:16113331,whitesmoke:16119285,yellowgreen:10145074,rebeccapurple:6697881},ae=ve;var $e=/-?[0-9.]+%?/g,Ie=/^#[0-9a-f]{3,8}$/i,Ae=/^([^( ]+)\s*\(\s*([^)]+)\)$/,De=16**6,fe=16**4,se=16**2;function m(r){let t=Se(r),e={get hex(){let[n,o,f,a]=t,s="#";return s+=V(Math.round(c(n*255,0,255)).toString(16),2),s+=V(Math.round(c(o*255,0,255)).toString(16),2),s+=V(Math.round(c(f*255,0,255)).toString(16),2),t[3]<1&&(s+=V(Math.round(a*255).toString(16),2)),s},get hexVal(){let[n,o,f,a]=t;return a<1&&console.warn(`hexVal converted a semi-transparent color (${a*100}%) to fully opaque`),n=Math.round(c(n*255,0,255)),o=Math.round(c(o*255,0,255)),f=Math.round(c(f*255,0,255)),n*fe+o*se+f},get rgb(){return _("rgb",t)},rgbVal:t,get rgba(){return _("rgb",t)},rgbaVal:t,get linearRGB(){return _("srgb-linear",G(t))},get linearRGBVal(){return G(t)},get p3(){return _("display-p3",t)},p3Val:t,get oklab(){return _("oklab",z(t))},get oklabVal(){return z(t)},get oklch(){return _("oklch",S(t))},get oklchVal(){return S(t)},get xyz(){return _("xyz-d65",ne(G(t)))},get xyzVal(){return ne(G(t))},adjust(n){return m(j(t,n))}};return e.toString=()=>e.hex,e}function ye(r){if(r>De)throw new Error("8-digit hex values (with transparency) aren\u2019t supported");let t=r,e=Math.floor(t/fe);t-=e*fe;let n=Math.floor(t/se);t-=n*se;let o=t;return[e/255,n/255,o/255,1]}function O(r,t){let e=r.match($e);if(!e)throw new Error(`Unexpected color format: ${r}`);let n=[0,0,0,1];for(let o=0;o<e.length;o++)e[o]&&(e[o].includes("%")?n[o]=parseFloat(e[o])/100:!t||t[o]===1/0||t[o]===1?n[o]=parseFloat(e[o]):n[o]=parseFloat(e[o])/t[o]);return n}function Se(r){let t=new Error(`Unable to parse color ${JSON.stringify(r)}`);if(r==null||r==null||typeof r=="boolean")throw t;if(Array.isArray(r)){if(r.some(a=>typeof a!="number"))throw new Error(`Color array must be numbers, received ${r}`);if(r.length<3||r.length>4)throw new Error(`Expected [R, G, B, A?], received ${r}`);let[e,n,o,f]=r;return[c(e,0,1),c(n,0,1),c(o,0,1),typeof f=="number"?c(f,0,1):1]}if(typeof r=="number")return ye(r);if(typeof r=="string"){let e=r.trim();if(!e)throw new Error("Expected color, received empty string");let n=e.toLowerCase();if(typeof ae[n]=="number")return ye(ae[n]);if(Ie.test(n)){let s=n.replace("#",""),i=[0,0,0,1];if(s.length===6||s.length===8)for(let l=0;l<s.length/2;l++){let x=l*2,b=x+2,h=s.substring(x,b);i[l]=parseInt(h,16)/255}else if(s.length===3||s.length===4)for(let l=0;l<s.length;l++){let x=s.charAt(l);i[l]=parseInt(`${x}${x}`,16)/255}else throw new Error(`Hex value "${n}" not a valid sRGB color`);return i}let o=e.match(Ae);if(!o)throw t;let[,f,a]=o;if(f==="color"){let s=a.indexOf(" ");f=a.substring(0,s),a=a.substring(s)}switch(f){case"rgb":case"rgba":case"srgb":{let[s,i,l,x]=O(a,[255,255,255,1]);return[c(s,0,1),c(i,0,1),c(l,0,1),c(x,0,1)]}case"linear-rgb":case"linear-srgb":case"rgb-linear":case"srgb-linear":{let s=O(a);return B(s)}case"hsl":case"hsla":{let[s,i,l,x]=O(a);return H([s,c(i,0,1),c(l,0,1),c(x,0,1)])}case"hwb":case"hwba":{let[s,i,l,x]=O(a);return te([s,c(i,0,1),c(l,0,1),c(x,0,1)])}case"p3":case"display-p3":{let[s,i,l,x]=O(a);return[c(s,0,1),c(i,0,1),c(l,0,1),c(x,0,1)]}case"oklab":return F(O(a));case"oklch":return D(O(a));case"xyz":case"xyz-d65":return B(oe(O(a)))}}if(typeof r=="object"){let e={...r},n=1;for(let o of Object.keys(e))o==="alpha"?n=c(e[o],0,1):e[o.toLowerCase()]=e[o];if("r"in e&&"g"in e&&"b"in e)return[c(e.r,0,1),c(e.g,0,1),c(e.b,0,1),n];if("h"in e&&"s"in e&&"l"in e)return H([e.h,c(e.s,0,1),c(e.l,0,1),n]);if("h"in e&&"w"in e&&"b"in e)return te([e.h,c(e.w,0,1),c(e.b,0,1),n]);if("l"in e&&"a"in e&&"b"in e)return F([e.l,e.a,e.b,n]);if("l"in e&&"c"in e&&"h"in e)return D([e.l,e.c,e.h,n]);if("x"in e&&"y"in e&&"z"in e)return B(oe([e.x,e.y,e.z,n]));throw t}throw t}function X(r,t,e=.5,n="oklab"){let o=c(e,0,1);if(e===0)return m(r);if(e===1)return m(t);let f=1-o,a=o,s={oklch:S,oklab:z,lms:p=>G(re(p)),linearRGB:G,sRGB:p=>p},i={oklch:D,oklab:F,lms:p=>q(B(p)),linearRGB:B,sRGB:p=>p},l=s[n],x=i[n];if(!l)throw new Error(`Unknown color space "${n}", try "oklab", "oklch", "linearRGB", or "sRGB"`);let[b,h,g,E]=m(r).rgbVal,[T,y,M,v]=m(t).rgbVal;n==="oklch"&&(b===h&&h===g||T===y&&y===M)&&(l=s.oklab,x=i.oklab);let[$,u,R,w]=l([b,h,g,E]),[I,N,A,Z]=l([T,y,M,v]);return n==="oklch"&&Math.abs(A-R)>180&&(Math.max(R,A)===A?A-=360:R-=360),m(x([$*f+I*a,u*f+N*a,R*f+A*a,w*f+Z*a]))}function K(r,t,e="oklab"){let n=c(t,-1,1);return n>=0?X(r,"black",n,e):C(r,-n)}function C(r,t,e="oklab"){let n=c(t,-1,1);return n>=0?X(r,"white",n,e):K(r,-n)}function Y(r){return m(r).xyzVal[1]}function le(r){return k(m(r).oklabVal[0],5)}function ce(r){return Y(r)<.36?"dark":"light"}function ie(r,t){let e=Y(r),n=Y(t),o=Math.max(e,n),f=Math.min(e,n),a=(o+.05)/(f+.05);return{ratio:a,AA:a>=4.5,AAA:a>=7}}var c0={adjust:j,contrastRatio:ie,darken:K,from:m,lighten:C,lightness:le,lightOrDark:ce,luminance:Y,mix:X};export{j as adjust,c as clamp,_ as colorFn,ie as contrastRatio,K as darken,c0 as default,J as degToRad,m as from,V as leftPad,ce as lightOrDark,C as lighten,le as lightness,Y as luminance,X as mix,L as multiplyColorMatrix,Q as radToDeg,k as round};
function V(r,t=2){let e=r;for(;e.length<t;)e=`0${e}`;return e}function J(r){return r*(Math.PI/180)}function Q(r){return r*(180/Math.PI)}function c(r,t,e){return Math.min(Math.max(r,t),e)}function _(r,t){let[e,n,o,f]=t,a=f<1?`/${k(f,5)}`:"";switch(r){case"rgb":case"rgba":return f<1?`rgba(${Math.round(e*255)}, ${Math.round(n*255)}, ${Math.round(o*255)}, ${k(f,5)})`:`rgb(${Math.round(e*255)}, ${Math.round(n*255)}, ${Math.round(o*255)})`;case"oklab":case"oklch":return`${r}(${k(e*100,6)}% ${k(n,6)} ${k(o,6)}${a})`;default:return`color(${r} ${k(e,6)} ${k(n,6)} ${k(o,6)}${a})`}}function L(r,t){let e=[...r];for(let n=0;n<t.length;n++){let o=0;for(let f=0;f<t[n].length;f++)o+=r[f]*t[n][f];e[n]=o}return e}function k(r,t=2){let e=10**t;return Math.round(r*e)/e}var he=[[.4123907992659593,.357584339383878,.1804807884018343],[.2126390058715102,.715168678767756,.0721923153607337],[.0193308187155918,.119194779794626,.9505321522496607]],be=[[3.240969941904522,-1.537383177570094,-.4986107602930034],[-.9692436362808793,1.8759675015077202,.0415550574071756],[.0556300796969937,-.2039769588889766,1.0569715142428782]],me=[[.2104542553,.793617785,-.0040720468],[1.9779984951,-2.428592205,.4505937099],[.0259040371,.7827717662,-.808675766]],d=[[4.0767416621,-3.3077115913,.2309699292],[-1.2684380046,2.6097574011,-.3413193965],[-.0041960863,-.7034186147,1.707614701]],ge=[[.4122214708,.5363325363,.0514459929],[.2119034982,.6806995451,.1073969566],[.0883024619,.2817188376,.6299787005]],pe=[[1,.39633779217376774,.2158037580607588],[1,-.10556134232365633,-.0638541747717059],[1,-.08948418209496574,-1.2914855378640917]];function Te(r,t){let e=[1/0,1/0,1/0,1/0,1/0],n=1/0,o=1/0,f=1/0;-1.88170328*r-.80936493*t>1?(e=[1.19086277,1.76576728,.59662641,.75515197,.56771245],n=4.0767416621,o=-3.3077115913,f=.2309699292):1.81444104*r-1.19445276*t>1?(e=[.73956515,-.45954404,.08285427,.1254107,.14503204],n=-1.2684380046,o=2.6097574011,f=-.3413193965):(e=[1.35733652,-.00915799,-1.1513021,-.50559606,.00692167],n=-.0041960863,o=-.7034186147,f=1.707614701);let a=e[0]+e[1]*r+e[2]*t+e[3]*r*r+e[4]*r*t,s=.3963377774*r+.2158037573*t,i=-.1055613458*r-.0638541728*t,l=-.0894841775*r-1.291485548*t;{let x=1+a*s,b=1+a*i,h=1+a*l,g=x**3,E=b**3,T=h**3,y=3*s*x**2,M=3*i*b**2,v=3*l*h**2,$=6*s**2*x,u=6*i**2*b,R=6*l**2*h,w=n*g+o*E+f*T,I=n*y+o*M+f*v,N=n*$+o*u+f*R;a=a-w*I/(I*I-.5*w*N)}return a}function Me(r,t){let e=Te(r,t),[n,o,f]=q(P([1,e*r,e*t,1])),a=Math.cbrt(1/Math.max(n,o,f)),s=a*e;return{L:a,C:s}}function ke(r,t,e,n,o){let f=Me(r,t);if((e-o)*f.C-(f.L-o)*n<=0)return f.C*o/(n*f.L+f.C*(o-e));let a=f.C*(o-1)/(n*(f.L-1)+f.C*(o-e)),s=e-o,i=n,l=.3963377774*r+.2158037573*t,x=-.1055613458*r-.0638541728*t,b=-.0894841775*r-1.291485548*t,h=s+i*l,g=s+i*x,E=s+i*b,T=o*(1-a)+a*e,y=a*n,M=T+y*l,v=T+y*x,$=T+y*b,u=[[M**3,v**3,$**3],[3*h*M**2,3*g*v**2,3*E*$**2],[6*h**2*M,6*g**2*v,6*E**2*$]],R=d[0][0]*u[0][0]+d[0][1]*u[0][1]+d[0][2]*u[0][2]-1,w=d[0][0]*u[1][0]+d[0][1]*u[1][1]+d[0][2]*u[1][2],I=d[0][0]*u[2][0]+d[0][1]*u[2][1]+d[0][2]*u[2][2],N=w/(w*w-.5*R*I),A=N>=0?-R*N:1/0,Z=d[1][0]*u[0][0]+d[1][1]*u[0][1]+d[1][2]*u[0][2]-1,p=d[1][0]*u[1][0]+d[1][1]*u[1][1]+d[1][2]*u[1][2],Re=d[1][0]*u[2][0]+d[1][1]*u[2][1]+d[1][2]*u[2][2],ue=p/(p*p-.5*Z*Re),_e=ue>=0?-Z*ue:1/0,xe=d[2][0]*u[0][0]+d[2][1]*u[0][1]+d[2][2]*u[0][2]-1,U=d[2][0]*u[1][0]+d[2][1]*u[1][1]+d[2][2]*u[1][2],Be=d[2][0]*u[2][0]+d[2][1]*u[2][1]+d[2][2]*u[2][2],de=U/(U*U-.5*xe*Be),Ge=de>=0?-xe*de:1/0;return a+Math.min(A,_e,Ge)}function W(r){let t=Math.abs(r);return t<=.0031308?r*12.92:1.055*Math.pow(t,1/2.4)-.055}function ee(r){return Math.abs(r)<=.04045?r/12.92:((Math.abs(r)+.055)/1.055)**2.4}function H(r){let[t,e,n,o]=r;t=Math.abs(t%360);let f=e*(1-Math.abs(2*n-1)),a=f*(1-Math.abs(t/60%2-1)),s=0,i=0,l=0;0<=t&&t<60?(s=f,i=a):60<=t&&t<120?(s=a,i=f):120<=t&&t<180?(i=f,l=a):180<=t&&t<240?(i=a,l=f):240<=t&&t<300?(s=a,l=f):300<=t&&t<360&&(s=f,l=a);let x=n-f/2;return[s+x,i+x,l+x,o]}function te(r){let[t,e,n,o]=r;if(e+n>=1){let a=e/(e+n);return[a,a,a,o]}let f=H([t,100,50,o]);for(let a=0;a<3;a++)f[a]*=1-e-n,f[a]+=e;return f}function we(r,t=2e-4){let[e,n,o,f]=r,a=Math.abs(n)<t&&Math.abs(o)<t?0:Q(Math.atan2(o,n));for(;a<0;)a+=360;for(;a>=360;)a-=360;return[e,Math.sqrt(n**2+o**2),a,f]}function Le(r){let[t,e,n,o]=r;if(t===0)return[0,0,0,o];for(;n<0;)n+=360;for(;n>=360;)n-=360;let f=J(n);return[t,Math.cos(f)*e,Math.sin(f)*e,o]}function Oe(r){return L(r,me)}function q(r){let[t,e,n,o]=r,[f,a,s]=L([t**3,e**3,n**3,o],d);return[f,a,s,o]}function B(r){let[t,e,n,o]=r;return[W(t),W(e),W(n),o]}function re(r){let[t,e,n,o]=L(r,ge);return[Math.cbrt(t),Math.cbrt(e),Math.cbrt(n),o]}function ne(r){return L(r,he)}function P(r){return L(r,pe)}function F(r){let[t,e,n,o]=q(P(r));if(t>1.001||t<-.001||e>1.001||e<-.001||n>1.001||n<-.001){let[a,s,i]=r,l=Math.max(1e-5,Math.sqrt(s**2+i**2)),x=c(a,0,1),b=s/l,h=i/l,g=ke(b,h,a,l,x);return B(q(P([x*(1-g)+g*a,b*(g*l),h*(g*l),o])))}return B([t,e,n,o])}function D(r){return F(Le(r))}function G(r){let[t,e,n,o]=r;return[ee(t),ee(e),ee(n),o]}function z(r){return Oe(re(G(r)))}function S(r){return we(z(r))}function oe(r){return L(r,be)}function j(r,t){let[e,n,o,f]=S(r);return typeof t.lightness=="number"&&(t.mode==="relative"?e+=t.lightness:e=t.lightness),typeof t.chroma=="number"&&(t.mode==="relative"?n+=t.chroma:n=t.chroma),typeof t.hue=="number"&&(t.mode==="relative"?o+=t.hue:o=t.hue),typeof t.alpha=="number"&&(t.mode==="relative"?f+=t.alpha:f=t.alpha),D([e,n,o,f])}var ve={black:0,silver:12632256,gray:8421504,white:16777215,maroon:8388608,red:16711680,purple:8388736,fuchsia:16711935,green:32768,lime:65280,olive:8421376,yellow:16776960,navy:128,blue:255,teal:32896,aqua:65535,orange:16753920,aliceblue:15792383,antiquewhite:16444375,aquamarine:8388564,azure:15794175,beige:16119260,bisque:16770244,blanchedalmond:16772045,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,gainsboro:14474460,ghostwhite:16316671,gold:16766720,goldenrod:14329120,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,limegreen:3329330,linen:16445670,magenta:16711935,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,oldlace:16643558,olivedrab:7048739,orangered:16729344,orchid:14315734,palegoldenrod:15657130,palegreen:10025880,paleturquoise:11529966,palevioletred:14381203,papayawhip:16773077,peachpuff:16767673,peru:13468991,pink:16761035,plum:14524637,powderblue:11591910,rosybrown:12357519,royalblue:4286945,saddlebrown:9127187,salmon:16416882,sandybrown:16032864,seagreen:3050327,seashell:16774638,sienna:10506797,skyblue:8900331,slateblue:6970061,slategray:7372944,slategrey:7372944,snow:16775930,springgreen:65407,steelblue:4620980,tan:13808780,thistle:14204888,tomato:16737095,turquoise:4251856,violet:15631086,wheat:16113331,whitesmoke:16119285,yellowgreen:10145074,rebeccapurple:6697881},ae=ve;var $e=/-?[0-9.]+%?/g,Ie=/^#[0-9a-f]{3,8}$/i,Ae=/^([^( ]+)\s*\(\s*([^)]+)\)$/,De=16**6,fe=16**4,se=16**2;function m(r){let t=Se(r),e={get hex(){let[n,o,f,a]=t,s="#";return s+=V(Math.round(c(n*255,0,255)).toString(16),2),s+=V(Math.round(c(o*255,0,255)).toString(16),2),s+=V(Math.round(c(f*255,0,255)).toString(16),2),t[3]<1&&(s+=V(Math.round(a*255).toString(16),2)),s},get hexVal(){let[n,o,f,a]=t;return a<1&&console.warn(`hexVal converted a semi-transparent color (${a*100}%) to fully opaque`),n=Math.round(c(n*255,0,255)),o=Math.round(c(o*255,0,255)),f=Math.round(c(f*255,0,255)),n*fe+o*se+f},get rgb(){return _("rgb",t)},rgbVal:t,get rgba(){return _("rgb",t)},rgbaVal:t,get linearRGB(){return _("srgb-linear",G(t))},get linearRGBVal(){return G(t)},get p3(){return _("display-p3",t)},p3Val:t,get oklab(){return _("oklab",z(t))},get oklabVal(){return z(t)},get oklch(){return _("oklch",S(t))},get oklchVal(){return S(t)},get xyz(){return _("xyz-d65",ne(G(t)))},get xyzVal(){return ne(G(t))},adjust(n){return m(j(t,n))}};return e.toString=()=>e.hex,e}function ye(r){if(r>De)throw new Error("8-digit hex values (with transparency) aren\u2019t supported");let t=r,e=Math.floor(t/fe);t-=e*fe;let n=Math.floor(t/se);t-=n*se;let o=t;return[e/255,n/255,o/255,1]}function O(r,t){let e=r.match($e);if(!e)throw new Error(`Unexpected color format: ${r}`);let n=[0,0,0,1];for(let o=0;o<e.length;o++)e[o]&&(e[o].includes("%")?n[o]=parseFloat(e[o])/100:!t||t[o]===1/0||t[o]===1?n[o]=parseFloat(e[o]):n[o]=parseFloat(e[o])/t[o]);return n}function Se(r){let t=new Error(`Unable to parse color ${JSON.stringify(r)}`);if(r==null||r==null||typeof r=="boolean")throw t;if(Array.isArray(r)){if(r.some(a=>typeof a!="number"))throw new Error(`Color array must be numbers, received ${r}`);if(r.length<3||r.length>4)throw new Error(`Expected [R, G, B, A?], received ${r}`);let[e,n,o,f]=r;return[c(e,0,1),c(n,0,1),c(o,0,1),typeof f=="number"?c(f,0,1):1]}if(typeof r=="number")return ye(r);if(typeof r=="string"){let e=r.trim();if(!e)throw new Error("Expected color, received empty string");let n=e.toLowerCase();if(typeof ae[n]=="number")return ye(ae[n]);if(Ie.test(n)){let s=n.replace("#",""),i=[0,0,0,1];if(s.length===6||s.length===8)for(let l=0;l<s.length/2;l++){let x=l*2,b=x+2,h=s.substring(x,b);i[l]=parseInt(h,16)/255}else if(s.length===3||s.length===4)for(let l=0;l<s.length;l++){let x=s.charAt(l);i[l]=parseInt(`${x}${x}`,16)/255}else throw new Error(`Hex value "${n}" not a valid sRGB color`);return i}let o=e.match(Ae);if(!o)throw t;let[,f,a]=o;if(f==="color"){let s=a.indexOf(" ");f=a.substring(0,s),a=a.substring(s)}switch(f){case"rgb":case"rgba":case"srgb":{let[s,i,l,x]=O(a,[255,255,255,1]);return[c(s,0,1),c(i,0,1),c(l,0,1),c(x,0,1)]}case"linear-rgb":case"linear-srgb":case"rgb-linear":case"srgb-linear":{let s=O(a);return B(s)}case"hsl":case"hsla":{let[s,i,l,x]=O(a);return H([s,c(i,0,1),c(l,0,1),c(x,0,1)])}case"hwb":case"hwba":{let[s,i,l,x]=O(a);return te([s,c(i,0,1),c(l,0,1),c(x,0,1)])}case"p3":case"display-p3":{let[s,i,l,x]=O(a);return[c(s,0,1),c(i,0,1),c(l,0,1),c(x,0,1)]}case"lab":case"oklab":return F(O(a));case"lch":case"oklch":return D(O(a));case"xyz":case"xyz-d65":return B(oe(O(a)))}}if(typeof r=="object"){let e={...r},n=1;for(let o of Object.keys(e))o==="alpha"?n=c(e[o],0,1):e[o.toLowerCase()]=e[o];if("r"in e&&"g"in e&&"b"in e)return[c(e.r,0,1),c(e.g,0,1),c(e.b,0,1),n];if("h"in e&&"s"in e&&"l"in e)return H([e.h,c(e.s,0,1),c(e.l,0,1),n]);if("h"in e&&"w"in e&&"b"in e)return te([e.h,c(e.w,0,1),c(e.b,0,1),n]);if("l"in e&&"a"in e&&"b"in e)return F([e.l,e.a,e.b,n]);if("l"in e&&"c"in e&&"h"in e)return D([e.l,e.c,e.h,n]);if("x"in e&&"y"in e&&"z"in e)return B(oe([e.x,e.y,e.z,n]));throw t}throw t}function X(r,t,e=.5,n="oklab"){let o=c(e,0,1);if(e===0)return m(r);if(e===1)return m(t);let f=1-o,a=o,s={oklch:S,oklab:z,lms:p=>G(re(p)),linearRGB:G,sRGB:p=>p},i={oklch:D,oklab:F,lms:p=>q(B(p)),linearRGB:B,sRGB:p=>p},l=s[n],x=i[n];if(!l)throw new Error(`Unknown color space "${n}", try "oklab", "oklch", "linearRGB", or "sRGB"`);let[b,h,g,E]=m(r).rgbVal,[T,y,M,v]=m(t).rgbVal;n==="oklch"&&(b===h&&h===g||T===y&&y===M)&&(l=s.oklab,x=i.oklab);let[$,u,R,w]=l([b,h,g,E]),[I,N,A,Z]=l([T,y,M,v]);return n==="oklch"&&Math.abs(A-R)>180&&(Math.max(R,A)===A?A-=360:R-=360),m(x([$*f+I*a,u*f+N*a,R*f+A*a,w*f+Z*a]))}function K(r,t,e="oklab"){let n=c(t,-1,1);return n>=0?X(r,"black",n,e):C(r,-n)}function C(r,t,e="oklab"){let n=c(t,-1,1);return n>=0?X(r,"white",n,e):K(r,-n)}function Y(r){return m(r).xyzVal[1]}function le(r){return k(m(r).oklabVal[0],5)}function ce(r){return Y(r)<.36?"dark":"light"}function ie(r,t){let e=Y(r),n=Y(t),o=Math.max(e,n),f=Math.min(e,n),a=(o+.05)/(f+.05);return{ratio:a,AA:a>=4.5,AAA:a>=7}}var c0={adjust:j,contrastRatio:ie,darken:K,from:m,lighten:C,lightness:le,lightOrDark:ce,luminance:Y,mix:X};export{j as adjust,c as clamp,_ as colorFn,ie as contrastRatio,K as darken,c0 as default,J as degToRad,m as from,V as leftPad,ce as lightOrDark,C as lighten,le as lightness,Y as luminance,X as mix,L as multiplyColorMatrix,Q as radToDeg,k as round};

@@ -235,5 +235,7 @@ import adjust from './adjust.js';

// }
case 'lab':
case 'oklab': {
return oklabTosRGB(parseValueStr(valueStr));
}
case 'lch':
case 'oklch': {

@@ -240,0 +242,0 @@ return oklchTosRGB(parseValueStr(valueStr));

{
"name": "better-color-tools",
"description": "Fast, minimal color conversion and tools for JS/Sass. Supports sRGB, Oklab, Oklch, Display P3, and more.",
"version": "0.12.0",
"version": "0.12.1",
"author": {

@@ -33,3 +33,3 @@ "name": "Drew Powers",

"dev": "tsc -w",
"lint": "eslint \"**/*.{js,ts}\"",
"lint": "eslint \"src/**/*.{js,ts}\"",
"prepublish": "npm run build",

@@ -36,0 +36,0 @@ "test": "vitest run",

@@ -54,4 +54,6 @@ # better-color-tools

This library understands **any CSS-valid color**, including [CSS Color Module 4](https://www.w3.org/TR/css-color-4/) (but if some aspect isn’t implemented yet, please request it!).
This library understands **any CSS-valid color**, including [CSS Color Module 4\*](https://www.w3.org/TR/css-color-4/) (but if some aspect isn’t implemented yet, please request it!).
_\* With the exception that `lab()` and `lch()` CSS Module 4 colors will be interpreted as `oklab()` and `oklch()`, respectively._
[Playground](https://better-color-tools.pages.dev/)

@@ -58,0 +60,0 @@

@@ -325,5 +325,7 @@ import type { AdjustOptions } from './adjust.js';

// }
case 'lab':
case 'oklab': {
return oklabTosRGB(parseValueStr(valueStr));
}
case 'lch':
case 'oklch': {

@@ -330,0 +332,0 @@ return oklchTosRGB(parseValueStr(valueStr));

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc