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.6.1 to 0.6.2

6

CHANGELOG.md
# better-color-tools
## 0.6.2
### Patch Changes
- 59acc29: fix parsing bug for hex integers
## 0.6.1

@@ -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 I(t){return t*(Math.PI/180)}function V(t){return t*(180/Math.PI)}function s(t,e,o){return Math.min(Math.max(t,e),o)}function B(t,e){return`color(${t} ${f(e[0],5)} ${f(e[1],5)} ${f(e[2],5)}${e[3]<1?`/${f(e[3],5)}`:""})`}function d(t,e){let o=[...t];for(let r=0;r<e.length;r++){let i=0;for(let n=0;n<e[0].length;n++)i+=t[n]*e[r][n];o[r]=i}return o}function f(t,e=2){let o=10**e;return Math.round(t*o)/o}function L(t){let e=`${Math.round(t[0]*255)}, ${Math.round(t[1]*255)}, ${Math.round(t[2]*255)}`;return t[3]!==1?`rgba(${e}, ${f(t[3],5)})`:`rgb(${e})`}var Y=[[.2104542553,.793617785,-.0040720468],[1.9779984951,-2.428592205,.4505937099],[.0259040371,.7827717662,-.808675766]],Z=[[4.0767416621,-3.3077115913,.2309699292],[-1.2684380046,2.6097574011,-.3413193965],[-.0041960863,-.7034186147,1.707614701]];var H=[[.4122214708,.5363325363,.0514459929],[.2119034982,.6806995451,.1073969566],[.0883024619,.2817188376,.6299787005]],U=[[1,.3963377774,.2158037573],[1,-.1055613458,-.0638541728],[1,-.0894841775,-1.291485548]],K=[[3.2406,-1.5372,-.4986],[-.9689,1.8758,.0415],[.0557,-.204,1.057]];function q(t){let[e,o,r,i]=t;e=Math.abs(e%360);let n=o*(1-Math.abs(2*r-1)),a=n*(1-Math.abs(e/60%2-1)),u=0,l=0,c=0;0<=e&&e<60?(u=n,l=a):60<=e&&e<120?(u=a,l=n):120<=e&&e<180?(l=n,c=a):180<=e&&e<240?(l=a,c=n):240<=e&&e<300?(u=a,c=n):300<=e&&e<360&&(u=n,c=a);let p=r-n/2;return[u+p,l+p,c+p,i]}function j(t){let[e,o,r,i]=t,n=o===0&&r===0?0:V(Math.atan2(r,o));return[e,Math.sqrt(o**2+r**2),n<0?n+360:n,i]}function J(t){let[e,o,r,i]=t,n=I(r);return[e,o*Math.cos(n),o*Math.sin(n),i]}function Q(t){let e=Math.cbrt(t[0]),o=Math.cbrt(t[1]),r=Math.cbrt(t[2]),i=t[3];return d([e,o,r,i],Y)}function W(t){let[e,o,r,i]=d(t,Z);return[Math.max(0,e),Math.max(0,o),Math.max(0,r),Math.max(0,i)]}function b(t){return t.map((e,o)=>o===3?s(e,0,1):e<=.0031308?s(e*12.92,0,1):s(1.055*e**(1/2.4)-.055,0,1))}function C(t){return d(t,H)}function e0(t){let[e,o,r,i]=t,n=[0,0,0,i],a=o/(13*e),u=r/(13*e);return n[1]=e<=8?e*(3/29)**3:((e+16)/116)**3,n[0]=n[1]*(9/4)*a,n[2]=n[1]*((12-3*a-20*u)/4*u),n}function F(t){return b(r0(e0(t)))}function t0(t){let e=d(t,U);return[e[0]**3,e[1]**3,e[2]**3,e[3]]}function G(t){return b(W(t0(t)))}function _(t){return G(J(t))}function T(t){return t.map((e,o)=>o===3?e:e<=.04045?e/12.92:((e+.055)/1.055)**2.4)}function k(t){return Q(C(T(t)))}function M(t){return j(k(t))}function r0(t){return d(t,K)}var n0={black:[0,0,0,1],silver:[.75,.75,.75,1],gray:[.5,.5,.5,1],white:[1,1,1,1],maroon:[.5,0,0,1],red:[1,0,0,1],purple:[.5,0,.5,1],fuchsia:[1,0,1,1],green:[0,.5,0,1],lime:[0,1,0,1],olive:[.5,.5,0,1],yellow:[1,1,0,1],navy:[0,0,.5,1],blue:[0,0,1,1],teal:[0,.5,.5,1],aqua:[0,1,1,1],orange:[1,.6470588235294118,0,1],aliceblue:[.9411764705882353,.9725490196078431,1,1],antiquewhite:[.9803921568627451,.9215686274509803,.8431372549019608,1],aquamarine:[.4980392156862745,1,.8313725490196079,1],azure:[.9411764705882353,1,1,1],beige:[.9607843137254902,.9607843137254902,.8627450980392157,1],bisque:[1,.8941176470588236,.7686274509803922,1],blanchedalmond:[1,.9215686274509803,.803921568627451,1],blueviolet:[.5411764705882353,.16862745098039217,.8862745098039215,1],brown:[.6470588235294118,.16470588235294117,.16470588235294117,1],burlywood:[.8705882352941177,.7215686274509804,.5294117647058824,1],cadetblue:[.37254901960784315,.6196078431372549,.6274509803921569,1],chartreuse:[.4980392156862745,1,0,1],chocolate:[.8235294117647058,.4117647058823529,.11764705882352941,1],coral:[1,.4980392156862745,.3137254901960784,1],cornflowerblue:[.39215686274509803,.5843137254901961,.9294117647058824,1],cornsilk:[1,.9725490196078431,.8627450980392157,1],crimson:[.8627450980392157,.0784313725490196,.23529411764705882,1],cyan:[0,1,1,1],darkblue:[0,0,.5450980392156862,1],darkcyan:[0,.5450980392156862,.5450980392156862,1],darkgoldenrod:[.7215686274509804,.5254901960784314,.043137254901960784,1],darkgray:[.6627450980392157,.6627450980392157,.6627450980392157,1],darkgreen:[0,.39215686274509803,0,1],darkgrey:[.6627450980392157,.6627450980392157,.6627450980392157,1],darkkhaki:[.7411764705882353,.7176470588235294,.4196078431372549,1],darkmagenta:[.5450980392156862,0,.5450980392156862,1],darkolivegreen:[.3333333333333333,.4196078431372549,.1843137254901961,1],darkorange:[1,.5490196078431373,0,1],darkorchard:[.6,.19607843137254902,.8,1],darkred:[.5450980392156862,0,0,1],darksalmon:[.9137254901960784,.5882352941176471,.47843137254901963,1],darkseagreen:[.5607843137254902,.7372549019607844,.5607843137254902,1],darkslateblue:[.2823529411764706,.23921568627450981,.5450980392156862,1],darkslategray:[.1843137254901961,.30980392156862746,.30980392156862746,1],darkslategrey:[.1843137254901961,.30980392156862746,.30980392156862746,1],darkturquoise:[.1843137254901961,.30980392156862746,.30980392156862746,1],darkviolet:[.5803921568627451,0,.8274509803921568,1],deeppink:[1,.0784313725490196,.5764705882352941,1],deepskyblue:[0,.7490196078431373,1,1],dimgray:[.4117647058823529,.4117647058823529,.4117647058823529,1],dimgrey:[.4117647058823529,.4117647058823529,.4117647058823529,1],dodgerblue:[.11764705882352941,.5647058823529412,1,1],firebrick:[.6980392156862745,.13333333333333333,.13333333333333333,1],floralwhite:[1,.9803921568627451,.9411764705882353,1],forestgreen:[.13333333333333333,.5450980392156862,.13333333333333333,1],gainsboro:[.8627450980392157,.8627450980392157,.8627450980392157,1],ghostwhite:[.9725490196078431,.9725490196078431,1,1],gold:[1,.8431372549019608,0,1],goldenrod:[.8549019607843137,.6470588235294118,.12549019607843137,1],greenyellow:[.6784313725490196,1,.1843137254901961,1],grey:[.5019607843137255,.5019607843137255,.5019607843137255,1],honeydew:[.9411764705882353,1,.9411764705882353,1],hotpink:[1,.4117647058823529,.7058823529411765,1],indianred:[.803921568627451,.3607843137254902,.3607843137254902,1],indigo:[.29411764705882354,0,.5098039215686274,1],ivory:[1,1,.9411764705882353,1],khaki:[.9411764705882353,.9019607843137255,.5490196078431373,1],lavendar:[.9019607843137255,.9019607843137255,.9803921568627451,1],lavenderblush:[1,.9411764705882353,.9607843137254902,1],lawngreen:[.48627450980392156,.9882352941176471,0,1],lemonchiffon:[1,.9803921568627451,.803921568627451,1],lightblue:[.6784313725490196,.8470588235294118,.9019607843137255,1],lightcoral:[.9411764705882353,.5019607843137255,.5019607843137255,1],lightcyan:[.8784313725490196,1,1,1],lightgoldenrodyellow:[.9803921568627451,.9803921568627451,.8235294117647058,1],lightgray:[.8274509803921568,.8274509803921568,.8274509803921568,1],lightgreen:[.5647058823529412,.9333333333333333,.5647058823529412,1],lightgrey:[.8274509803921568,.8274509803921568,.8274509803921568,1],lightpink:[1,.7137254901960784,.7568627450980392,1],lightsalmon:[1,.6274509803921569,.47843137254901963,1],lightseagreen:[.12549019607843137,.6980392156862745,.6666666666666666,1],lightskyblue:[.5294117647058824,.807843137254902,.9803921568627451,1],lightslategray:[.4666666666666667,.5333333333333333,.6,1],lightslategrey:[.4666666666666667,.5333333333333333,.6,1],lightsteelblue:[.6901960784313725,.7686274509803922,.8705882352941177,1],lightyellow:[1,1,.8784313725490196,1],limegreen:[.19607843137254902,.803921568627451,.19607843137254902,1],linen:[.9803921568627451,.9411764705882353,.9019607843137255,1],magenta:[1,0,1,1],mediumaquamarine:[.4,.803921568627451,.6666666666666666,1],mediumblue:[0,0,.803921568627451,1],mediumorchid:[.7294117647058823,.3333333333333333,.8274509803921568,1],mediumpurple:[.5764705882352941,.4392156862745098,.8588235294117647,1],mediumseagreen:[.23529411764705882,.7019607843137254,.44313725490196076,1],mediumslateblue:[.4823529411764706,.40784313725490196,.9333333333333333,1],mediumspringgreen:[0,.9803921568627451,.6039215686274509,1],mediumturquoise:[.2823529411764706,.8196078431372549,.8,1],mediumvioletred:[.7803921568627451,.08235294117647059,.5215686274509804,1],midnightblue:[.09803921568627451,.09803921568627451,.4392156862745098,1],mintcream:[.9607843137254902,1,.9803921568627451,1],mistyrose:[1,.8941176470588236,.8823529411764706,1],moccasin:[1,.8941176470588236,.7098039215686275,1],navajowhite:[1,.8705882352941177,.6784313725490196,1],oldlace:[.9921568627450981,.9607843137254902,.9019607843137255,1],olivedrab:[.4196078431372549,.5568627450980392,.13725490196078433,1],orangered:[1,.27058823529411763,0,1],orchid:[.8549019607843137,.4392156862745098,.8392156862745098,1],palegoldenrod:[.9333333333333333,.9098039215686274,.6666666666666666,1],palegreen:[.596078431372549,.984313725490196,.596078431372549,1],paleturquoise:[.6862745098039216,.9333333333333333,.9333333333333333,1],palevioletred:[.8588235294117647,.4392156862745098,.5764705882352941,1],papayawhip:[1,.9372549019607843,.8352941176470589,1],peachpuff:[1,.8549019607843137,.7254901960784313,1],peru:[.803921568627451,.5215686274509804,.24705882352941178,1],pink:[1,.7529411764705882,.796078431372549,1],plum:[.8666666666666667,.6274509803921569,.8666666666666667,1],powderblue:[.6901960784313725,.8784313725490196,.9019607843137255,1],rosybrown:[.7372549019607844,.5607843137254902,.5607843137254902,1],royalblue:[.2549019607843137,.4117647058823529,.8823529411764706,1],saddlebrown:[.5450980392156862,.27058823529411763,.07450980392156863,1],salmon:[.9803921568627451,.5019607843137255,.4470588235294118,1],sandybrown:[.9568627450980393,.6431372549019608,.3764705882352941,1],seagreen:[.1803921568627451,.5450980392156862,.3411764705882353,1],seashell:[1,.9607843137254902,.9333333333333333,1],sienna:[.6274509803921569,.3215686274509804,.17647058823529413,1],skyblue:[.5294117647058824,.807843137254902,.9215686274509803,1],slateblue:[.41568627450980394,.35294117647058826,.803921568627451,1],slategray:[.4392156862745098,.5019607843137255,.5647058823529412,1],slategrey:[.4392156862745098,.5019607843137255,.5647058823529412,1],snow:[1,.9803921568627451,.9803921568627451,1],springgreen:[0,1,.4980392156862745,1],steelblue:[.27450980392156865,.5098039215686274,.7058823529411765,1],tan:[.8235294117647058,.7058823529411765,.5490196078431373,1],thistle:[.8470588235294118,.7490196078431373,.8470588235294118,1],tomato:[1,.38823529411764707,.2784313725490196,1],turquoise:[.25098039215686274,.8784313725490196,.8156862745098039,1],violet:[.9333333333333333,.5098039215686274,.9333333333333333,1],wheat:[.9607843137254902,.8705882352941177,.7019607843137254,1],whitesmoke:[.9607843137254902,.9607843137254902,.9607843137254902,1],yellowgreen:[.6039215686274509,.803921568627451,.19607843137254902,1],rebeccapurple:[.4,.2,.6,1]},O=n0;var o0=/-?[0-9.]+%?/g,a0=/^#?[0-9a-f]{3,8}$/i,l0=256**2,s0=256,i0=0;function h(t){let e=u0(t),o={get hex(){let r="#";return r+=m(Math.round(e[0]*255).toString(16),2),r+=m(Math.round(e[1]*255).toString(16),2),r+=m(Math.round(e[2]*255).toString(16),2),e[3]<1&&(r+=m(Math.round(e[3]*255).toString(16),2)),r},get hexVal(){let r=Math.round(e[0]*255),i=Math.round(e[1]*255),n=Math.round(e[2]*255),a=Math.round(e[3]*255);return e[3]<1?r+256**3+i+256**2+n+256+a:r+256**2+i+256+n},get rgb(){return L(e)},rgbVal:e,get rgba(){return L(e)},rgbaVal:e,get linearRGB(){return T(e)},get p3(){return B("display-p3",e)},p3Val:e,get oklab(){return B("oklab",k(e))},get oklabVal(){return k(e)},get oklch(){return B("oklch",M(e))},get oklchVal(){return M(e)}};return o.toString=()=>o.hex,o}function u0(t){function e(r){let i=Math.max(r-l0,0)/256,n=Math.max(r-s0,0)/256,a=Math.max(r-i0,0)/256;return[i,n,a,1]}function o(r,i){let n=r.match(o0);if(!n)throw new Error(`Unexpected color format: ${r}`);let a=[0,0,0,1];return n.forEach((u,l)=>{u.includes("%")?a[l]=parseFloat(u)/100:i[l]===1/0?a[l]=parseFloat(u):a[l]=parseFloat(u)/(i[l]||1)}),a}if(Array.isArray(t)){if(t.some(r=>typeof r!="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[s(t[0],0,1),s(t[1],0,1),s(t[2],0,1),typeof t[3]=="number"?s(t[3],0,1):1]}if(typeof t=="number")return e(t);if(typeof t=="string"){let r=t.trim();if(!r)throw new Error("Expected color, received empty string");if(O[r.toLowerCase()])return O[r.toLowerCase()];if(a0.test(r)){let a=r.replace("#",""),u=[0,0,0,1];if(a.length>=6)for(let l=0;l<a.length/2;l++){let c=l*2,p=c+2,g=a.substring(c,p);u[l]=parseInt(g,16)/255}else for(let l=0;l<a.length;l++){let c=a.charAt(l);u[l]=parseInt(`${c}${c}`,16)/255}return u}let[i,n]=r.split("(");if(i==="color"){let a=n.indexOf(" ");i=n.substring(0,a),n=n.substring(a)}switch(i){case"rgb":case"rgba":case"srgb":{let[a,u,l,c]=o(n,[255,255,255,1]);return[s(a,0,1),s(u,0,1),s(l,0,1),s(c,0,1)]}case"srgb-linear":{let[a,u,l,c]=o(n,[255,255,255,1]);return b([s(a,0,1),s(u,0,1),s(l,0,1),s(c,0,1)])}case"hsl":{let[a,u,l,c]=o(n,[1/0,1,1,1]);return q([a,s(u,0,1),s(l,0,1),s(c,0,1)])}case"p3":case"display-p3":{let[a,u,l,c]=o(n,[1,1,1,1]);return[s(a,0,1),s(u,0,1),s(l,0,1),s(c,0,1)]}case"luv":return F(o(n,[1,1,1,1]));case"oklab":return G(o(n,[1,1,1,1]));case"oklch":return _(o(n,[1,1,1/0,1]))}}throw new Error(`Unable to parse color "${t}"`)}function A(t){return f(h(t).oklabVal[0],5)}function x(t,e,o=.5,r="oklab"){let i=s(o,0,1);if(o===0)return h(t);if(o===1)return h(e);let n=1-i,a=i,u={oklch:M,oklab:k,linearRGB:T,sRGB:E=>E},l={oklch:_,oklab:G,linearRGB:b,sRGB:E=>E},c=u[r],p=l[r];if(!c)throw new Error(`Unknown color space "${r}", try "oklab", "oklch", "linearRGB", or "sRGB"`);let g=h(t).rgbVal,R=h(e).rgbVal;r==="oklch"&&(g[0]===g[1]&&g[1]===g[2]||R[0]===R[1]&&R[1]===R[2])&&(c=u.oklab,p=l.oklab);let[S,N,w,z]=c(g),[X,P,y,D]=c(R);return r==="oklch"&&Math.abs(y-w)>180&&(Math.max(w,y)===y?y-=360:w-=360),h(p([S*n+X*a,N*n+P*a,w*n+y*a,z*n+D*a]))}function v(t,e,o="oklab"){let r=s(e,-1,1);return r>=0?x(t,"black",r,o):$(t,-r)}function $(t,e,o="oklab"){let r=s(e,-1,1);return r>=0?x(t,"white",r,o):v(t,-r)}var _0={darken:v,from:h,lighten:$,lightness:A,mix:x};export{s as clamp,B as colorFn,v as darken,_0 as default,h as from,m as leftPad,$ as lighten,A as lightness,x as mix,L as rgbFn,f as round};
function m(t,e=2){let n=t;for(;n.length<e;)n=`0${n}`;return n}function I(t){return t*(Math.PI/180)}function V(t){return t*(180/Math.PI)}function s(t,e,n){return Math.min(Math.max(t,e),n)}function G(t,e){return`color(${t} ${p(e[0],5)} ${p(e[1],5)} ${p(e[2],5)}${e[3]<1?`/${p(e[3],5)}`:""})`}function d(t,e){let n=[...t];for(let r=0;r<e.length;r++){let a=0;for(let o=0;o<e[0].length;o++)a+=t[o]*e[r][o];n[r]=a}return n}function p(t,e=2){let n=10**e;return Math.round(t*n)/n}function _(t){let e=`${Math.round(t[0]*255)}, ${Math.round(t[1]*255)}, ${Math.round(t[2]*255)}`;return t[3]!==1?`rgba(${e}, ${p(t[3],5)})`:`rgb(${e})`}var U=[[.2104542553,.793617785,-.0040720468],[1.9779984951,-2.428592205,.4505937099],[.0259040371,.7827717662,-.808675766]],K=[[4.0767416621,-3.3077115913,.2309699292],[-1.2684380046,2.6097574011,-.3413193965],[-.0041960863,-.7034186147,1.707614701]];var j=[[.4122214708,.5363325363,.0514459929],[.2119034982,.6806995451,.1073969566],[.0883024619,.2817188376,.6299787005]],J=[[1,.3963377774,.2158037573],[1,-.1055613458,-.0638541728],[1,-.0894841775,-1.291485548]],Q=[[3.2406,-1.5372,-.4986],[-.9689,1.8758,.0415],[.0557,-.204,1.057]];function q(t){let[e,n,r,a]=t;e=Math.abs(e%360);let o=n*(1-Math.abs(2*r-1)),l=o*(1-Math.abs(e/60%2-1)),i=0,c=0,u=0;0<=e&&e<60?(i=o,c=l):60<=e&&e<120?(i=l,c=o):120<=e&&e<180?(c=o,u=l):180<=e&&e<240?(c=l,u=o):240<=e&&e<300?(i=l,u=o):300<=e&&e<360&&(i=o,u=l);let g=r-o/2;return[i+g,c+g,u+g,a]}function W(t){let[e,n,r,a]=t,o=n===0&&r===0?0:V(Math.atan2(r,n));return[e,Math.sqrt(n**2+r**2),o<0?o+360:o,a]}function C(t){let[e,n,r,a]=t,o=I(r);return[e,n*Math.cos(o),n*Math.sin(o),a]}function e0(t){let e=Math.cbrt(t[0]),n=Math.cbrt(t[1]),r=Math.cbrt(t[2]),a=t[3];return d([e,n,r,a],U)}function t0(t){let[e,n,r,a]=d(t,K);return[Math.max(0,e),Math.max(0,n),Math.max(0,r),Math.max(0,a)]}function b(t){return t.map((e,n)=>n===3?s(e,0,1):e<=.0031308?s(e*12.92,0,1):s(1.055*e**(1/2.4)-.055,0,1))}function r0(t){return d(t,j)}function n0(t){let[e,n,r,a]=t,o=[0,0,0,a],l=n/(13*e),i=r/(13*e);return o[1]=e<=8?e*(3/29)**3:((e+16)/116)**3,o[0]=o[1]*(9/4)*l,o[2]=o[1]*((12-3*l-20*i)/4*i),o}function S(t){return b(a0(n0(t)))}function o0(t){let e=d(t,J);return[e[0]**3,e[1]**3,e[2]**3,e[3]]}function T(t){return b(t0(o0(t)))}function v(t){return T(C(t))}function M(t){return t.map((e,n)=>n===3?e:e<=.04045?e/12.92:((e+.055)/1.055)**2.4)}function k(t){return e0(r0(M(t)))}function w(t){return W(k(t))}function a0(t){return d(t,Q)}var l0={black:[0,0,0,1],silver:[.75,.75,.75,1],gray:[.5,.5,.5,1],white:[1,1,1,1],maroon:[.5,0,0,1],red:[1,0,0,1],purple:[.5,0,.5,1],fuchsia:[1,0,1,1],green:[0,.5,0,1],lime:[0,1,0,1],olive:[.5,.5,0,1],yellow:[1,1,0,1],navy:[0,0,.5,1],blue:[0,0,1,1],teal:[0,.5,.5,1],aqua:[0,1,1,1],orange:[1,.6470588235294118,0,1],aliceblue:[.9411764705882353,.9725490196078431,1,1],antiquewhite:[.9803921568627451,.9215686274509803,.8431372549019608,1],aquamarine:[.4980392156862745,1,.8313725490196079,1],azure:[.9411764705882353,1,1,1],beige:[.9607843137254902,.9607843137254902,.8627450980392157,1],bisque:[1,.8941176470588236,.7686274509803922,1],blanchedalmond:[1,.9215686274509803,.803921568627451,1],blueviolet:[.5411764705882353,.16862745098039217,.8862745098039215,1],brown:[.6470588235294118,.16470588235294117,.16470588235294117,1],burlywood:[.8705882352941177,.7215686274509804,.5294117647058824,1],cadetblue:[.37254901960784315,.6196078431372549,.6274509803921569,1],chartreuse:[.4980392156862745,1,0,1],chocolate:[.8235294117647058,.4117647058823529,.11764705882352941,1],coral:[1,.4980392156862745,.3137254901960784,1],cornflowerblue:[.39215686274509803,.5843137254901961,.9294117647058824,1],cornsilk:[1,.9725490196078431,.8627450980392157,1],crimson:[.8627450980392157,.0784313725490196,.23529411764705882,1],cyan:[0,1,1,1],darkblue:[0,0,.5450980392156862,1],darkcyan:[0,.5450980392156862,.5450980392156862,1],darkgoldenrod:[.7215686274509804,.5254901960784314,.043137254901960784,1],darkgray:[.6627450980392157,.6627450980392157,.6627450980392157,1],darkgreen:[0,.39215686274509803,0,1],darkgrey:[.6627450980392157,.6627450980392157,.6627450980392157,1],darkkhaki:[.7411764705882353,.7176470588235294,.4196078431372549,1],darkmagenta:[.5450980392156862,0,.5450980392156862,1],darkolivegreen:[.3333333333333333,.4196078431372549,.1843137254901961,1],darkorange:[1,.5490196078431373,0,1],darkorchard:[.6,.19607843137254902,.8,1],darkred:[.5450980392156862,0,0,1],darksalmon:[.9137254901960784,.5882352941176471,.47843137254901963,1],darkseagreen:[.5607843137254902,.7372549019607844,.5607843137254902,1],darkslateblue:[.2823529411764706,.23921568627450981,.5450980392156862,1],darkslategray:[.1843137254901961,.30980392156862746,.30980392156862746,1],darkslategrey:[.1843137254901961,.30980392156862746,.30980392156862746,1],darkturquoise:[.1843137254901961,.30980392156862746,.30980392156862746,1],darkviolet:[.5803921568627451,0,.8274509803921568,1],deeppink:[1,.0784313725490196,.5764705882352941,1],deepskyblue:[0,.7490196078431373,1,1],dimgray:[.4117647058823529,.4117647058823529,.4117647058823529,1],dimgrey:[.4117647058823529,.4117647058823529,.4117647058823529,1],dodgerblue:[.11764705882352941,.5647058823529412,1,1],firebrick:[.6980392156862745,.13333333333333333,.13333333333333333,1],floralwhite:[1,.9803921568627451,.9411764705882353,1],forestgreen:[.13333333333333333,.5450980392156862,.13333333333333333,1],gainsboro:[.8627450980392157,.8627450980392157,.8627450980392157,1],ghostwhite:[.9725490196078431,.9725490196078431,1,1],gold:[1,.8431372549019608,0,1],goldenrod:[.8549019607843137,.6470588235294118,.12549019607843137,1],greenyellow:[.6784313725490196,1,.1843137254901961,1],grey:[.5019607843137255,.5019607843137255,.5019607843137255,1],honeydew:[.9411764705882353,1,.9411764705882353,1],hotpink:[1,.4117647058823529,.7058823529411765,1],indianred:[.803921568627451,.3607843137254902,.3607843137254902,1],indigo:[.29411764705882354,0,.5098039215686274,1],ivory:[1,1,.9411764705882353,1],khaki:[.9411764705882353,.9019607843137255,.5490196078431373,1],lavendar:[.9019607843137255,.9019607843137255,.9803921568627451,1],lavenderblush:[1,.9411764705882353,.9607843137254902,1],lawngreen:[.48627450980392156,.9882352941176471,0,1],lemonchiffon:[1,.9803921568627451,.803921568627451,1],lightblue:[.6784313725490196,.8470588235294118,.9019607843137255,1],lightcoral:[.9411764705882353,.5019607843137255,.5019607843137255,1],lightcyan:[.8784313725490196,1,1,1],lightgoldenrodyellow:[.9803921568627451,.9803921568627451,.8235294117647058,1],lightgray:[.8274509803921568,.8274509803921568,.8274509803921568,1],lightgreen:[.5647058823529412,.9333333333333333,.5647058823529412,1],lightgrey:[.8274509803921568,.8274509803921568,.8274509803921568,1],lightpink:[1,.7137254901960784,.7568627450980392,1],lightsalmon:[1,.6274509803921569,.47843137254901963,1],lightseagreen:[.12549019607843137,.6980392156862745,.6666666666666666,1],lightskyblue:[.5294117647058824,.807843137254902,.9803921568627451,1],lightslategray:[.4666666666666667,.5333333333333333,.6,1],lightslategrey:[.4666666666666667,.5333333333333333,.6,1],lightsteelblue:[.6901960784313725,.7686274509803922,.8705882352941177,1],lightyellow:[1,1,.8784313725490196,1],limegreen:[.19607843137254902,.803921568627451,.19607843137254902,1],linen:[.9803921568627451,.9411764705882353,.9019607843137255,1],magenta:[1,0,1,1],mediumaquamarine:[.4,.803921568627451,.6666666666666666,1],mediumblue:[0,0,.803921568627451,1],mediumorchid:[.7294117647058823,.3333333333333333,.8274509803921568,1],mediumpurple:[.5764705882352941,.4392156862745098,.8588235294117647,1],mediumseagreen:[.23529411764705882,.7019607843137254,.44313725490196076,1],mediumslateblue:[.4823529411764706,.40784313725490196,.9333333333333333,1],mediumspringgreen:[0,.9803921568627451,.6039215686274509,1],mediumturquoise:[.2823529411764706,.8196078431372549,.8,1],mediumvioletred:[.7803921568627451,.08235294117647059,.5215686274509804,1],midnightblue:[.09803921568627451,.09803921568627451,.4392156862745098,1],mintcream:[.9607843137254902,1,.9803921568627451,1],mistyrose:[1,.8941176470588236,.8823529411764706,1],moccasin:[1,.8941176470588236,.7098039215686275,1],navajowhite:[1,.8705882352941177,.6784313725490196,1],oldlace:[.9921568627450981,.9607843137254902,.9019607843137255,1],olivedrab:[.4196078431372549,.5568627450980392,.13725490196078433,1],orangered:[1,.27058823529411763,0,1],orchid:[.8549019607843137,.4392156862745098,.8392156862745098,1],palegoldenrod:[.9333333333333333,.9098039215686274,.6666666666666666,1],palegreen:[.596078431372549,.984313725490196,.596078431372549,1],paleturquoise:[.6862745098039216,.9333333333333333,.9333333333333333,1],palevioletred:[.8588235294117647,.4392156862745098,.5764705882352941,1],papayawhip:[1,.9372549019607843,.8352941176470589,1],peachpuff:[1,.8549019607843137,.7254901960784313,1],peru:[.803921568627451,.5215686274509804,.24705882352941178,1],pink:[1,.7529411764705882,.796078431372549,1],plum:[.8666666666666667,.6274509803921569,.8666666666666667,1],powderblue:[.6901960784313725,.8784313725490196,.9019607843137255,1],rosybrown:[.7372549019607844,.5607843137254902,.5607843137254902,1],royalblue:[.2549019607843137,.4117647058823529,.8823529411764706,1],saddlebrown:[.5450980392156862,.27058823529411763,.07450980392156863,1],salmon:[.9803921568627451,.5019607843137255,.4470588235294118,1],sandybrown:[.9568627450980393,.6431372549019608,.3764705882352941,1],seagreen:[.1803921568627451,.5450980392156862,.3411764705882353,1],seashell:[1,.9607843137254902,.9333333333333333,1],sienna:[.6274509803921569,.3215686274509804,.17647058823529413,1],skyblue:[.5294117647058824,.807843137254902,.9215686274509803,1],slateblue:[.41568627450980394,.35294117647058826,.803921568627451,1],slategray:[.4392156862745098,.5019607843137255,.5647058823529412,1],slategrey:[.4392156862745098,.5019607843137255,.5647058823529412,1],snow:[1,.9803921568627451,.9803921568627451,1],springgreen:[0,1,.4980392156862745,1],steelblue:[.27450980392156865,.5098039215686274,.7058823529411765,1],tan:[.8235294117647058,.7058823529411765,.5490196078431373,1],thistle:[.8470588235294118,.7490196078431373,.8470588235294118,1],tomato:[1,.38823529411764707,.2784313725490196,1],turquoise:[.25098039215686274,.8784313725490196,.8156862745098039,1],violet:[.9333333333333333,.5098039215686274,.9333333333333333,1],wheat:[.9607843137254902,.8705882352941177,.7019607843137254,1],whitesmoke:[.9607843137254902,.9607843137254902,.9607843137254902,1],yellowgreen:[.6039215686274509,.803921568627451,.19607843137254902,1],rebeccapurple:[.4,.2,.6,1]},A=l0;var s0=/-?[0-9.]+%?/g,i0=/^#?[0-9a-f]{3,8}$/i,u0=16**6,N=16**4,z=16**2;function h(t){let e=h0(t),n={get hex(){let r="#";return r+=m(Math.round(e[0]*255).toString(16),2),r+=m(Math.round(e[1]*255).toString(16),2),r+=m(Math.round(e[2]*255).toString(16),2),e[3]<1&&(r+=m(Math.round(e[3]*255).toString(16),2)),r},get hexVal(){let r=Math.round(e[0]*255),a=Math.round(e[1]*255),o=Math.round(e[2]*255),l=Math.round(e[3]*255);return e[3]<1?r+256**3+a+256**2+o+256+l:r+256**2+a+256+o},get rgb(){return _(e)},rgbVal:e,get rgba(){return _(e)},rgbaVal:e,get linearRGB(){return M(e)},get p3(){return G("display-p3",e)},p3Val:e,get oklab(){return G("oklab",k(e))},get oklabVal(){return k(e)},get oklch(){return G("oklch",w(e))},get oklchVal(){return w(e)}};return n.toString=()=>n.hex,n}function c0(t){if(t>u0)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,n=Math.floor(e/N);e-=n*N;let r=Math.floor(e/z);e-=r*z;let a=e;return[n/255,r/255,a/255,1]}function f(t,e){let n=t.match(s0);if(!n)throw new Error(`Unexpected color format: ${t}`);let r=[0,0,0,1];return n.forEach((a,o)=>{a.includes("%")?r[o]=parseFloat(a)/100:e[o]===1/0?r[o]=parseFloat(a):r[o]=parseFloat(a)/(e[o]||1)}),r}function h0(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[s(t[0],0,1),s(t[1],0,1),s(t[2],0,1),typeof t[3]=="number"?s(t[3],0,1):1]}if(typeof t=="number")return c0(t);if(typeof t=="string"){let e=t.trim();if(!e)throw new Error("Expected color, received empty string");if(A[e.toLowerCase()])return A[e.toLowerCase()];if(i0.test(e)){let a=e.replace("#",""),o=[0,0,0,1];if(a.length>=6)for(let l=0;l<a.length/2;l++){let i=l*2,c=i+2,u=a.substring(i,c);o[l]=parseInt(u,16)/255}else for(let l=0;l<a.length;l++){let i=a.charAt(l);o[l]=parseInt(`${i}${i}`,16)/255}return o}let[n,r]=e.split("(");if(n==="color"){let a=r.indexOf(" ");n=r.substring(0,a),r=r.substring(a)}switch(n){case"rgb":case"rgba":case"srgb":{let[a,o,l,i]=f(r,[255,255,255,1]);return[s(a,0,1),s(o,0,1),s(l,0,1),s(i,0,1)]}case"srgb-linear":{let[a,o,l,i]=f(r,[255,255,255,1]);return b([s(a,0,1),s(o,0,1),s(l,0,1),s(i,0,1)])}case"hsl":{let[a,o,l,i]=f(r,[1/0,1,1,1]);return q([a,s(o,0,1),s(l,0,1),s(i,0,1)])}case"p3":case"display-p3":{let[a,o,l,i]=f(r,[1,1,1,1]);return[s(a,0,1),s(o,0,1),s(l,0,1),s(i,0,1)]}case"luv":return S(f(r,[1,1,1,1]));case"oklab":return T(f(r,[1,1,1,1]));case"oklch":return v(f(r,[1,1,1/0,1]))}}throw new Error(`Unable to parse color "${t}"`)}function F(t){return p(h(t).oklabVal[0],5)}function x(t,e,n=.5,r="oklab"){let a=s(n,0,1);if(n===0)return h(t);if(n===1)return h(e);let o=1-a,l=a,i={oklch:w,oklab:k,linearRGB:M,sRGB:E=>E},c={oklch:v,oklab:T,linearRGB:b,sRGB:E=>E},u=i[r],g=c[r];if(!u)throw new Error(`Unknown color space "${r}", try "oklab", "oklch", "linearRGB", or "sRGB"`);let R=h(t).rgbVal,y=h(e).rgbVal;r==="oklch"&&(R[0]===R[1]&&R[1]===R[2]||y[0]===y[1]&&y[1]===y[2])&&(u=i.oklab,g=c.oklab);let[P,X,L,D]=u(R),[Y,Z,B,H]=u(y);return r==="oklch"&&Math.abs(B-L)>180&&(Math.max(L,B)===B?B-=360:L-=360),h(g([P*o+Y*l,X*o+Z*l,L*o+B*l,D*o+H*l]))}function $(t,e,n="oklab"){let r=s(e,-1,1);return r>=0?x(t,"black",r,n):O(t,-r)}function O(t,e,n="oklab"){let r=s(e,-1,1);return r>=0?x(t,"white",r,n):$(t,-r)}var $0={darken:$,from:h,lighten:O,lightness:F,mix:x};export{s as clamp,G as colorFn,$ as darken,$0 as default,h as from,m as leftPad,O as lighten,F as lightness,x as mix,_ as rgbFn,p as round};

