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.7.2 to 0.7.3

6

CHANGELOG.md
# better-color-tools
## 0.7.3
### Patch Changes
- b09be1a: Fix Oklab/Oklch color() function output
## 0.7.2

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

2

dist/index.min.js

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

function m(t,e=2){let o=t;for(;o.length<e;)o=`0${o}`;return o}function X(t){return t*(Math.PI/180)}function U(t){return t*(180/Math.PI)}function l(t,e,o){return Math.min(Math.max(t,e),o)}function h(t,e){let[o,n,a,r]=e,f=r<1?`/${u(r,5)}`:"";switch(t){case"rgb":case"rgba":return r<1?`rgba(${Math.round(o*255)}, ${Math.round(n*255)}, ${Math.round(a*255)}, ${u(r,5)})`:`rgb(${Math.round(o*255)}, ${Math.round(n*255)}, ${Math.round(a*255)})`;case"oklab":case"oklch":return`color(${t} ${u(o*100,5)}% ${u(n,5)} ${u(a,5)}${f}`;default:return`color(${t} ${u(o,5)} ${u(n,5)} ${u(a,5)}${f})`}}function p(t,e){let o=[...t];for(let n=0;n<e.length;n++){let a=0;for(let r=0;r<e[0].length;r++)a+=t[r]*e[n][r];o[n]=a}return o}function u(t,e=2){let o=10**e;return Math.round(t*o)/o}var Y=216/24389,z=24389/27,M=[95.47,100,108.33],Z=M[0]+15*M[1]+3*M[2],H=4*M[0]/Z,K=9*M[1]/Z,oe=[[.2104542553,.793617785,-.0040720468],[1.9779984951,-2.428592205,.4505937099],[.0259040371,.7827717662,-.808675766]],ne=[[4.0767416621,-3.3077115913,.2309699292],[-1.2684380046,2.6097574011,-.3413193965],[-.0041960863,-.7034186147,1.707614701]],ae=[[.4124,.3576,.1805],[.2126,.7152,.0722],[.0193,.1192,.9505]],fe=[[.4122214708,.5363325363,.0514459929],[.2119034982,.6806995451,.1073969566],[.0883024619,.2817188376,.6299787005]],se=[[1,.3963377774,.2158037573],[1,-.1055613458,-.0638541728],[1,-.0894841775,-1.291485548]],le=[[3.2406,-1.5372,-.4986],[-.9689,1.8758,.0415],[.0557,-.204,1.057]];function q(t){let[e,o,n,a]=t;e=Math.abs(e%360);let r=o*(1-Math.abs(2*n-1)),f=r*(1-Math.abs(e/60%2-1)),s=0,c=0,i=0;0<=e&&e<60?(s=r,c=f):60<=e&&e<120?(s=f,c=r):120<=e&&e<180?(c=r,i=f):180<=e&&e<240?(c=f,i=r):240<=e&&e<300?(s=f,i=r):300<=e&&e<360&&(s=r,i=f);let x=n-r/2;return[s+x,c+x,i+x,a]}function j(t){let[e,o,n,a]=t;if(o+n>=1){let f=o/(o+n);return[f,f,f,a]}let r=q([e,100,50,a]);for(let f=0;f<3;f++)r[f]*=1-o-n,r[f]+=o;return r}function ce(t){let[e,o,n,a]=t,r=o===0&&n===0?0:U(Math.atan2(n,o));return[e,Math.sqrt(o**2+n**2),r<0?r+360:r,a]}function ie(t){let[e,o,n,a]=t,r=X(n);return[e,o*Math.cos(r),o*Math.sin(r),a]}function xe(t){let e=Math.cbrt(t[0]),o=Math.cbrt(t[1]),n=Math.cbrt(t[2]),a=t[3];return p([e,o,n,a],oe)}function ue(t){let[e,o,n,a]=p(t,ne);return[Math.max(0,e),Math.max(0,o),Math.max(0,n),Math.max(0,a)]}function k(t){return t.map((e,o)=>o===3?l(e,0,1):e<=.0031308?l(e*12.92,0,1):l(1.055*Math.pow(e,1/2.4)-.055,0,1))}function de(t){return p(t,fe)}function he(t){let[e,o,n,a]=t;e*=100,o*=100,n*=100;let r=e>Y*z?((e+16)/116)**3:e/z,f=(52*e/(o+13*e*H)-1)/3||0,s=-5*r||0,c=-1/3,x=((r*(39*e/(n+13*e*K)-5)||0)-s)/(f-c)||0,g=x*f+s;return[x,r,g,a]}function $(t){return F(he(t))}function be(t){let e=p(t,se);return[e[0]**3,e[1]**3,e[2]**3,e[3]]}function w(t){return k(ue(be(t)))}function E(t){return w(ie(t))}function R(t){return t.map((e,o)=>o===3?e:e<=.04045?e/12.92:((e+.055)/1.055)**2.4)}function v(t){return ge(O(t))}function y(t){return xe(de(R(t)))}function _(t){return ce(y(t))}function O(t){return p(R(t),ae)}function F(t){return k(p(t,le))}function ge(t){let[e,o,n,a]=t,r=e+15*o+3*n,f=4*e/r||0,s=9*o/r||0,c=e>Y?116*Math.pow(o,1/3)-16:z*o,i=13*c*(f-H)||0,x=13*c*(s-K)||0;return[c/100,i/100,x/100,a]}var pe={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},N=pe;var me=/-?[0-9.]+%?/g,ke=/^#?[0-9a-f]{3,8}$/i,Re=16**6,D=16**4,S=16**2;function d(t){let e=ye(t),o={get hex(){let n="#";return n+=m(Math.round(e[0]*255).toString(16),2),n+=m(Math.round(e[1]*255).toString(16),2),n+=m(Math.round(e[2]*255).toString(16),2),e[3]<1&&(n+=m(Math.round(e[3]*255).toString(16),2)),n},get hexVal(){e[3]<1&&console.warn(`hexVal converted a semi-transparent color (${e[3]*100}%) to fully opaque`);let n=Math.round(e[0]*255),a=Math.round(e[1]*255),r=Math.round(e[2]*255);return n*D+a*S+r},get luv(){return h("luv",v(e))},get luvVal(){return v(e)},get rgb(){return h("rgb",e)},rgbVal:e,get rgba(){return h("rgb",e)},rgbaVal:e,get linearRGB(){return R(e)},get p3(){return h("display-p3",e)},p3Val:e,get oklab(){return h("oklab",y(e))},get oklabVal(){return y(e)},get oklch(){return h("oklch",_(e))},get oklchVal(){return _(e)},get xyz(){return h("xyz-d65",O(e))},get xyzVal(){return O(e)}};return o.toString=()=>o.hex,o}function W(t){if(t>Re)throw new Error("better-color-tools can\u2019t parse hex numbers with alpha (0x0000000 is indistinguishable from 0x00000000). Please use hex string, or another color method");let e=t,o=Math.floor(e/D);e-=o*D;let n=Math.floor(e/S);e-=n*S;let a=e;return[o/255,n/255,a/255,1]}function b(t,e){let o=t.match(me);if(!o)throw new Error(`Unexpected color format: ${t}`);let n=[0,0,0,1];return o.forEach((a,r)=>{a.includes("%")?n[r]=parseFloat(a)/100:e[r]===1/0?n[r]=parseFloat(a):n[r]=parseFloat(a)/(e[r]||1)}),n}function ye(t){if(Array.isArray(t)){if(t.some(e=>typeof e!="number"))throw new Error(`Color array must be numbers, received ${t}`);if(t.length<3||t.length>4)throw new Error(`Expected [R, G, B, A?], received ${t}`);return[l(t[0],0,1),l(t[1],0,1),l(t[2],0,1),typeof t[3]=="number"?l(t[3],0,1):1]}if(typeof t=="number")return W(t);if(typeof t=="string"){let e=t.trim();if(!e)throw new Error("Expected color, received empty string");let o=e.toLowerCase();if(typeof N[o]=="number")return W(N[o]);if(ke.test(e)){let r=e.replace("#",""),f=[0,0,0,1];if(r.length>=6)for(let s=0;s<r.length/2;s++){let c=s*2,i=c+2,x=r.substring(c,i);f[s]=parseInt(x,16)/255}else for(let s=0;s<r.length;s++){let c=r.charAt(s);f[s]=parseInt(`${c}${c}`,16)/255}return f}let[n,a]=e.split("(");if(n==="color"){let r=a.indexOf(" ");n=a.substring(0,r),a=a.substring(r)}switch(n){case"rgb":case"rgba":case"srgb":{let[r,f,s,c]=b(a,[255,255,255,1]);return[l(r,0,1),l(f,0,1),l(s,0,1),l(c,0,1)]}case"srgb-linear":{let[r,f,s,c]=b(a,[255,255,255,1]);return k([l(r,0,1),l(f,0,1),l(s,0,1),l(c,0,1)])}case"hsl":case"hsla":{let[r,f,s,c]=b(a,[1/0,1,1,1]);return q([r,l(f,0,1),l(s,0,1),l(c,0,1)])}case"hwb":case"hwba":{let[r,f,s,c]=b(a,[1/0,1,1,1]);return j([r,l(f,0,1),l(s,0,1),l(c,0,1)])}case"p3":case"display-p3":{let[r,f,s,c]=b(a,[1,1,1,1]);return[l(r,0,1),l(f,0,1),l(s,0,1),l(c,0,1)]}case"luv":{let r=b(a,[1,1,1,1]);return $(r)}case"oklab":return w(b(a,[1,1,1,1]));case"oklch":return E(b(a,[1,1,1/0,1]));case"xyz":case"xyz-d65":return F(b(a,[1,1,1,1]))}}throw new Error(`Unable to parse color "${t}"`)}function P(t){return u(d(t).oklabVal[0],5)}function B(t,e,o=.5,n="oklab"){let a=l(o,0,1);if(o===0)return d(t);if(o===1)return d(e);let r=1-a,f=a,s={oklch:_,oklab:y,luv:v,linearRGB:R,sRGB:V=>V},c={oklch:E,oklab:w,luv:$,linearRGB:k,sRGB:V=>V},i=s[n],x=c[n];if(!i)throw new Error(`Unknown color space "${n}", try "oklab", "oklch", "linearRGB", or "sRGB"`);let g=d(t).rgbVal,G=d(e).rgbVal;n==="oklch"&&(g[0]===g[1]&&g[1]===g[2]||G[0]===G[1]&&G[1]===G[2])&&(i=s.oklab,x=c.oklab);let[J,Q,L,C]=i(g),[ee,te,T,re]=i(G);return n==="oklch"&&Math.abs(T-L)>180&&(Math.max(L,T)===T?T-=360:L-=360),d(x([J*r+ee*f,Q*r+te*f,L*r+T*f,C*r+re*f]))}function A(t,e,o="oklab"){let n=l(e,-1,1);return n>=0?B(t,"black",n,o):I(t,-n)}function I(t,e,o="oklab"){let n=l(e,-1,1);return n>=0?B(t,"white",n,o):A(t,-n)}var Se={darken:A,from:d,lighten:I,lightness:P,mix:B};export{l as clamp,h as colorFn,A as darken,Se as default,d as from,m as leftPad,I as lighten,P as lightness,B as mix,u as round};
function m(t,e=2){let o=t;for(;o.length<e;)o=`0${o}`;return o}function X(t){return t*(Math.PI/180)}function U(t){return t*(180/Math.PI)}function l(t,e,o){return Math.min(Math.max(t,e),o)}function h(t,e){let[o,n,a,r]=e,f=r<1?`/${u(r,5)}`:"";switch(t){case"rgb":case"rgba":return r<1?`rgba(${Math.round(o*255)}, ${Math.round(n*255)}, ${Math.round(a*255)}, ${u(r,5)})`:`rgb(${Math.round(o*255)}, ${Math.round(n*255)}, ${Math.round(a*255)})`;case"oklab":case"oklch":return`color(${t} ${u(o*100,5)}% ${u(n,5)} ${u(a,5)}${f})`;default:return`color(${t} ${u(o,5)} ${u(n,5)} ${u(a,5)}${f})`}}function p(t,e){let o=[...t];for(let n=0;n<e.length;n++){let a=0;for(let r=0;r<e[0].length;r++)a+=t[r]*e[n][r];o[n]=a}return o}function u(t,e=2){let o=10**e;return Math.round(t*o)/o}var Y=216/24389,z=24389/27,M=[95.47,100,108.33],Z=M[0]+15*M[1]+3*M[2],H=4*M[0]/Z,K=9*M[1]/Z,oe=[[.2104542553,.793617785,-.0040720468],[1.9779984951,-2.428592205,.4505937099],[.0259040371,.7827717662,-.808675766]],ne=[[4.0767416621,-3.3077115913,.2309699292],[-1.2684380046,2.6097574011,-.3413193965],[-.0041960863,-.7034186147,1.707614701]],ae=[[.4124,.3576,.1805],[.2126,.7152,.0722],[.0193,.1192,.9505]],fe=[[.4122214708,.5363325363,.0514459929],[.2119034982,.6806995451,.1073969566],[.0883024619,.2817188376,.6299787005]],se=[[1,.3963377774,.2158037573],[1,-.1055613458,-.0638541728],[1,-.0894841775,-1.291485548]],le=[[3.2406,-1.5372,-.4986],[-.9689,1.8758,.0415],[.0557,-.204,1.057]];function q(t){let[e,o,n,a]=t;e=Math.abs(e%360);let r=o*(1-Math.abs(2*n-1)),f=r*(1-Math.abs(e/60%2-1)),s=0,c=0,i=0;0<=e&&e<60?(s=r,c=f):60<=e&&e<120?(s=f,c=r):120<=e&&e<180?(c=r,i=f):180<=e&&e<240?(c=f,i=r):240<=e&&e<300?(s=f,i=r):300<=e&&e<360&&(s=r,i=f);let x=n-r/2;return[s+x,c+x,i+x,a]}function j(t){let[e,o,n,a]=t;if(o+n>=1){let f=o/(o+n);return[f,f,f,a]}let r=q([e,100,50,a]);for(let f=0;f<3;f++)r[f]*=1-o-n,r[f]+=o;return r}function ce(t){let[e,o,n,a]=t,r=o===0&&n===0?0:U(Math.atan2(n,o));return[e,Math.sqrt(o**2+n**2),r<0?r+360:r,a]}function ie(t){let[e,o,n,a]=t,r=X(n);return[e,o*Math.cos(r),o*Math.sin(r),a]}function xe(t){let e=Math.cbrt(t[0]),o=Math.cbrt(t[1]),n=Math.cbrt(t[2]),a=t[3];return p([e,o,n,a],oe)}function ue(t){let[e,o,n,a]=p(t,ne);return[Math.max(0,e),Math.max(0,o),Math.max(0,n),Math.max(0,a)]}function k(t){return t.map((e,o)=>o===3?l(e,0,1):e<=.0031308?l(e*12.92,0,1):l(1.055*Math.pow(e,1/2.4)-.055,0,1))}function de(t){return p(t,fe)}function he(t){let[e,o,n,a]=t;e*=100,o*=100,n*=100;let r=e>Y*z?((e+16)/116)**3:e/z,f=(52*e/(o+13*e*H)-1)/3||0,s=-5*r||0,c=-1/3,x=((r*(39*e/(n+13*e*K)-5)||0)-s)/(f-c)||0,g=x*f+s;return[x,r,g,a]}function $(t){return F(he(t))}function be(t){let e=p(t,se);return[e[0]**3,e[1]**3,e[2]**3,e[3]]}function w(t){return k(ue(be(t)))}function E(t){return w(ie(t))}function R(t){return t.map((e,o)=>o===3?e:e<=.04045?e/12.92:((e+.055)/1.055)**2.4)}function v(t){return ge(O(t))}function y(t){return xe(de(R(t)))}function _(t){return ce(y(t))}function O(t){return p(R(t),ae)}function F(t){return k(p(t,le))}function ge(t){let[e,o,n,a]=t,r=e+15*o+3*n,f=4*e/r||0,s=9*o/r||0,c=e>Y?116*Math.pow(o,1/3)-16:z*o,i=13*c*(f-H)||0,x=13*c*(s-K)||0;return[c/100,i/100,x/100,a]}var pe={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},N=pe;var me=/-?[0-9.]+%?/g,ke=/^#?[0-9a-f]{3,8}$/i,Re=16**6,D=16**4,S=16**2;function d(t){let e=ye(t),o={get hex(){let n="#";return n+=m(Math.round(e[0]*255).toString(16),2),n+=m(Math.round(e[1]*255).toString(16),2),n+=m(Math.round(e[2]*255).toString(16),2),e[3]<1&&(n+=m(Math.round(e[3]*255).toString(16),2)),n},get hexVal(){e[3]<1&&console.warn(`hexVal converted a semi-transparent color (${e[3]*100}%) to fully opaque`);let n=Math.round(e[0]*255),a=Math.round(e[1]*255),r=Math.round(e[2]*255);return n*D+a*S+r},get luv(){return h("luv",v(e))},get luvVal(){return v(e)},get rgb(){return h("rgb",e)},rgbVal:e,get rgba(){return h("rgb",e)},rgbaVal:e,get linearRGB(){return R(e)},get p3(){return h("display-p3",e)},p3Val:e,get oklab(){return h("oklab",y(e))},get oklabVal(){return y(e)},get oklch(){return h("oklch",_(e))},get oklchVal(){return _(e)},get xyz(){return h("xyz-d65",O(e))},get xyzVal(){return O(e)}};return o.toString=()=>o.hex,o}function W(t){if(t>Re)throw new Error("better-color-tools can\u2019t parse hex numbers with alpha (0x0000000 is indistinguishable from 0x00000000). Please use hex string, or another color method");let e=t,o=Math.floor(e/D);e-=o*D;let n=Math.floor(e/S);e-=n*S;let a=e;return[o/255,n/255,a/255,1]}function b(t,e){let o=t.match(me);if(!o)throw new Error(`Unexpected color format: ${t}`);let n=[0,0,0,1];return o.forEach((a,r)=>{a.includes("%")?n[r]=parseFloat(a)/100:e[r]===1/0?n[r]=parseFloat(a):n[r]=parseFloat(a)/(e[r]||1)}),n}function ye(t){if(Array.isArray(t)){if(t.some(e=>typeof e!="number"))throw new Error(`Color array must be numbers, received ${t}`);if(t.length<3||t.length>4)throw new Error(`Expected [R, G, B, A?], received ${t}`);return[l(t[0],0,1),l(t[1],0,1),l(t[2],0,1),typeof t[3]=="number"?l(t[3],0,1):1]}if(typeof t=="number")return W(t);if(typeof t=="string"){let e=t.trim();if(!e)throw new Error("Expected color, received empty string");let o=e.toLowerCase();if(typeof N[o]=="number")return W(N[o]);if(ke.test(e)){let r=e.replace("#",""),f=[0,0,0,1];if(r.length>=6)for(let s=0;s<r.length/2;s++){let c=s*2,i=c+2,x=r.substring(c,i);f[s]=parseInt(x,16)/255}else for(let s=0;s<r.length;s++){let c=r.charAt(s);f[s]=parseInt(`${c}${c}`,16)/255}return f}let[n,a]=e.split("(");if(n==="color"){let r=a.indexOf(" ");n=a.substring(0,r),a=a.substring(r)}switch(n){case"rgb":case"rgba":case"srgb":{let[r,f,s,c]=b(a,[255,255,255,1]);return[l(r,0,1),l(f,0,1),l(s,0,1),l(c,0,1)]}case"srgb-linear":{let[r,f,s,c]=b(a,[255,255,255,1]);return k([l(r,0,1),l(f,0,1),l(s,0,1),l(c,0,1)])}case"hsl":case"hsla":{let[r,f,s,c]=b(a,[1/0,1,1,1]);return q([r,l(f,0,1),l(s,0,1),l(c,0,1)])}case"hwb":case"hwba":{let[r,f,s,c]=b(a,[1/0,1,1,1]);return j([r,l(f,0,1),l(s,0,1),l(c,0,1)])}case"p3":case"display-p3":{let[r,f,s,c]=b(a,[1,1,1,1]);return[l(r,0,1),l(f,0,1),l(s,0,1),l(c,0,1)]}case"luv":{let r=b(a,[1,1,1,1]);return $(r)}case"oklab":return w(b(a,[1,1,1,1]));case"oklch":return E(b(a,[1,1,1/0,1]));case"xyz":case"xyz-d65":return F(b(a,[1,1,1,1]))}}throw new Error(`Unable to parse color "${t}"`)}function P(t){return u(d(t).oklabVal[0],5)}function B(t,e,o=.5,n="oklab"){let a=l(o,0,1);if(o===0)return d(t);if(o===1)return d(e);let r=1-a,f=a,s={oklch:_,oklab:y,luv:v,linearRGB:R,sRGB:V=>V},c={oklch:E,oklab:w,luv:$,linearRGB:k,sRGB:V=>V},i=s[n],x=c[n];if(!i)throw new Error(`Unknown color space "${n}", try "oklab", "oklch", "linearRGB", or "sRGB"`);let g=d(t).rgbVal,G=d(e).rgbVal;n==="oklch"&&(g[0]===g[1]&&g[1]===g[2]||G[0]===G[1]&&G[1]===G[2])&&(i=s.oklab,x=c.oklab);let[J,Q,L,C]=i(g),[ee,te,T,re]=i(G);return n==="oklch"&&Math.abs(T-L)>180&&(Math.max(L,T)===T?T-=360:L-=360),d(x([J*r+ee*f,Q*r+te*f,L*r+T*f,C*r+re*f]))}function A(t,e,o="oklab"){let n=l(e,-1,1);return n>=0?B(t,"black",n,o):I(t,-n)}function I(t,e,o="oklab"){let n=l(e,-1,1);return n>=0?B(t,"white",n,o):A(t,-n)}var Se={darken:A,from:d,lighten:I,lightness:P,mix:B};export{l as clamp,h as colorFn,A as darken,Se as default,d as from,m as leftPad,I as lighten,P as lightness,B as mix,u as round};

@@ -33,3 +33,3 @@ /** you know it, you love it */

case 'oklch': {
return `color(${colorSpace} ${round(x * 100, 5)}% ${round(y, 5)} ${round(z, 5)}${alphaSlash}`;
return `color(${colorSpace} ${round(x * 100, 5)}% ${round(y, 5)} ${round(z, 5)}${alphaSlash})`;
}

@@ -36,0 +36,0 @@ // color(display-p3 0.4 0.2 0.6)

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

@@ -6,0 +6,0 @@ "name": "Drew Powers",

@@ -40,3 +40,3 @@ import type { sRGB } from './index';

case 'oklch': {
return `color(${colorSpace} ${round(x * 100, 5)}% ${round(y, 5)} ${round(z, 5)}${alphaSlash}`;
return `color(${colorSpace} ${round(x * 100, 5)}% ${round(y, 5)} ${round(z, 5)}${alphaSlash})`;
}

@@ -43,0 +43,0 @@ // color(display-p3 0.4 0.2 0.6)

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