lib-colors
Version:
Simple node.js library for work with colors
2 lines (1 loc) • 8.9 kB
JavaScript
"use strict";var z=Object.defineProperty;var nr=Object.getOwnPropertyDescriptor;var er=Object.getOwnPropertyNames;var mr=Object.prototype.hasOwnProperty;var ar=(e,r)=>{for(var t in r)z(e,t,{get:r[t],enumerable:!0})},ir=(e,r,t,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of er(r))!mr.call(e,n)&&n!==t&&z(e,n,{get:()=>r[n],enumerable:!(o=nr(r,n))||o.enumerable});return e};var cr=e=>ir(z({},"__esModule",{value:!0}),e);var Ir={};ar(Ir,{CMYK:()=>g,Gray:()=>I,HSL:()=>M,RGB:()=>h,cmyk:()=>hr,gray:()=>lr,hex:()=>yr,hsl:()=>gr,rgb:()=>xr});module.exports=cr(Ir);function f(e){return e=Number(e),e>=0&&e<1}function p(e,r,t){return Math.min(Math.max(e,r),t)}var T={g:0};var C={g:100};function H(e){let{g:r,a:t}={...e},o=Math.round(r/100*256)-1;return{r:o,g:o,b:o,a:t}}function $(e){let{g:r,a:t}={...e},o=Math.round(r/100*255);return f(t)?`rgba(${o},${o},${o},${t})`:`rgb(${o},${o},${o})`}var I=class{color={...T};max=C;get g(){return this.color.g}set g(r){this.color.g=p(r,0,this.max.g)}get a(){return this.color.a}set a(r){this.color.a=f(r)?p(Number(r),0,1):void 0}constructor(r,t){this.g=r,this.a=t}toString(){return $(this.color)}rgb(){let{r,g:t,b:o,a:n}=H(this.color);return new h(r,t,o,n)}};var K={h:0,s:0,l:0};var w={h:360,s:100,l:100};function A(e){let{h:r,s:t,l:o,a:n}={...e},m,a,i;if(t===0)m=a=i=o;else{let c=o<.5?o*(1+t):o+t-o*t,s=2*o-c,u=r/360;m=Y(s,c,u+1/3),a=Y(s,c,u),i=Y(s,c,u-1/3)}return{r:Math.round(m*255),g:Math.round(a*255),b:Math.round(i*255),a:n}}function Y(e,r,t){return t<0&&(t+=1),t>1&&(t-=1),t<1/6?e+(r-e)*6*t:t<1/2?r:t<2/3?e+(r-e)*(2/3-t)*6:e}function X(e){let{h:r,s:t,l:o,a:n}={...e},m=f(n)?` / ${n}`:"";return`hsl(${r}, ${t}%, ${o}%${m})`}var M=class{color={...K};max=w;get h(){return this.color.h}set h(r){this.color.h=Math.round(r%this.max.h)}get s(){return this.color.s}set s(r){this.color.s=p(r,0,this.max.s)}get l(){return this.color.l}set l(r){this.color.l=p(r,0,this.max.l)}get a(){return this.color.a}set a(r){this.color.a=f(r)?p(Number(r),0,1):void 0}constructor(r,t,o,n){this.h=r,this.s=t,this.l=o,this.a=n}toString(){return X(this.color)}rgb(){let{r,g:t,b:o,a:n}=A(this.color);return new h(r,t,o,n)}};var N={r:0,g:0,b:0};var Z={r:255,g:255,b:255};function V(e){e=e.replace(/^#/,"");let r=parseInt(e.substring(0,2),16),t=parseInt(e.substring(2,4),16),o=parseInt(e.substring(4,6),16),n;return e.length===8&&(n=parseInt(e.substring(6,8),16)/255),{r,g:t,b:o,a:n}}function F(e){let{r,g:t,b:o,a:n}={...e};r/=255,t/=255,o/=255;let m=1-Math.max(r,t,o);if(m===1)return{c:0,m:0,y:0,k:100,a:n};let a=(1-r-m)/(1-m),i=(1-t-m)/(1-m),c=(1-o-m)/(1-m);return{c:Math.round(a*100),m:Math.round(i*100),y:Math.round(c*100),k:Math.round(m*100),a:n}}function E(e){let{r,g:t,b:o,a:n}={...e};return{g:Math.round((.299*r+.587*t+.114*o)*100/255),a:n}}function v(e){let{r,g:t,b:o,a:n}={...e},m=d(r),a=d(t),i=d(o),c=f(n)?d((n||1)*256-1):"";return`#${m}${a}${i}${c}`}function d(e){return Math.round(e).toString(16).padStart(2,"0")}function D(e){let{r,g:t,b:o,a:n}={...e};r/=255,t/=255,o/=255;let m=Math.max(r,t,o),a=Math.min(r,t,o),i=0,c=0,s=(m+a)/2;if(m===a)i=c=0;else{let l=m-a;switch(c=s>.5?l/(2-m-a):l/(m+a),m){case r:i=(t-o)/l+(t<o?6:0);break;case t:i=(o-r)/l+2;break;case o:i=(r-t)/l+4;break}i=i/6}let u=Math.round(i*360),b=Math.round(c*100),y=Math.round(s*100);return{h:u,s:b,l:y,a:n}}function R(e){let{x:r,y:t,z:o,alpha:n}={...e},m=95.047,a=100,i=108.883,c=r/m,s=t/a,u=o/i,b=c>.008856?Math.pow(c,1/3):7.787*c+16/116,y=s>.008856?Math.pow(s,1/3):7.787*s+16/116,l=u>.008856?Math.pow(u,1/3):7.787*u+16/116,G=Math.max(0,116*y-16),k=500*(b-y),S=200*(y-l);return G=Math.round(G*100)/100,k=Math.round(k*100)/100,S=Math.round(S*100)/100,{l:G,a:k,b:S,alpha:n}}function j(e){let r=sr(e);return R(r)}function sr(e){let{r,g:t,b:o,a:n}={...e};r/=255,t/=255,o/=255,r=r>.04045?Math.pow((r+.055)/1.055,2.4):r/12.92,t=t>.04045?Math.pow((t+.055)/1.055,2.4):t/12.92,o=o>.04045?Math.pow((o+.055)/1.055,2.4):o/12.92;let m=(r*.4124564+t*.3575761+o*.1804375)*100,a=(r*.2126729+t*.7151522+o*.072175)*100,i=(r*.0193339+t*.119192+o*.9503041)*100;return{x:m,y:a,z:i,alpha:n}}function J(e){let{r,g:t,b:o,a:n}={...e};return f(n)?`rgba(${r},${t},${o},${n})`:`rgb(${r},${t},${o})`}var h=class{color={...N};max=Z;get r(){return this.color.r}set r(r){this.color.r=p(r,0,this.max.r)}get g(){return this.color.g}set g(r){this.color.g=p(r,0,this.max.g)}get b(){return this.color.b}set b(r){this.color.b=p(r,0,this.max.b)}get a(){return this.color.a}set a(r){this.color.a=f(r)?p(Number(r),0,1):void 0}constructor(r=0,t=0,o=0,n){this.r=r,this.g=t,this.b=o,this.a=n}toString(){return J(this.color)}fromHex(r){let{r:t,g:o,b:n,a:m}=V(r);this.r=t,this.g=o,this.b=n,this.a=m}toHex(){return v(this.color)}cmyk(){let{c:r,m:t,y:o,k:n,a:m}=F(this.color);return new g(r,t,o,n,m)}gray(){let{g:r,a:t}=E(this.color);return new I(r,t)}hsl(){let{h:r,s:t,l:o,a:n}=D(this.color);return new M(r,t,o,n)}lab(){let{l:r,a:t,b:o,alpha:n}=j(this.color);return new B(r,t,o,n)}};var O={l:0,a:0,b:0};var P={l:100,a:127,b:127};function L(e){let{l:r,a:t,b:o,alpha:n}={...e},m=95.047,a=100,i=108.883,c=(r+16)/116,s=t/500+c,u=c-o/200,b=s>.206893?Math.pow(s,3):(s-16/116)/7.787,y=c>.206893?Math.pow(c,3):(c-16/116)/7.787,l=u>.206893?Math.pow(u,3):(u-16/116)/7.787;return{x:Math.max(0,b*m),y:Math.max(0,y*a),z:Math.max(0,l*i),alpha:n}}function Q(e){let r=L(e);return ur(r)}function ur(e){let{x:r,y:t,z:o,alpha:n}={...e},m=Math.min(1,r/95.047),a=Math.min(1,t/100),i=Math.min(1,o/108.883),c=1-m,s=1-a,u=1-i,b=Math.min(c,s,u),y=(c-b)/(1-b)||0,l=(s-b)/(1-b)||0,G=(u-b)/(1-b)||0;return{c:Math.round(Math.max(0,Math.min(1,y))*100),m:Math.round(Math.max(0,Math.min(1,l))*100),y:Math.round(Math.max(0,Math.min(1,G))*100),k:Math.round(Math.max(0,Math.min(1,b))*100),a:n}}function U(e){let r=L(e);return pr(r)}function pr(e){let{x:r,y:t,z:o,alpha:n}={...e},m=r/100,a=t/100,i=o/100,c=m*3.2404542+a*-1.5371385+i*-.4985314,s=m*-.969266+a*1.8760108+i*.041556,u=m*.0556434+a*-.2040259+i*1.0572252;return c=c>.0031308?1.055*Math.pow(c,1/2.4)-.055:12.92*c,s=s>.0031308?1.055*Math.pow(s,1/2.4)-.055:12.92*s,u=u>.0031308?1.055*Math.pow(u,1/2.4)-.055:12.92*u,{r:Math.round(Math.max(0,Math.min(1,c))*255),g:Math.round(Math.max(0,Math.min(1,s))*255),b:Math.round(Math.max(0,Math.min(1,u))*255),a:n}}function W(e){let{l:r,a:t,b:o,alpha:n}={...e},m=f(n)?` / ${n}`:"";return`lab(${r} ${t} ${o}${m})`}var B=class{color={...O};max=P;get l(){return this.color.l}set l(r){this.color.l=p(r,0,this.max.l)}get a(){return this.color.a}set a(r){this.color.a=p(r,-128,this.max.a)}get b(){return this.color.b}set b(r){this.color.b=p(r,-128,this.max.b)}get alpha(){return this.color.alpha}set alpha(r){this.color.alpha=f(r)?p(Number(r),0,1):void 0}constructor(r,t,o,n){this.l=r,this.a=t,this.b=o,this.alpha=n}toString(){return W(this.color)}cmyk(){let{c:r,m:t,y:o,k:n,a:m}=Q(this.color);return new g(r,t,o,n,m)}rgb(){let{r,g:t,b:o,a:n}=U(this.color);return new h(r,t,o,n)}};var _={c:0,m:0,y:0,k:0};var q={c:100,m:100,y:100,k:100};function rr(e){let r=fr(e);return R(r)}var x={c:{x:.1,y:.2,z:.8},m:{x:.6,y:.2,z:.4},y:{x:.8,y:.9,z:.1},k:{x:.05,y:.05,z:.05}};function fr(e){let{c:r,m:t,y:o,k:n,a:m}={...e};r/=100,t/=100,o/=100,n/=100;let a=br(r,t,o,n),i={x:95.047,y:100,z:108.883};return{x:a.x*i.x,y:a.y*i.y,z:a.z*i.z,alpha:m}}function br(e,r,t,o){let n={x:.95,y:.95,z:.95},m=1-o*.9;n.x*=m,n.y*=m,n.z*=m;let a={x:1-e*(1-x.c.x),y:1-e*(1-x.c.y),z:1-e*(1-x.c.z)};n.x*=a.x,n.y*=a.y,n.z*=a.z;let i={x:1-r*(1-x.m.x),y:1-r*(1-x.m.y),z:1-r*(1-x.m.z)};n.x*=i.x,n.y*=i.y,n.z*=i.z;let c={x:1-t*(1-x.y.x),y:1-t*(1-x.y.y),z:1-t*(1-x.y.z)};n.x*=c.x,n.y*=c.y,n.z*=c.z;let s=e+r+t+o,b=1-Math.min(.3,s*.1);return n.x*=b,n.y*=b,n.z*=b,{x:Math.max(0,Math.min(1,n.x)),y:Math.max(0,Math.min(1,n.y)),z:Math.max(0,Math.min(1,n.z))}}function tr(e){let{c:r,m:t,y:o,k:n,a:m}={...e};r/=100,t/=100,o/=100,n/=100;let a=Math.round(255*(1-r)*(1-n)),i=Math.round(255*(1-t)*(1-n)),c=Math.round(255*(1-o)*(1-n));return{r:a,g:i,b:c,a:m}}function or(e){let{c:r,m:t,y:o,k:n,a:m}={...e},a=f(m)?` ${m}`:"";return`${r} ${t} ${o} ${n}${a}`}var g=class{color={..._};max=q;get c(){return this.color.c}set c(r){this.color.c=p(r,0,this.max.c)}get m(){return this.color.m}set m(r){this.color.m=p(r,0,this.max.m)}get y(){return this.color.y}set y(r){this.color.y=p(r,0,this.max.y)}get k(){return this.color.k}set k(r){this.color.k=p(r,0,this.max.k)}get a(){return this.color.a}set a(r){this.color.a=f(r)?p(Number(r),0,1):void 0}constructor(r,t,o,n,m){this.c=r,this.m=t,this.y=o,this.k=n,this.a=m}sum(){let{c:r,m:t,y:o,k:n}=this.color;return r+t+o+n}toString(){return or(this.color)}lab(){let{l:r,a:t,b:o,alpha:n}=rr(this.color);return new B(r,t,o,n)}rgb(){let{r,g:t,b:o,a:n}=tr(this.color);return new h(r,t,o,n)}};var hr=(e,r,t,o,n)=>new g(e,r,t,o,n),lr=(e,r)=>new I(e,r),yr=e=>{let r=new h;return r.fromHex(e),r},gr=(e,r,t,o)=>new M(e,r,t,o),xr=(e,r,t,o)=>new h(e,r,t,o);0&&(module.exports={CMYK,Gray,HSL,RGB,cmyk,gray,hex,hsl,rgb});