@@ -48,2 +48,9 @@ import type { Color, LinearRGB, Oklab, Oklch, sRGB } from './colorspace.js';

/**
* hex num to sRGB (doesn’t support alpha as 0x000000 == 0x00000000)
* V8 handles number ops ~ 2x faster than parseInt(hex, 16) with string manipulations
*/
export declare function hexNumTosRGB(hex: number): sRGB;
/** Convert any number of user inputs into RGBA array */
export declare function parse(rawColor: Color): sRGB;
/**
* Lightness

@@ -50,0 +57,0 @@ * Shortcut of "L” from oklab

@@ -7,5 +7,6 @@ import { hslTosRGB, linearRGBTosRGB, luvTosRGB, oklabTosRGB, oklchTosRGB, sRGBToLinearRGB, sRGBToOklab, sRGBToOklch } from './colorspace.js';

const HEX_RE = /^#?[0-9a-f]{3,8}$/i;
const R_RANGE = 256 ** 2;
const G_RANGE = 256;
const B_RANGE = 0;
const RGB_RANGE = 16 ** 6;
const R_FACTOR = 16 ** 4; // base 16, starting after 4 digits (GGBB)
const G_FACTOR = 16 ** 2; // base 16, starting after 2 digits (BB)
// B_FACTOR = 1 (16 ** 0); not really needed
/**

@@ -83,30 +84,38 @@ * Parse any valid CSS color color string and convert to:

}
/**
* hex num to sRGB (doesn’t support alpha as 0x000000 == 0x00000000)
* V8 handles number ops ~ 2x faster than parseInt(hex, 16) with string manipulations
*/
export function hexNumTosRGB(hex) {
if (hex > RGB_RANGE)
throw new Error('better-color-tools can’t parse hex numbers with alpha (0x0000000 is indistinguishable from 0x00000000). Please use hex string, or another color method');
let remaining = hex;
const r = Math.floor(remaining / R_FACTOR); // Math.floor gets rid of G + B
remaining -= r * R_FACTOR;
const g = Math.floor(remaining / G_FACTOR); // Math.floor gets rid of B
remaining -= g * G_FACTOR;
const b = remaining;
return [r / 255, g / 255, b / 255, 1];
}
/** only grabs numbers from a color string (ignores spaces, commas, slashes, etc.) */
function parseValueStr(colorStr, normalize) {
const matches = colorStr.match(FLOAT_RE);
if (!matches)
throw new Error(`Unexpected color format: ${colorStr}`);
const values = [0, 0, 0, 1]; // always start alpha at 1 unless overridden
matches.forEach((value, n) => {
// percentage (already normalized)
if (value.includes('%'))
values[n] = parseFloat(value) / 100;
// unbounded
else if (normalize[n] === Infinity)
values[n] = parseFloat(value);
// bounded
else
values[n] = parseFloat(value) / (normalize[n] || 1);
});
return values;
}
/** Convert any number of user inputs into RGBA array */
function parse(rawColor) {
/** hex num to sRGB (note: doesn’t support alpha!) */
function hexNumTosRGB(hex) {
const r = Math.max(hex - R_RANGE, 0) / 256;
const g = Math.max(hex - G_RANGE, 0) / 256;
const b = Math.max(hex - B_RANGE, 0) / 256;
return [r, g, b, 1];
}
/** only grabs numbers from a color string (ignores spaces, commas, slashes, etc.) */
function parseValueStr(colorStr, normalize) {
const matches = colorStr.match(FLOAT_RE);
if (!matches)
throw new Error(`Unexpected color format: ${colorStr}`);
const values = [0, 0, 0, 1]; // always start alpha at 1 unless overridden
matches.forEach((value, n) => {
// percentage (already normalized)
if (value.includes('%'))
values[n] = parseFloat(value) / 100;
// unbounded
else if (normalize[n] === Infinity)
values[n] = parseFloat(value);
// bounded
else
values[n] = parseFloat(value) / (normalize[n] || 1);
});
return values;
}
export function parse(rawColor) {
// [R, G, B] or [R, G, B, A]

@@ -113,0 +122,0 @@ if (Array.isArray(rawColor)) {

{
"name": "better-color-tools",
"description": "Better color manipulation for Sass and JavaScript / TypeScript.",
"version": "0.6.1",
"version": "0.6.2",
"author": {

@@ -28,2 +28,18 @@ "name": "Drew Powers",

"types": "./dist/index.d.ts",
"devDependencies": {
"@changesets/cli": "^2.21.1",
"@types/node": "^16.11.26",
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/parser": "^5.14.0",
"chai": "^4.3.6",
"esbuild": "^0.14.25",
"eslint": "^8.10.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"mocha": "^9.2.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.5.1",
"sass": "^1.49.9",
"typescript": "^4.6.2"
},
"scripts": {

@@ -35,22 +51,6 @@ "build": "rm -rf dist && tsc && npm run bundle && cp dist/index.min.js www/src/lib/better.min.js",

"lint": "eslint \"**/*.{js,ts}\"",
"prepublishOnly": "npm run build",
"test": "mocha --parallel",
"test:benchmark": "mocha"
},
"devDependencies": {
"@changesets/cli": "^2.21.0",
"@types/node": "^16.11.25",
"@typescript-eslint/eslint-plugin": "^5.12.1",
"@typescript-eslint/parser": "^5.12.1",
"chai": "^4.3.6",
"esbuild": "^0.14.23",
"eslint": "^8.9.0",
"eslint-config-prettier": "^8.4.0",
"eslint-plugin-prettier": "^4.0.0",
"mocha": "^9.2.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.5.1",
"sass": "^1.49.8",
"typescript": "^4.5.5"
}
}
"readme": "# better-color-tools\n\nColor parser and better color manipulation through **the power of science!** 🧪 Uses [Oklab](https://bottosson.github.io/posts/oklab/)/Oklch for better color operations.\n\nThe JS version of this libray is fast (`> 220k` ops/s), lightweight (`4.5 kB` gzip), and dependency-free. The Sass version… is Sass (which has no runtime).\n\n👉 **Playground**: https://better-color-tools.pages.dev/\n\n## Usage\n\n```\nnpm install better-color-tools\n```\n\n### JavaScript\n\nWorks in the browser (ESM) and Node (14+).\n\n```js\nimport better from 'better-color-utils';\n```\n\n#### Quick guide\n\n| Code | Description |\n| :-------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `better.from('red')` | Parse any valid CSS color (including [color()][css-color]) |\n| `better.from('red').[colorspace]` | Convert color to [another colorspace](#supported-colorspaces) |\n| `better.mix('red', 'lime', 0.35)` | Mix `red` and `lime` 35%, i.e. more red. Uses Oklab for better color mixing. |\n| `better.lighten('red', 0.5)` | Lighten color by 50%, i.e. halfway to white (100% is white; 0% is original color). Better than Sass’ builtin. |\n| `better.darken('red', 0.5)` | Darken color by 50%, i.e. halfway to black (100% is black; 0% is original color). Better than Sass’ builtin. |\n| `better.lightness('red', 0.5)` | Get the human-perceived value of lightness from `0` (pure black) to `1` (pure white). Alias for `better.from().oklabVal`’s lightness (first value) |\n\n#### Supported colorspaces\n\n| Colorspace | Type | Example |\n| :---------- | :--------- | :------------------------------------------------- |\n| `.hex` | `string` | `'#ff0000'` |\n| `.hexVal` | `number` | `0xff0000` |\n| `.rgb` | `string` | `'rgb(255, 0, 0)'` |\n| `.rgbVal` | `number[]` | `[1, 0, 0, 1]` (R, G, B, alpha) |\n| `.p3` | `string` | `'color(display-p3 1 0 0)'` |\n| `.p3Val` | `number[]` | (alias for `rgbVal`) |\n| `.oklab` | `string` | `'color(oklab 0.62796 0.22486 0.12585)'` |\n| `.oklabVal` | `number[]` | `[0.62796, 0.22486, 0.12585, 1]` (L, a, b, alpha) |\n| `.oklch` | `string` | `'color(oklch 0.62796 0.25768 29.23389)'` |\n| `.oklchVal` | `number[]` | `[0.62796, 0.25768, 29.23389, 1]` (L, C, h, alpha) |\n\n### Sass\n\nWorks with any version of [Dart Sass](https://sass-lang.com/dart-sass) (the current version).\n\n```scss\n@use 'better-color-tools' as better;\n```\n\n#### Quick guide\n\n| Code | Description |\n| :-------------------------------------------------------------- | ------------------------------------------------------------------------------------- |\n| `better.p3(#f00)` | Convert RGB color to [P3][p3] (`color(display-p3 …)`) ([CSS Module 5][css-color]) |\n| `better.rgbToOklab(#f00)` | Convert RGB to Oklab `color(oklab …)` ([CSS Module 5][css-color]) |\n| `better.oklabToRGB(('l': 0.87421, 'a': -0.19121, 'b': 0.1174))` | Convert Oklab map of `l`, `a`, `b` to Sass color (with all values normalized to `1`). |\n| `better.fallback('color', better.p3(#f00), #f00)` | Easy fallback constructor (meant for color, but may be used for anything). |\n| `better.mix(red, lime, 0.35)` | Mix `red` and `lime` 35%, i.e. more red. Uses Oklab for improved mixing. |\n| `better.lighten(#f00, 0.5)` | Lighten color by 50%, i.e. halfway to white (`1` is white; `0` is original color) |\n| `better.darken(#f00, 0.5)` | Darken color by 50%, i.e. halfway to black (`1` is black; `0` is original color) |\n| `better.lightness(#f00, 0.5)` | Get the human-perceived value of lightness from `0` (pure black) to `1` (pure white). |\n\n## Project summary\n\nThis project is meant to provide **the best possible method** for common color operations such as mixing, lightening/darkening, and conversion. This library is _not_ comprehensive, and doesn’t support any colorspaces that don’t serve a practical purpose\n(limiting colorspaces helps this library optimize for performance over completeness, not to mention ease-of-use). If you are well-versed in color science and need a comprehensive library, consider [Culori][culori] or [Color.js][colorjs] instead.\n\nTo learn more, see [Project Goals](./docs/faq.md#project-goals)\n\n[culori]: https://culorijs.org/\n[colorjs]: https://colorjs.io/\n[css-color]: https://www.w3.org/TR/css-color-5/#color-function\n[faq]: https://github.com/drwpow/better-color-tools/blob/main/faq.md\n[p3]: https://webkit.org/blog/10042/wide-gamut-color-in-css-with-display-p3/\n"
}

@@ -5,3 +5,3 @@ # better-color-tools

The JS version of this libray is fast (`> 200k` ops/s), lightweight (`4 kB` gzip), and dependency-free. The Sass version… is Sass (which has no runtime).
The JS version of this libray is fast (`> 220k` ops/s), lightweight (`4.5 kB` gzip), and dependency-free. The Sass version… is Sass (which has no runtime).

@@ -8,0 +8,0 @@ 👉 **Playground**: https://better-color-tools.pages.dev/

@@ -44,5 +44,6 @@ import type { Color, LinearRGB, Oklab, Oklch, sRGB } from './colorspace.js';

const HEX_RE = /^#?[0-9a-f]{3,8}$/i;
const R_RANGE = 256 ** 2;
const G_RANGE = 256;
const B_RANGE = 0;
const RGB_RANGE = 16 ** 6;
const R_FACTOR = 16 ** 4; // base 16, starting after 4 digits (GGBB)
const G_FACTOR = 16 ** 2; // base 16, starting after 2 digits (BB)
// B_FACTOR = 1 (16 ** 0); not really needed

@@ -123,28 +124,35 @@ /**

/** Convert any number of user inputs into RGBA array */
function parse(rawColor: Color): sRGB {
/** hex num to sRGB (note: doesn’t support alpha!) */
function hexNumTosRGB(hex: number): sRGB {
const r = Math.max(hex - R_RANGE, 0) / 256;
const g = Math.max(hex - G_RANGE, 0) / 256;
const b = Math.max(hex - B_RANGE, 0) / 256;
return [r, g, b, 1];
}
/**
* hex num to sRGB (doesn’t support alpha as 0x000000 == 0x00000000)
* V8 handles number ops ~ 2x faster than parseInt(hex, 16) with string manipulations
*/
export function hexNumTosRGB(hex: number): sRGB {
if (hex > RGB_RANGE) throw new Error('better-color-tools can’t parse hex numbers with alpha (0x0000000 is indistinguishable from 0x00000000). Please use hex string, or another color method');
let remaining = hex;
const r = Math.floor(remaining / R_FACTOR); // Math.floor gets rid of G + B
remaining -= r * R_FACTOR;
const g = Math.floor(remaining / G_FACTOR); // Math.floor gets rid of B
remaining -= g * G_FACTOR;
const b = remaining;
return [r / 255, g / 255, b / 255, 1];
}
/** only grabs numbers from a color string (ignores spaces, commas, slashes, etc.) */
function parseValueStr(colorStr: string, normalize: number[]): [number, number, number, number] {
const matches = colorStr.match(FLOAT_RE);
if (!matches) throw new Error(`Unexpected color format: ${colorStr}`);
const values: [number, number, number, number] = [0, 0, 0, 1]; // always start alpha at 1 unless overridden
matches.forEach((value, n) => {
// percentage (already normalized)
if (value.includes('%')) values[n] = parseFloat(value) / 100;
// unbounded
else if (normalize[n] === Infinity) values[n] = parseFloat(value);
// bounded
else values[n] = parseFloat(value) / (normalize[n] || 1);
});
return values;
}
/** only grabs numbers from a color string (ignores spaces, commas, slashes, etc.) */
function parseValueStr(colorStr: string, normalize: number[]): [number, number, number, number] {
const matches = colorStr.match(FLOAT_RE);
if (!matches) throw new Error(`Unexpected color format: ${colorStr}`);
const values: [number, number, number, number] = [0, 0, 0, 1]; // always start alpha at 1 unless overridden
matches.forEach((value, n) => {
// percentage (already normalized)
if (value.includes('%')) values[n] = parseFloat(value) / 100;
// unbounded
else if (normalize[n] === Infinity) values[n] = parseFloat(value);
// bounded
else values[n] = parseFloat(value) / (normalize[n] || 1);
});
return values;
}
/** Convert any number of user inputs into RGBA array */
export function parse(rawColor: Color): sRGB {
// [R, G, B] or [R, G, B, A]

@@ -151,0 +159,0 @@ if (Array.isArray(rawColor)) {

Sorry, the diff of this file is not supported yet

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