huexjs
Version:
huexjs is a lightweight and efficient JavaScript library for seamless color conversions. Convert colors between HEX, RGB, HSL, CMYK, and more with ease. Designed for developers who need a reliable and fast solution for color transformations in web applica
2 lines (1 loc) • 2.25 kB
JavaScript
(function(l,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(l=typeof globalThis<"u"?globalThis:l||self,f(l.huexjs={}))})(this,function(l){"use strict";function f(t){let n=t.replace(/^#/,"");return n.length===3&&(n=n.split("").map(r=>r+r).join("")),/^([0-9A-F]{6})$/i.test(n)?{r:parseInt(n.substring(0,2),16),g:parseInt(n.substring(2,4),16),b:parseInt(n.substring(4,6),16)}:null}function h({r:t,g:n,b:r}){return`#${(1<<24|t<<16|n<<8|r).toString(16).slice(1).toUpperCase()}`}function d({r:t,g:n,b:r}){t/=255,n/=255,r/=255;let e=Math.max(t,n,r),i=Math.min(t,n,r),o=0,a,s=(e+i)/2;if(e===i)o=a=0;else{let u=e-i;switch(a=s>.5?u/(2-e-i):u/(e+i),e){case t:o=(n-r)/u+(n<r?6:0);break;case n:o=(r-t)/u+2;break;case r:o=(t-n)/u+4;break}o*=60}return{h:Math.round(o),s:Math.round(a*100),l:Math.round(s*100)}}function c({h:t,s:n,l:r}){n/=100,r/=100;let e=(1-Math.abs(2*r-1))*n,i=e*(1-Math.abs(t/60%2-1)),o=r-e/2,a=0,s=0,u=0;return t<60?(a=e,s=i):t<120?(a=i,s=e):t<180?(s=e,u=i):t<240?(s=i,u=e):t<300?(a=i,u=e):(a=e,u=i),{r:Math.round((a+o)*255),g:Math.round((s+o)*255),b:Math.round((u+o)*255)}}function M({r:t,g:n,b:r}){let e=1-t/255,i=1-n/255,o=1-r/255,a=Math.min(e,i,o);return a===1?{c:0,m:0,y:0,k:1}:{c:Math.round((e-a)/(1-a)*100),m:Math.round((i-a)/(1-a)*100),y:Math.round((o-a)/(1-a)*100),k:Math.round(a*100)}}function b({c:t,m:n,y:r,k:e}){return{r:Math.round(255*(1-t/100)*(1-e/100)),g:Math.round(255*(1-n/100)*(1-e/100)),b:Math.round(255*(1-r/100)*(1-e/100))}}function m({r:t,g:n,b:r}){t/=255,n/=255,r/=255;let e=Math.max(t,n,r),i=Math.min(t,n,r),o=0,a,s=e,u=e-i;if(a=e===0?0:u/e,e!==i){switch(e){case t:o=(n-r)/u+(n<r?6:0);break;case n:o=(r-t)/u+2;break;case r:o=(t-n)/u+4;break}o*=60}return{h:Math.round(o),s:Math.round(a*100),v:Math.round(s*100)}}function T({h:t,s:n,v:r}){n/=100,r/=100;let e=r*n,i=e*(1-Math.abs(t/60%2-1)),o=r-e,a=0,s=0,u=0;return t<60?(a=e,s=i):t<120?(a=i,s=e):t<180?(s=e,u=i):t<240?(s=i,u=e):t<300?(a=i,u=e):(a=e,u=i),{r:Math.round((a+o)*255),g:Math.round((s+o)*255),b:Math.round((u+o)*255)}}l.cmykToRgb=b,l.hexToRgb=f,l.hslToRgb=c,l.hsvToRgb=T,l.rgbToCmyk=M,l.rgbToHex=h,l.rgbToHsl=d,l.rgbToHsv=m,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});