@hueycolor/core 0.0.1-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ # MIT License
2
+
3
+ Copyright (c) 2025 Eihab Khan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const lr=[[1,.3963377773761749,.2158037573099136],[1,-.1055613458156586,-.0638541728258133],[1,-.0894841775298119,-1.2914855480194092]],fr=[[.210454268309314,.7936177747023054,-.0040720430116193],[1.9779985324311684,-2.42859224204858,.450593709617411],[.0259040424655478,.7827717124575296,-.8086757549230774]],hr=[[.819022437996703,.3619062600528904,-.1288737815209879],[.0329836539323885,.9292868615863434,.0361446663506424],[.0481771893596242,.2642395317527308,.6335478284694309]],_r=[[1.2268798758459243,-.5578149944602171,.2813910456659647],[-.0405757452148008,1.112286803280317,-.0717110580655164],[-.0763729366746601,-.4214933324022432,1.5869240198367816]],ur=[[.4123907992659595,.35758433938387796,.1804807884018343],[.21263900587151036,.7151686787677559,.07219231536073371],[.01933081871559185,.11919477979462599,.9505321522496606]],dr=[[3.2409699419045213,-1.5373831775700935,-.4986107602930033],[-.9692436362808798,1.8759675015077206,.04155505740717561],[.05563007969699361,-.20397695888897657,1.0569715142428786]],mr=[[.4122214694707629,.5363325372617349,.051445993267502196],[.2119034958178251,.6806995506452345,.10739695353694051],[.08830245919005637,.2817188391361215,.6299787016738223]],gr=[[4.076741636075959,-3.307711539258062,.2309699031821041],[-1.2684379732850313,2.6097573492876878,-.3413193760026569],[-.004196076138675526,-.703418617935936,1.7076146940746113]],br=[[.48657094864821626,.26566769316909294,.1982172852343625],[.22897456406974884,.6917385218365062,.079286914093745],[0,.045113381858902575,1.0439443689009757]],Mr=[[2.4934969119414245,-.9313836179191236,-.40271078445071684],[-.829488969561575,1.7626640603183468,.02362468584194359],[.035845830243784335,-.07617238926804171,.9568845240076873]],xr=[[.48137985274995443,.4621183710113181,.05650177623872757],[.22883194181124475,.6532168193835676,.11795123880518778],[.08394575232299319,.22416527097756642,.6918889766994405]],pr=[[3.1277689713618737,-2.2571357625916395,.1293667912297651],[-1.091009018437798,2.413331710306923,-.3223226918691248],[-.02601080193857041,-.5080413317041669,1.5340521336427373]],wr=[[.6369580483012913,.14461690358620838,.16888097516417205],[.26270021201126703,.677998071518871,.059301716469861945],[0,.028072693049087508,1.0609850577107909]],$r=[[1.7166511879712676,-.3556707837763924,-.2533662813736598],[-.666684351832489,1.616481236634939,.01576854581391113],[.017639857445310915,-.042770613257808655,.942103121235474]],Lr=[[.6167557848654444,.3601984012264634,.023045813908092266],[.26513305939263676,.6358393720678492,.09902756853951414],[.10010262952034828,.20390652261661452,.6959908478630372]],Nr=[[2.1399067304346513,-1.246389493760618,.10648276332596689],[-.8847358357577675,2.1632309383612007,-.27849510260343363],[-.04857374640044396,-.45450314971409633,1.5030768961145404]],K=(r,t,e)=>Math.max(Math.min(r,e),t),Xr=r=>(r%360+360)%360,Fr=(r,t=b())=>{let e=r.replace(/#/,"");e.length===3?e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]:e.length>6&&(e=e.slice(0,6));const n=parseInt(e,16);return t[0]=(n>>16&255)/255,t[1]=(n>>8&255)/255,t[2]=(n&255)/255,t},Hr=r=>`#${r.map(t=>L(t).toString(16).padStart(2,"0")).join("")}`,L=r=>K(Math.round(255*r),0,255),b=()=>[0,0,0],x=b(),Er=r=>{const t=r[0],e=r[1],n=r[2];r[0]=t*t*t,r[1]=e*e*e,r[2]=n*n*n},Rr=r=>{r[0]=Math.cbrt(r[0]),r[1]=Math.cbrt(r[1]),r[2]=Math.cbrt(r[2])},R=(r,t)=>r[0]*t[0]+r[1]*t[1]+r[2]*t[2],yr=(r,t,e=b())=>($(r,lr,e),Er(e),$(e,t,e)),O=(r,t,e=b())=>($(r,t,e),Rr(e),$(e,fr,e)),$=(r,t,e=b())=>{const n=R(r,t[0]),i=R(r,t[1]),o=R(r,t[2]);return e[0]=n,e[1]=i,e[2]=o,e},v=(r,t)=>{t[0]=r[0],t[1]=r[1],t[2]=r[2]},Yr=(r,t,e=t)=>{if(!t)throw new Error("must specify an input space");let n=1;r.length>3&&(n=r[3]),v(r,x),t!==e&&g(r,t,e,x);const i=e.id;if(i=="srgb"){const o=L(x[0]),l=L(x[1]),h=L(x[2]),f=`${o}, ${l}, ${h}`;return n===1?`rgb(${f})`:`rgba(${f}, ${n})`}else{const o=n===1?"":` / ${n}`;return i=="oklab"||i=="oklch"?`${i}(${x[0]*100}% ${x[1]} ${x[2]}${o})`:`color(${i} ${x[0]} ${x[1]} ${x[2]}${o})`}},A=r=>r.length>=4&&r[3]===1?r.slice(0,3):r,y=r=>parseFloat(r)||0,P=(r,t=!1)=>t?K(y(r)/255,0,255):r.includes("%")?y(r)/100:y(r),Sr=r=>{if(typeof r!="string")throw new Error("expected a string as input");if(r=r.trim(),r.charAt(0)==="#"){const t=r.slice(0,7);let e=r.length>7?parseInt(r.slice(7,9),16):255,n=isNaN(e)?1:e/255;const i=Fr(t);return n!==1&&i.push(n),{id:"srgb",coords:i}}else{const t=/^(rgb|rgba|oklab|oklch|color)\((.+)\)$/i.exec(r);if(!t)throw new Error(`could not parse color string ${r}`);const e=t[1].toLowerCase();if(/^rgba?$/i.test(e)&&t[2].includes(",")){const n=t[2].split(",").map((i,o)=>P(i.trim(),o<3));return{id:"srgb",coords:A(n)}}else{let n,i,o=!1;if(/^color$/i.test(e)){const h=/([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s/]+)(?:\s?\/\s?([^\s]+))?/.exec(t[2]);if(!h)throw new Error(`could not parse color() function ${r}`);n=h[1].toLowerCase(),i=h.slice(2,6)}else{if(/^(oklab|oklch)$/i.test(e))n=e;else if(/rgba?/i.test(e))n="srgb",o=!0;else throw new Error(`unknown color function ${e}`);const h=/([^\s]+)\s+([^\s]+)\s+([^\s/]+)(?:\s?\/\s?([^\s]+))?/.exec(t[2]);if(!h)throw new Error(`could not parse color() function ${r}`);i=h.slice(1,6)}i[3]==null&&(i=i.slice(0,3));const l=i.map((h,f)=>P(h.trim(),o&&f<3));if(l.length<3||l.length>4)throw new Error("invalid number of coordinates");return{id:n,coords:A(l)}}}},g=(r,t,e,n=b())=>{if(v(r,n),!t)throw new Error("must specify a fromSpace");if(!e)throw new Error("must specify a toSpace");if(t==e)return n;t.base&&(n=t.toBase(n,n),t=t.base);let i=t,o=e.base??e;if(t.base||o.base)throw new Error("Currently only base of depth=1 is supported");if(i!==o){let l=i.id==="xyz",h=o.id==="xyz",f=!1,s=!1;if(i.id==="oklab"){let c=o.fromLMS_M;c||(c=z.fromLMS_M,f=!0,l=!0),n=yr(n,c,n)}else if(o.id==="oklab"){let c=i.toLMS_M;c?n=O(n,c,n):(f=!0,s=!0)}else f=!0;if(f){if(!l)if(i.toXYZ)n=i.toXYZ(n,n);else if(i.toXYZ_M)n=$(n,i.toXYZ_M,n);else throw new Error(`no toXYZ or toXYZ_M on ${i.id}`);if(i.adapt&&(n=$(n,i.adapt.to,n)),o.adapt&&(n=$(n,o.adapt.from,n)),!h)if(s)n=O(n,z.toLMS_M,n);else if(o.fromXYZ)n=o.fromXYZ(n,n);else if(o.fromXYZ_M)n=$(n,o.fromXYZ_M,n);else throw new Error(`no fromXYZ or fromXYZ_M on ${o.id}`)}}if(o!==e)if(e.fromBase)n=e.fromBase(n,n);else throw new Error(`could not transform ${o.id} to ${e.id}`);return n},z={toLMS_M:hr,fromLMS_M:_r},Y=r=>{let t=r<0?-1:1,e=Math.abs(r);return e<=.04045?r/12.92:t*Math.pow((e+.055)/1.055,2.4)},S=r=>{let t=r<0?-1:1,e=Math.abs(r);return e>.0031308?t*(1.055*Math.pow(e,1/2.4)-.055):12.92*r},V=(r,t=b())=>(t[0]=S(r[0]),t[1]=S(r[1]),t[2]=S(r[2]),t),j=(r,t=b())=>(t[0]=Y(r[0]),t[1]=Y(r[1]),t[2]=Y(r[2]),t),Br={id:"srgb-linear",toXYZ_M:ur,fromXYZ_M:dr,toLMS_M:mr,fromLMS_M:gr},w={id:"srgb",base:Br,toBase:j,fromBase:V},D=(.4-0)/1e5,Cr={id:"oklab"},p={id:"oklch",base:Cr,toBase:(r,t=b())=>{const e=r[1],n=r[2];return t[0]=r[0],t[1]=e*Math.cos(n*Math.PI/180),t[2]=e*Math.sin(n*Math.PI/180),t},fromBase:(r,t=b())=>{const e=r[1],n=r[2];let i=Math.abs(e)<D&&Math.abs(n)<D,o=i?0:Xr(Math.atan2(n,e)*180/Math.PI),l=i?0:Math.sqrt(e*e+n*n);return t[0]=r[0],t[1]=l,t[2]=o,t}},Tr={id:"display-p3-linear",toXYZ_M:br,fromXYZ_M:Mr,toLMS_M:xr,fromLMS_M:pr},kr={id:"display-p3",base:Tr,toBase:j,fromBase:V},F=1.09929682680944,I=.018053968510807,B=r=>r<I*4.5?r/4.5:Math.pow((r+F-1)/F,1/.45),C=r=>r>=I?F*Math.pow(r,.45)-(F-1):4.5*r,Zr={id:"rec2020-linear",toXYZ_M:wr,fromXYZ_M:$r,toLMS_M:Lr,fromLMS_M:Nr},Gr={id:"rec2020",base:Zr,toBase:(r,t=b())=>(t[0]=B(r[0]),t[1]=B(r[1]),t[2]=B(r[2]),t),fromBase:(r,t=b())=>(t[0]=C(r[0]),t[1]=C(r[1]),t[2]=C(r[2]),t)},T=Symbol("huey-color"),U=/^#?(?:[A-F0-9]{8}|[A-F0-9]{6}|[A-F0-9]{3})$/i,W=/^rgba?\(\s*(\d+)\s*[,\s]\s*(\d+)\s*[,\s]\s*(\d+)\s*(?:[,/]\s*([\d.]+)\s*)?\)$/i,k=/^hsla?\(\s*([\d.]+)(?:deg|rad|grad|turn)?\s*[,\s]\s*([\d.]+)%?\s*[,\s]\s*([\d.]+)%?\s*(?:[,/]\s*([\d.]+))?\s*\)$/i,Z=/^oklch\(\s*([\d.]+%?)\s*[,\s]\s*([\d.]+)\s*[,\s]\s*([\d.]+)(?:deg|rad|grad|turn)?\s*(?:[,/]\s*([\d.]+))?\s*\)$/i;function q(r){return typeof r=="object"&&r!==null&&T in r&&r[T]===!0}function J(r){return U.test(r.trim())}function Q(r){const t=W.exec(r);if(!t)return!1;const[,e,n,i,o]=t;return[Number.parseInt(e),Number.parseInt(n),Number.parseInt(i)].some(h=>h<0||h>255)?!1:o!==void 0?E(o):!0}function rr(r){const t=k.exec(r);if(!t)return!1;const[,e,n,i,o]=t;return!G(e)||!H(n)||!H(i)?!1:o!==void 0?E(o):!0}function tr(r){const t=Z.exec(r);if(!t)return!1;const[,e,n,i,o]=t;return!H(e)||!er(n)||!G(i)?!1:o!==void 0?E(o):!0}function H(r){const t=typeof r=="string"?Number.parseFloat(r):r;return t>=0&&t<=1||t>=0&&t<=100}function er(r){return(typeof r=="string"?Number.parseFloat(r):r)>=0}function G(r){const t=typeof r=="string"?Number.parseFloat(r):r;return!(t<0||t>360)}function E(r){const t=typeof r=="string"?Number.parseFloat(r):r;return t>=0&&t<=1}function nr(r){const t=r.trim();return J(t)?"hex":Q(t)?"rgb":rr(t)?"hsl":tr(t)?"oklch":"unknown"}function sr(){const r=window.scrollX||window.pageXOffset||document.documentElement.scrollLeft||0,t=window.scrollY||window.pageYOffset||document.documentElement.scrollTop||0;return{x:r,y:t}}function Or(r){const{x:t,y:e}=sr(),n=r.getBoundingClientRect();return{x:n.left+t,y:n.top+e}}function Ar(r){const t={x:0,y:0};return r instanceof MouseEvent&&(t.x=r.pageX,t.y=r.pageY),typeof TouchEvent<"u"&&r instanceof TouchEvent&&(t.x=r.touches?r.touches[0].pageX:r.changedTouches?r.changedTouches[0].pageX:0,t.y=r.touches?r.touches[0].pageY:r.changedTouches?r.changedTouches[0].pageY:0),t}function Pr(r){return{ArrowUp:"up",ArrowDown:"down",ArrowLeft:"left",ArrowRight:"right"}[r.code]}function zr(r,t){let e=0;switch(t){case"down":e=-1;break;case"up":e=1;break}return r+e}function Dr(r,t,e){return Math.min(Math.max(r,t),e)}function Kr(r){let e=0;return["r","g","b"].includes(r)&&(e=255),r==="h"&&(e=360),["s","l","a"].includes(r)&&(e=100),{min:0,max:e}}function vr(r){return Math.trunc(Number(r.replace(/[%°]/g,"").trim()))}function Vr(r,t,e,n=0,i=1){const o=(r-t)/(e-t);return(i-n)*o+n}function jr(r,t){const e=t<1?Math.ceil(-Math.log10(t)):0;return Math.round(r*10**e)/10**e}function or(r){const t=k.exec(r.trim());if(!t)return null;const[,e,n,i,o]=t;if(!e||!n||!i)return null;const l=Number.parseFloat(e),h=Number.parseFloat(n),f=Number.parseFloat(i),s=o?Number.parseFloat(o):1;if(Number.isNaN(l)||Number.isNaN(h)||Number.isNaN(f)||h<0||h>100||f<0||f>100||s<0||s>1)return null;const{r:c,g:a,b:u}=X(l,h,f);return{coords:[c/255,a/255,u/255,s],space:"rgb"}}function ir(r){const t=Z.exec(r.trim());if(!t)return null;const[,e,n,i,o]=t;if(!e||!n||!i)return null;let l=Number.parseFloat(e.replace("%",""));const h=Number.parseFloat(n),f=Number.parseFloat(i),s=o?Number.parseFloat(o):1;return Number.isNaN(l)||Number.isNaN(h)||Number.isNaN(f)||(e.includes("%")&&(l=l/100),l<0||l>1)||h<0||s<0||s>1?null:{coords:[l,h,f,s],space:"oklch"}}function X(r,t,e,n=!1){const i=r/360,o=t/100,l=e/100;let h,f,s;if(o===0)h=f=s=l;else{const c=(_,m,d)=>(d<0&&(d+=1),d>1&&(d-=1),d<.16666666666666666?_+(m-_)*6*d:d<.5?m:d<.6666666666666666?_+(m-_)*(.6666666666666666-d)*6:_),a=l<.5?l*(1+o):l+o-l*o,u=2*l-a;h=c(u,a,i+1/3),f=c(u,a,i),s=c(u,a,i-1/3)}return n?{r:h,g:f,b:s}:{r:h*255,g:f*255,b:s*255}}function cr(r,t,e){const n=Math.max(r,t,e),i=Math.min(r,t,e),o=n-i;let l=0,h=0;const f=(n+i)/2;if(o!==0)switch(h=f>.5?o/(2-n-i):o/(n+i),n){case r:l=((t-e)/o+(t<e?6:0))/6;break;case t:l=((e-r)/o+2)/6;break;case e:l=((r-t)/o+4)/6;break}return{h:l*360,s:h*100,l:f*100}}function Ir(r,t,e){const n=t/100,i=e/100,o=i+n*Math.min(i,1-i),l=o===0?0:2*(1-i/o);return{h:r,s:l*100,v:o*100}}function Ur(r,t,e){const n=t/100,i=e/100,o=i*(1-n/2),l=o===0||o===1?0:(i-o)/Math.min(o,1-o);return{h:r,s:l*100,l:o*100}}function ar(r){if(q(r))return r;const t=nr(r);if(t==="unknown")throw new Error(`invalid color provided: ${r}`);let e,n;if(t==="hsl"){const c=or(r);if(!c)throw new Error(`invalid color provided: ${r}`);e=c.coords,n=e[3]??1}else if(t==="oklch"){const c=ir(r);if(!c)throw new Error(`invalid color provided: ${r}`);e=c.coords,n=e[3]??1}else{const c=t==="hex"&&!r.startsWith("#")?`#${r}`:r;e=Sr(c).coords,n=e[3]??1}const i=t==="oklch"?e.slice(0,3):g(e.slice(0,3),w,p),[o,l,h]=i,f=(c,a,u,_)=>{const m=Math.max(0,Math.min(1,c)),d=ar(`oklch(${m*100}% ${a} ${u} / ${_})`);return d.getFormat=()=>t,d.getOriginalInput=()=>r,d},s={[T]:!0,_l:Number(o.toFixed(4)),_c:Number(l.toFixed(4)),_h:Number(h.toFixed(4)),_a:n,getFormat:()=>t,getOriginalInput:()=>r,getAlpha:()=>s._a,setAlpha:c=>f(s._l,s._c,s._h,c),setHue:c=>{const a=s.toHsl(),{r:u,g:_,b:m}=X(c,a.s,a.l,!0),[d,M,N]=g([u,_,m],w,p);return f(d,M,N,s._a)},setSaturation:c=>{const a=s.toHsl(),{r:u,g:_,b:m}=X(a.h,c,a.l,!0),[d,M,N]=g([u,_,m],w,p);return f(d,M,N,s._a)},setLightness:c=>{const a=s.toHsl(),{r:u,g:_,b:m}=X(a.h,a.s,c,!0),[d,M,N]=g([u,_,m],w,p);return f(d,M,N,s._a)},desaturate:c=>f(s._l,Math.max(0,s._c-c),s._h,s._a),saturate:c=>f(s._l,Math.max(0,s._c+c),s._h,s._a),brighten:c=>f(Math.min(1,s._l+c),s._c,s._h,s._a),lighten:c=>f(Math.min(1,s._l+c),s._c,s._h,s._a),darken:c=>f(Math.max(0,s._l-c),s._c,s._h,s._a),randomize:()=>f(Math.random(),Math.random()*.4,Math.random()*360,s._a),clone:()=>f(s._l,s._c,s._h,s._a),contrastRatio:c=>{const a=s.getLuminance(),u=c.getLuminance(),_=Math.max(a,u),m=Math.min(a,u);return(_+.05)/(m+.05)},getLuminance:()=>{const[c,a,u]=g([s._l,s._c,s._h],p,w),_=m=>m<=.03928?m/12.92:((m+.055)/1.055)**2.4;return .2126*_(c)+.7152*_(a)+.0722*_(u)},getBrightness:()=>s._l,isLight:()=>s._l>.5,isDark:()=>s._l<=.5,toHsl:()=>{const[c,a,u]=g([s._l,s._c,s._h],p,w),{h:_,s:m,l:d}=cr(c,a,u);return{h:Number(_),s:Number(m.toFixed(2)),l:Number(d.toFixed(2)),a:Number(s._a.toFixed(1))}},toHslString:()=>{const{h:c,s:a,l:u}=s.toHsl(),_=Number(s._a.toFixed(1));return _<1?`hsla(${c.toFixed(0)}, ${a.toFixed(0)}%, ${u.toFixed(0)}%, ${_})`:`hsl(${c.toFixed(0)}, ${a.toFixed(0)}%, ${u.toFixed(0)}%)`},toHex:()=>{let c=s.toHexString().replace("#","");return n===1&&(c=c.slice(0,6)),c},toHexString:()=>{const c=g([s._l,s._c,s._h],p,w);let a=Hr([...c,s._a]);return n===1&&(a=a.slice(0,7)),a},toRgb:()=>{const[c,a,u]=g([s._l,s._c,s._h],p,w);return{r:L(c),g:L(a),b:L(u),a:Number(s._a.toFixed(1))}},toRgbString:()=>{const c=g([s._l,s._c,s._h],p,w);return Yr([...c,Number(s._a.toFixed(1))],w)},toString:()=>t==="hex"?s.toHexString():t==="hsl"?s.toHslString():t==="oklch"?s.toOklchString():t==="rgb"?s.toRgbString():"",toOklchString:()=>{const c=(s._l*100).toFixed(2),a=s._c.toFixed(2),u=s._h.toFixed(2),_=s._a;return _<1?`oklch(${c}% ${a} ${u} / ${_})`:`oklch(${c}% ${a} ${u})`},toDisplayP3:()=>{const[c,a,u]=g([s._l,s._c,s._h],p,kr),_=c.toFixed(2),m=a.toFixed(2),d=u.toFixed(2),M=s._a;return M<1?`color(display-p3 ${_} ${m} ${d} / ${M})`:`color(display-p3 ${_} ${m} ${d})`},toRec2020:()=>{const[c,a,u]=g([s._l,s._c,s._h],p,Gr),_=c.toFixed(2),m=a.toFixed(2),d=u.toFixed(2),M=s._a;return M<1?`color(rec2020 ${_} ${m} ${d} / ${M})`:`color(rec2020 ${_} ${m} ${d})`}};return s}exports.HEX_REGEX=U;exports.HSL_REGEX=k;exports.OKLCH_REGEX=Z;exports.RGB_REGEX=W;exports.bumpColorValue=zr;exports.clamp=Dr;exports.getAbsolutePosition=Or;exports.getChannelBounds=Kr;exports.getFormat=nr;exports.getPageXYFromEvent=Ar;exports.getScrollXY=sr;exports.hslToHsv=Ir;exports.hslToRgb=X;exports.hsvToHsl=Ur;exports.hueyColor=ar;exports.isAlpha=E;exports.isChroma=er;exports.isHex=J;exports.isHsl=rr;exports.isHue=G;exports.isHuey=q;exports.isOklch=tr;exports.isPercentage=H;exports.isRgb=Q;exports.normalize=Vr;exports.parseHSL=or;exports.parseOKLCH=ir;exports.resolveArrowDirection=Pr;exports.rgbToHsl=cr;exports.roundToStep=jr;exports.truncColorValue=vr;
@@ -0,0 +1,198 @@
1
+ export declare type AlphaChannel = 'a';
2
+
3
+ export declare type ArrowDirection = 'up' | 'down' | 'left' | 'right';
4
+
5
+ export declare function bumpColorValue(value: number, direction: Omit<ArrowDirection, 'left' | 'right'> | null): number;
6
+
7
+ export declare function clamp(value: number, min: number, max: number): number;
8
+
9
+ export declare type ColorChannel = RgbChannel | HslChannel | AlphaChannel;
10
+
11
+ export declare type ColorFormat = 'hex' | 'rgb' | 'hsl' | 'oklch';
12
+
13
+ export declare interface Coordinates {
14
+ x: number;
15
+ y: number;
16
+ }
17
+
18
+ /** get the position of the container relative to the document’s edge, regardless of any scrolling that has occurred */
19
+ export declare function getAbsolutePosition(container: HTMLElement): Coordinates;
20
+
21
+ export declare function getChannelBounds(key: ColorChannel): {
22
+ min: number;
23
+ max: number;
24
+ };
25
+
26
+ export declare function getFormat(input: string): ColorFormat | 'unknown';
27
+
28
+ export declare function getPageXYFromEvent(e: MouseEvent | TouchEvent): Coordinates;
29
+
30
+ export declare function getScrollXY(): Coordinates;
31
+
32
+ export declare const HEX_REGEX: RegExp;
33
+
34
+ export declare const HSL_REGEX: RegExp;
35
+
36
+ export declare interface HSLA {
37
+ h: number;
38
+ s: number;
39
+ l: number;
40
+ a: number;
41
+ }
42
+
43
+ export declare type HslChannel = 'h' | 's' | 'l';
44
+
45
+ /**
46
+ * Convert HSL to HSV
47
+ * Input: h in 0-360, s and l in 0-100
48
+ * Output: { h, s, v } where h is 0-360, s and v are 0-100
49
+ */
50
+ export declare function hslToHsv(h: number, s: number, l: number): {
51
+ h: number;
52
+ s: number;
53
+ v: number;
54
+ };
55
+
56
+ /**
57
+ * Convert HSL to RGB
58
+ * Input: h, s, l in 0-1 range
59
+ * Output: [r, g, b] in 0-1 range
60
+ */
61
+ export declare function hslToRgb(_h: number, _s: number, _l: number, asVector?: boolean): Omit<RGBA, 'a'>;
62
+
63
+ /**
64
+ * Convert HSV to HSL
65
+ * Input: h in 0-360, s and v in 0-100
66
+ * Output: { h, s, l } where h is 0-360, s and l are 0-100
67
+ */
68
+ export declare function hsvToHsl(h: number, s: number, v: number): {
69
+ h: number;
70
+ s: number;
71
+ l: number;
72
+ };
73
+
74
+ export declare const HUEY_COLOR: unique symbol;
75
+
76
+ export declare interface HueyColor {
77
+ _l: OKLCH['l'];
78
+ _c: OKLCH['c'];
79
+ _h: OKLCH['h'];
80
+ _a: OKLCH['a'];
81
+ getFormat: () => ColorFormat;
82
+ getOriginalInput: () => string;
83
+ getAlpha: () => number;
84
+ getLuminance: () => number;
85
+ getBrightness: () => number;
86
+ isLight: () => boolean;
87
+ isDark: () => boolean;
88
+ setAlpha: (a: number) => HueyColor;
89
+ setHue: (h: number) => HueyColor;
90
+ setSaturation: (s: number) => HueyColor;
91
+ setLightness: (l: number) => HueyColor;
92
+ toHsl: () => HSLA;
93
+ toHslString: () => string;
94
+ toHex: () => string;
95
+ toHexString: () => string;
96
+ toRgb: () => RGBA;
97
+ toRgbString: () => string;
98
+ toRec2020: () => string;
99
+ toOklchString: () => string;
100
+ toDisplayP3: () => string;
101
+ toString: () => string;
102
+ lighten: (v: number) => HueyColor;
103
+ brighten: (v: number) => HueyColor;
104
+ darken: (v: number) => HueyColor;
105
+ saturate: (v: number) => HueyColor;
106
+ desaturate: (v: number) => HueyColor;
107
+ contrastRatio: (c: HueyColor) => number;
108
+ randomize: () => HueyColor;
109
+ clone: () => HueyColor;
110
+ }
111
+
112
+ export declare function hueyColor(colorInput: string | HueyColor): HueyColor;
113
+
114
+ export declare interface HueyColorSymbol extends HueyColor {
115
+ [HUEY_COLOR]: true;
116
+ }
117
+
118
+ export declare interface HueyError {
119
+ type: 'invalid-format' | 'parse-error' | 'unsupported-format';
120
+ input: string;
121
+ format: string;
122
+ message: string;
123
+ }
124
+
125
+ export declare interface HueyOptions {
126
+ fallback?: string;
127
+ onError?: (error: HueyError) => void;
128
+ }
129
+
130
+ export declare function isAlpha(a: string | number): boolean;
131
+
132
+ export declare function isChroma(c: number | string): boolean;
133
+
134
+ export declare function isHex(str: string): boolean;
135
+
136
+ export declare function isHsl(str: string): boolean;
137
+
138
+ export declare function isHue(h: number | string): boolean;
139
+
140
+ export declare function isHuey(value: unknown): value is HueyColor;
141
+
142
+ export declare function isOklch(str: string): boolean;
143
+
144
+ export declare function isPercentage(v: number | string): boolean;
145
+
146
+ export declare function isRgb(str: string): boolean;
147
+
148
+ export declare function normalize(number: number, currentScaleMin: number, currentScaleMax: number, newScaleMin?: number, newScaleMax?: number): number;
149
+
150
+ export declare interface OKLCH {
151
+ l: number;
152
+ c: number;
153
+ h: number;
154
+ a: number;
155
+ }
156
+
157
+ export declare const OKLCH_REGEX: RegExp;
158
+
159
+ declare interface ParsedColor {
160
+ coords: number[];
161
+ space: 'rgb' | 'oklch';
162
+ }
163
+
164
+ /**
165
+ * Parse HSL string and convert to RGB [0-1 range]
166
+ */
167
+ export declare function parseHSL(str: string): ParsedColor | null;
168
+
169
+ /**
170
+ * Parse OKLCH string
171
+ */
172
+ export declare function parseOKLCH(str: string): ParsedColor | null;
173
+
174
+ export declare function resolveArrowDirection(e: KeyboardEvent): ArrowDirection;
175
+
176
+ export declare const RGB_REGEX: RegExp;
177
+
178
+ export declare interface RGBA {
179
+ r: number;
180
+ g: number;
181
+ b: number;
182
+ a: number;
183
+ }
184
+
185
+ export declare type RgbChannel = 'r' | 'g' | 'b';
186
+
187
+ /**
188
+ * Convert RGB to HSL
189
+ * Input: r, g, b in 0-1 range
190
+ * Output: { h, s, l } where h is 0-360, s and l are 0-100
191
+ */
192
+ export declare function rgbToHsl(r: number, g: number, b: number): Omit<HSLA, 'a'>;
193
+
194
+ export declare function roundToStep(value: number, step: number): number;
195
+
196
+ export declare function truncColorValue(value: string): number;
197
+
198
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,579 @@
1
+ const U = [
2
+ [1, 0.3963377773761749, 0.2158037573099136],
3
+ [1, -0.1055613458156586, -0.0638541728258133],
4
+ [1, -0.0894841775298119, -1.2914855480194092]
5
+ ], W = [
6
+ [0.210454268309314, 0.7936177747023054, -0.0040720430116193],
7
+ [1.9779985324311684, -2.42859224204858, 0.450593709617411],
8
+ [0.0259040424655478, 0.7827717124575296, -0.8086757549230774]
9
+ ], q = [
10
+ [0.819022437996703, 0.3619062600528904, -0.1288737815209879],
11
+ [0.0329836539323885, 0.9292868615863434, 0.0361446663506424],
12
+ [0.0481771893596242, 0.2642395317527308, 0.6335478284694309]
13
+ ], J = [
14
+ [1.2268798758459243, -0.5578149944602171, 0.2813910456659647],
15
+ [-0.0405757452148008, 1.112286803280317, -0.0717110580655164],
16
+ [-0.0763729366746601, -0.4214933324022432, 1.5869240198367816]
17
+ ], Q = [
18
+ [0.4123907992659595, 0.35758433938387796, 0.1804807884018343],
19
+ [0.21263900587151036, 0.7151686787677559, 0.07219231536073371],
20
+ [0.01933081871559185, 0.11919477979462599, 0.9505321522496606]
21
+ ], tt = [
22
+ [3.2409699419045213, -1.5373831775700935, -0.4986107602930033],
23
+ [-0.9692436362808798, 1.8759675015077206, 0.04155505740717561],
24
+ [0.05563007969699361, -0.20397695888897657, 1.0569715142428786]
25
+ ], rt = [
26
+ [0.4122214694707629, 0.5363325372617349, 0.051445993267502196],
27
+ [0.2119034958178251, 0.6806995506452345, 0.10739695353694051],
28
+ [0.08830245919005637, 0.2817188391361215, 0.6299787016738223]
29
+ ], et = [
30
+ [4.076741636075959, -3.307711539258062, 0.2309699031821041],
31
+ [-1.2684379732850313, 2.6097573492876878, -0.3413193760026569],
32
+ [-0.004196076138675526, -0.703418617935936, 1.7076146940746113]
33
+ ], nt = [
34
+ [0.48657094864821626, 0.26566769316909294, 0.1982172852343625],
35
+ [0.22897456406974884, 0.6917385218365062, 0.079286914093745],
36
+ [0, 0.045113381858902575, 1.0439443689009757]
37
+ ], st = [
38
+ [2.4934969119414245, -0.9313836179191236, -0.40271078445071684],
39
+ [-0.829488969561575, 1.7626640603183468, 0.02362468584194359],
40
+ [0.035845830243784335, -0.07617238926804171, 0.9568845240076873]
41
+ ], ot = [
42
+ [0.48137985274995443, 0.4621183710113181, 0.05650177623872757],
43
+ [0.22883194181124475, 0.6532168193835676, 0.11795123880518778],
44
+ [0.08394575232299319, 0.22416527097756642, 0.6918889766994405]
45
+ ], ct = [
46
+ [3.1277689713618737, -2.2571357625916395, 0.1293667912297651],
47
+ [-1.091009018437798, 2.413331710306923, -0.3223226918691248],
48
+ [-0.02601080193857041, -0.5080413317041669, 1.5340521336427373]
49
+ ], it = [
50
+ [0.6369580483012913, 0.14461690358620838, 0.16888097516417205],
51
+ [0.26270021201126703, 0.677998071518871, 0.059301716469861945],
52
+ [0, 0.028072693049087508, 1.0609850577107909]
53
+ ], at = [
54
+ [1.7166511879712676, -0.3556707837763924, -0.2533662813736598],
55
+ [-0.666684351832489, 1.616481236634939, 0.01576854581391113],
56
+ [0.017639857445310915, -0.042770613257808655, 0.942103121235474]
57
+ ], lt = [
58
+ [0.6167557848654444, 0.3601984012264634, 0.023045813908092266],
59
+ [0.26513305939263676, 0.6358393720678492, 0.09902756853951414],
60
+ [0.10010262952034828, 0.20390652261661452, 0.6959908478630372]
61
+ ], ft = [
62
+ [2.1399067304346513, -1.246389493760618, 0.10648276332596689],
63
+ [-0.8847358357577675, 2.1632309383612007, -0.27849510260343363],
64
+ [-0.04857374640044396, -0.45450314971409633, 1.5030768961145404]
65
+ ], P = (t, r, e) => Math.max(Math.min(t, e), r), _t = (t) => (t % 360 + 360) % 360, ht = (t, r = b()) => {
66
+ let e = t.replace(/#/, "");
67
+ e.length === 3 ? e = e[0] + e[0] + e[1] + e[1] + e[2] + e[2] : e.length > 6 && (e = e.slice(0, 6));
68
+ const n = parseInt(e, 16);
69
+ return r[0] = (n >> 16 & 255) / 255, r[1] = (n >> 8 & 255) / 255, r[2] = (n & 255) / 255, r;
70
+ }, ut = (t) => `#${t.map((r) => L(r).toString(16).padStart(2, "0")).join("")}`, L = (t) => P(Math.round(255 * t), 0, 255), b = () => [0, 0, 0], x = b(), dt = (t) => {
71
+ const r = t[0], e = t[1], n = t[2];
72
+ t[0] = r * r * r, t[1] = e * e * e, t[2] = n * n * n;
73
+ }, mt = (t) => {
74
+ t[0] = Math.cbrt(t[0]), t[1] = Math.cbrt(t[1]), t[2] = Math.cbrt(t[2]);
75
+ }, y = (t, r) => t[0] * r[0] + t[1] * r[1] + t[2] * r[2], gt = (t, r, e = b()) => (p(t, U, e), dt(e), p(e, r, e)), C = (t, r, e = b()) => (p(t, r, e), mt(e), p(e, W, e)), p = (t, r, e = b()) => {
76
+ const n = y(t, r[0]), c = y(t, r[1]), o = y(t, r[2]);
77
+ return e[0] = n, e[1] = c, e[2] = o, e;
78
+ }, z = (t, r) => {
79
+ r[0] = t[0], r[1] = t[1], r[2] = t[2];
80
+ }, bt = (t, r, e = r) => {
81
+ if (!r) throw new Error("must specify an input space");
82
+ let n = 1;
83
+ t.length > 3 && (n = t[3]), z(t, x), r !== e && g(t, r, e, x);
84
+ const c = e.id;
85
+ if (c == "srgb") {
86
+ const o = L(x[0]), l = L(x[1]), _ = L(x[2]), f = `${o}, ${l}, ${_}`;
87
+ return n === 1 ? `rgb(${f})` : `rgba(${f}, ${n})`;
88
+ } else {
89
+ const o = n === 1 ? "" : ` / ${n}`;
90
+ return c == "oklab" || c == "oklch" ? `${c}(${x[0] * 100}% ${x[1]} ${x[2]}${o})` : `color(${c} ${x[0]} ${x[1]} ${x[2]}${o})`;
91
+ }
92
+ }, T = (t) => t.length >= 4 && t[3] === 1 ? t.slice(0, 3) : t, R = (t) => parseFloat(t) || 0, G = (t, r = !1) => r ? P(R(t) / 255, 0, 255) : t.includes("%") ? R(t) / 100 : R(t), Mt = (t) => {
93
+ if (typeof t != "string")
94
+ throw new Error("expected a string as input");
95
+ if (t = t.trim(), t.charAt(0) === "#") {
96
+ const r = t.slice(0, 7);
97
+ let e = t.length > 7 ? parseInt(t.slice(7, 9), 16) : 255, n = isNaN(e) ? 1 : e / 255;
98
+ const c = ht(r);
99
+ return n !== 1 && c.push(n), {
100
+ id: "srgb",
101
+ coords: c
102
+ };
103
+ } else {
104
+ const r = /^(rgb|rgba|oklab|oklch|color)\((.+)\)$/i.exec(t);
105
+ if (!r)
106
+ throw new Error(`could not parse color string ${t}`);
107
+ const e = r[1].toLowerCase();
108
+ if (/^rgba?$/i.test(e) && r[2].includes(",")) {
109
+ const n = r[2].split(",").map((c, o) => G(c.trim(), o < 3));
110
+ return {
111
+ id: "srgb",
112
+ coords: T(n)
113
+ };
114
+ } else {
115
+ let n, c, o = !1;
116
+ if (/^color$/i.test(e)) {
117
+ const _ = /([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s/]+)(?:\s?\/\s?([^\s]+))?/.exec(
118
+ r[2]
119
+ );
120
+ if (!_)
121
+ throw new Error(`could not parse color() function ${t}`);
122
+ n = _[1].toLowerCase(), c = _.slice(2, 6);
123
+ } else {
124
+ if (/^(oklab|oklch)$/i.test(e))
125
+ n = e;
126
+ else if (/rgba?/i.test(e))
127
+ n = "srgb", o = !0;
128
+ else
129
+ throw new Error(`unknown color function ${e}`);
130
+ const _ = /([^\s]+)\s+([^\s]+)\s+([^\s/]+)(?:\s?\/\s?([^\s]+))?/.exec(r[2]);
131
+ if (!_)
132
+ throw new Error(`could not parse color() function ${t}`);
133
+ c = _.slice(1, 6);
134
+ }
135
+ c[3] == null && (c = c.slice(0, 3));
136
+ const l = c.map((_, f) => G(_.trim(), o && f < 3));
137
+ if (l.length < 3 || l.length > 4)
138
+ throw new Error("invalid number of coordinates");
139
+ return {
140
+ id: n,
141
+ coords: T(l)
142
+ };
143
+ }
144
+ }
145
+ }, g = (t, r, e, n = b()) => {
146
+ if (z(t, n), !r) throw new Error("must specify a fromSpace");
147
+ if (!e) throw new Error("must specify a toSpace");
148
+ if (r == e)
149
+ return n;
150
+ r.base && (n = r.toBase(n, n), r = r.base);
151
+ let c = r, o = e.base ?? e;
152
+ if (r.base || o.base)
153
+ throw new Error("Currently only base of depth=1 is supported");
154
+ if (c !== o) {
155
+ let l = c.id === "xyz", _ = o.id === "xyz", f = !1, s = !1;
156
+ if (c.id === "oklab") {
157
+ let i = o.fromLMS_M;
158
+ i || (i = A.fromLMS_M, f = !0, l = !0), n = gt(n, i, n);
159
+ } else if (o.id === "oklab") {
160
+ let i = c.toLMS_M;
161
+ i ? n = C(n, i, n) : (f = !0, s = !0);
162
+ } else
163
+ f = !0;
164
+ if (f) {
165
+ if (!l)
166
+ if (c.toXYZ)
167
+ n = c.toXYZ(n, n);
168
+ else if (c.toXYZ_M)
169
+ n = p(n, c.toXYZ_M, n);
170
+ else
171
+ throw new Error(`no toXYZ or toXYZ_M on ${c.id}`);
172
+ if (c.adapt && (n = p(n, c.adapt.to, n)), o.adapt && (n = p(n, o.adapt.from, n)), !_)
173
+ if (s)
174
+ n = C(n, A.toLMS_M, n);
175
+ else if (o.fromXYZ)
176
+ n = o.fromXYZ(n, n);
177
+ else if (o.fromXYZ_M)
178
+ n = p(n, o.fromXYZ_M, n);
179
+ else
180
+ throw new Error(`no fromXYZ or fromXYZ_M on ${o.id}`);
181
+ }
182
+ }
183
+ if (o !== e)
184
+ if (e.fromBase)
185
+ n = e.fromBase(n, n);
186
+ else
187
+ throw new Error(`could not transform ${o.id} to ${e.id}`);
188
+ return n;
189
+ }, A = {
190
+ toLMS_M: q,
191
+ fromLMS_M: J
192
+ }, Y = (t) => {
193
+ let r = t < 0 ? -1 : 1, e = Math.abs(t);
194
+ return e <= 0.04045 ? t / 12.92 : r * Math.pow((e + 0.055) / 1.055, 2.4);
195
+ }, E = (t) => {
196
+ let r = t < 0 ? -1 : 1, e = Math.abs(t);
197
+ return e > 31308e-7 ? r * (1.055 * Math.pow(e, 1 / 2.4) - 0.055) : 12.92 * t;
198
+ }, D = (t, r = b()) => (r[0] = E(t[0]), r[1] = E(t[1]), r[2] = E(t[2]), r), K = (t, r = b()) => (r[0] = Y(t[0]), r[1] = Y(t[1]), r[2] = Y(t[2]), r), xt = {
199
+ id: "srgb-linear",
200
+ toXYZ_M: Q,
201
+ fromXYZ_M: tt,
202
+ toLMS_M: rt,
203
+ fromLMS_M: et
204
+ }, w = {
205
+ id: "srgb",
206
+ base: xt,
207
+ toBase: K,
208
+ fromBase: D
209
+ }, O = (0.4 - 0) / 1e5, $t = {
210
+ id: "oklab"
211
+ }, $ = {
212
+ id: "oklch",
213
+ base: $t,
214
+ toBase: (t, r = b()) => {
215
+ const e = t[1], n = t[2];
216
+ return r[0] = t[0], r[1] = e * Math.cos(n * Math.PI / 180), r[2] = e * Math.sin(n * Math.PI / 180), r;
217
+ },
218
+ fromBase: (t, r = b()) => {
219
+ const e = t[1], n = t[2];
220
+ let c = Math.abs(e) < O && Math.abs(n) < O, o = c ? 0 : _t(Math.atan2(n, e) * 180 / Math.PI), l = c ? 0 : Math.sqrt(e * e + n * n);
221
+ return r[0] = t[0], r[1] = l, r[2] = o, r;
222
+ }
223
+ }, wt = {
224
+ id: "display-p3-linear",
225
+ toXYZ_M: nt,
226
+ fromXYZ_M: st,
227
+ toLMS_M: ot,
228
+ fromLMS_M: ct
229
+ }, pt = {
230
+ id: "display-p3",
231
+ base: wt,
232
+ toBase: K,
233
+ fromBase: D
234
+ }, X = 1.09929682680944, V = 0.018053968510807, H = (t) => t < V * 4.5 ? t / 4.5 : Math.pow((t + X - 1) / X, 1 / 0.45), B = (t) => t >= V ? X * Math.pow(t, 0.45) - (X - 1) : 4.5 * t, Lt = {
235
+ id: "rec2020-linear",
236
+ toXYZ_M: it,
237
+ fromXYZ_M: at,
238
+ toLMS_M: lt,
239
+ fromLMS_M: ft
240
+ }, Nt = {
241
+ id: "rec2020",
242
+ base: Lt,
243
+ toBase: (t, r = b()) => (r[0] = H(t[0]), r[1] = H(t[1]), r[2] = H(t[2]), r),
244
+ fromBase: (t, r = b()) => (r[0] = B(t[0]), r[1] = B(t[1]), r[2] = B(t[2]), r)
245
+ }, S = Symbol("huey-color"), Ft = /^#?(?:[A-F0-9]{8}|[A-F0-9]{6}|[A-F0-9]{3})$/i, Xt = /^rgba?\(\s*(\d+)\s*[,\s]\s*(\d+)\s*[,\s]\s*(\d+)\s*(?:[,/]\s*([\d.]+)\s*)?\)$/i, v = /^hsla?\(\s*([\d.]+)(?:deg|rad|grad|turn)?\s*[,\s]\s*([\d.]+)%?\s*[,\s]\s*([\d.]+)%?\s*(?:[,/]\s*([\d.]+))?\s*\)$/i, I = /^oklch\(\s*([\d.]+%?)\s*[,\s]\s*([\d.]+)\s*[,\s]\s*([\d.]+)(?:deg|rad|grad|turn)?\s*(?:[,/]\s*([\d.]+))?\s*\)$/i;
246
+ function yt(t) {
247
+ return typeof t == "object" && t !== null && S in t && t[S] === !0;
248
+ }
249
+ function Rt(t) {
250
+ return Ft.test(t.trim());
251
+ }
252
+ function Yt(t) {
253
+ const r = Xt.exec(t);
254
+ if (!r)
255
+ return !1;
256
+ const [, e, n, c, o] = r;
257
+ return [Number.parseInt(e), Number.parseInt(n), Number.parseInt(c)].some((_) => _ < 0 || _ > 255) ? !1 : o !== void 0 ? Z(o) : !0;
258
+ }
259
+ function Et(t) {
260
+ const r = v.exec(t);
261
+ if (!r)
262
+ return !1;
263
+ const [, e, n, c, o] = r;
264
+ return !j(e) || !k(n) || !k(c) ? !1 : o !== void 0 ? Z(o) : !0;
265
+ }
266
+ function Ht(t) {
267
+ const r = I.exec(t);
268
+ if (!r)
269
+ return !1;
270
+ const [, e, n, c, o] = r;
271
+ return !k(e) || !Bt(n) || !j(c) ? !1 : o !== void 0 ? Z(o) : !0;
272
+ }
273
+ function k(t) {
274
+ const r = typeof t == "string" ? Number.parseFloat(t) : t;
275
+ return r >= 0 && r <= 1 || r >= 0 && r <= 100;
276
+ }
277
+ function Bt(t) {
278
+ return (typeof t == "string" ? Number.parseFloat(t) : t) >= 0;
279
+ }
280
+ function j(t) {
281
+ const r = typeof t == "string" ? Number.parseFloat(t) : t;
282
+ return !(r < 0 || r > 360);
283
+ }
284
+ function Z(t) {
285
+ const r = typeof t == "string" ? Number.parseFloat(t) : t;
286
+ return r >= 0 && r <= 1;
287
+ }
288
+ function St(t) {
289
+ const r = t.trim();
290
+ return Rt(r) ? "hex" : Yt(r) ? "rgb" : Et(r) ? "hsl" : Ht(r) ? "oklch" : "unknown";
291
+ }
292
+ function kt() {
293
+ const t = window.scrollX || window.pageXOffset || document.documentElement.scrollLeft || 0, r = window.scrollY || window.pageYOffset || document.documentElement.scrollTop || 0;
294
+ return { x: t, y: r };
295
+ }
296
+ function At(t) {
297
+ const { x: r, y: e } = kt(), n = t.getBoundingClientRect();
298
+ return {
299
+ x: n.left + r,
300
+ y: n.top + e
301
+ };
302
+ }
303
+ function Ot(t) {
304
+ const r = { x: 0, y: 0 };
305
+ return t instanceof MouseEvent && (r.x = t.pageX, r.y = t.pageY), typeof TouchEvent < "u" && t instanceof TouchEvent && (r.x = t.touches ? t.touches[0].pageX : t.changedTouches ? t.changedTouches[0].pageX : 0, r.y = t.touches ? t.touches[0].pageY : t.changedTouches ? t.changedTouches[0].pageY : 0), r;
306
+ }
307
+ function Pt(t) {
308
+ return {
309
+ ArrowUp: "up",
310
+ ArrowDown: "down",
311
+ ArrowLeft: "left",
312
+ ArrowRight: "right"
313
+ }[t.code];
314
+ }
315
+ function zt(t, r) {
316
+ let e = 0;
317
+ switch (r) {
318
+ case "down":
319
+ e = -1;
320
+ break;
321
+ case "up":
322
+ e = 1;
323
+ break;
324
+ }
325
+ return t + e;
326
+ }
327
+ function Dt(t, r, e) {
328
+ return Math.min(Math.max(t, r), e);
329
+ }
330
+ function Kt(t) {
331
+ let e = 0;
332
+ return ["r", "g", "b"].includes(t) && (e = 255), t === "h" && (e = 360), ["s", "l", "a"].includes(t) && (e = 100), {
333
+ min: 0,
334
+ max: e
335
+ };
336
+ }
337
+ function Vt(t) {
338
+ return Math.trunc(Number(t.replace(/[%°]/g, "").trim()));
339
+ }
340
+ function vt(t, r, e, n = 0, c = 1) {
341
+ const o = (t - r) / (e - r);
342
+ return (c - n) * o + n;
343
+ }
344
+ function It(t, r) {
345
+ const e = r < 1 ? Math.ceil(-Math.log10(r)) : 0;
346
+ return Math.round(t * 10 ** e) / 10 ** e;
347
+ }
348
+ function Zt(t) {
349
+ const r = v.exec(t.trim());
350
+ if (!r)
351
+ return null;
352
+ const [, e, n, c, o] = r;
353
+ if (!e || !n || !c)
354
+ return null;
355
+ const l = Number.parseFloat(e), _ = Number.parseFloat(n), f = Number.parseFloat(c), s = o ? Number.parseFloat(o) : 1;
356
+ if (Number.isNaN(l) || Number.isNaN(_) || Number.isNaN(f) || _ < 0 || _ > 100 || f < 0 || f > 100 || s < 0 || s > 1)
357
+ return null;
358
+ const { r: i, g: a, b: u } = F(l, _, f);
359
+ return {
360
+ coords: [i / 255, a / 255, u / 255, s],
361
+ space: "rgb"
362
+ };
363
+ }
364
+ function Ct(t) {
365
+ const r = I.exec(t.trim());
366
+ if (!r)
367
+ return null;
368
+ const [, e, n, c, o] = r;
369
+ if (!e || !n || !c)
370
+ return null;
371
+ let l = Number.parseFloat(e.replace("%", ""));
372
+ const _ = Number.parseFloat(n), f = Number.parseFloat(c), s = o ? Number.parseFloat(o) : 1;
373
+ return Number.isNaN(l) || Number.isNaN(_) || Number.isNaN(f) || (e.includes("%") && (l = l / 100), l < 0 || l > 1) || _ < 0 || s < 0 || s > 1 ? null : {
374
+ coords: [l, _, f, s],
375
+ space: "oklch"
376
+ };
377
+ }
378
+ function F(t, r, e, n = !1) {
379
+ const c = t / 360, o = r / 100, l = e / 100;
380
+ let _, f, s;
381
+ if (o === 0)
382
+ _ = f = s = l;
383
+ else {
384
+ const i = (h, m, d) => (d < 0 && (d += 1), d > 1 && (d -= 1), d < 0.16666666666666666 ? h + (m - h) * 6 * d : d < 0.5 ? m : d < 0.6666666666666666 ? h + (m - h) * (0.6666666666666666 - d) * 6 : h), a = l < 0.5 ? l * (1 + o) : l + o - l * o, u = 2 * l - a;
385
+ _ = i(u, a, c + 1 / 3), f = i(u, a, c), s = i(u, a, c - 1 / 3);
386
+ }
387
+ return n ? { r: _, g: f, b: s } : {
388
+ r: _ * 255,
389
+ g: f * 255,
390
+ b: s * 255
391
+ };
392
+ }
393
+ function Tt(t, r, e) {
394
+ const n = Math.max(t, r, e), c = Math.min(t, r, e), o = n - c;
395
+ let l = 0, _ = 0;
396
+ const f = (n + c) / 2;
397
+ if (o !== 0)
398
+ switch (_ = f > 0.5 ? o / (2 - n - c) : o / (n + c), n) {
399
+ case t:
400
+ l = ((r - e) / o + (r < e ? 6 : 0)) / 6;
401
+ break;
402
+ case r:
403
+ l = ((e - t) / o + 2) / 6;
404
+ break;
405
+ case e:
406
+ l = ((t - r) / o + 4) / 6;
407
+ break;
408
+ }
409
+ return {
410
+ h: l * 360,
411
+ // Convert to degrees
412
+ s: _ * 100,
413
+ // Convert to percentage
414
+ l: f * 100
415
+ // Convert to percentage
416
+ };
417
+ }
418
+ function jt(t, r, e) {
419
+ const n = r / 100, c = e / 100, o = c + n * Math.min(c, 1 - c), l = o === 0 ? 0 : 2 * (1 - c / o);
420
+ return {
421
+ h: t,
422
+ // Hue stays the same
423
+ s: l * 100,
424
+ // Convert to percentage
425
+ v: o * 100
426
+ // Convert to percentage
427
+ };
428
+ }
429
+ function Ut(t, r, e) {
430
+ const n = r / 100, c = e / 100, o = c * (1 - n / 2), l = o === 0 || o === 1 ? 0 : (c - o) / Math.min(o, 1 - o);
431
+ return {
432
+ h: t,
433
+ s: l * 100,
434
+ l: o * 100
435
+ };
436
+ }
437
+ function Gt(t) {
438
+ if (yt(t))
439
+ return t;
440
+ const r = St(t);
441
+ if (r === "unknown")
442
+ throw new Error(`invalid color provided: ${t}`);
443
+ let e, n;
444
+ if (r === "hsl") {
445
+ const i = Zt(t);
446
+ if (!i)
447
+ throw new Error(`invalid color provided: ${t}`);
448
+ e = i.coords, n = e[3] ?? 1;
449
+ } else if (r === "oklch") {
450
+ const i = Ct(t);
451
+ if (!i)
452
+ throw new Error(`invalid color provided: ${t}`);
453
+ e = i.coords, n = e[3] ?? 1;
454
+ } else {
455
+ const i = r === "hex" && !t.startsWith("#") ? `#${t}` : t;
456
+ e = Mt(i).coords, n = e[3] ?? 1;
457
+ }
458
+ const c = r === "oklch" ? e.slice(0, 3) : g(e.slice(0, 3), w, $), [o, l, _] = c, f = (i, a, u, h) => {
459
+ const m = Math.max(0, Math.min(1, i)), d = Gt(`oklch(${m * 100}% ${a} ${u} / ${h})`);
460
+ return d.getFormat = () => r, d.getOriginalInput = () => t, d;
461
+ }, s = {
462
+ [S]: !0,
463
+ _l: Number(o.toFixed(4)),
464
+ _c: Number(l.toFixed(4)),
465
+ _h: Number(_.toFixed(4)),
466
+ _a: n,
467
+ getFormat: () => r,
468
+ getOriginalInput: () => t,
469
+ getAlpha: () => s._a,
470
+ setAlpha: (i) => f(s._l, s._c, s._h, i),
471
+ setHue: (i) => {
472
+ const a = s.toHsl(), { r: u, g: h, b: m } = F(i, a.s, a.l, !0), [d, M, N] = g([u, h, m], w, $);
473
+ return f(d, M, N, s._a);
474
+ },
475
+ setSaturation: (i) => {
476
+ const a = s.toHsl(), { r: u, g: h, b: m } = F(a.h, i, a.l, !0), [d, M, N] = g([u, h, m], w, $);
477
+ return f(d, M, N, s._a);
478
+ },
479
+ setLightness: (i) => {
480
+ const a = s.toHsl(), { r: u, g: h, b: m } = F(a.h, a.s, i, !0), [d, M, N] = g([u, h, m], w, $);
481
+ return f(d, M, N, s._a);
482
+ },
483
+ desaturate: (i) => f(s._l, Math.max(0, s._c - i), s._h, s._a),
484
+ saturate: (i) => f(s._l, Math.max(0, s._c + i), s._h, s._a),
485
+ brighten: (i) => f(Math.min(1, s._l + i), s._c, s._h, s._a),
486
+ lighten: (i) => f(Math.min(1, s._l + i), s._c, s._h, s._a),
487
+ darken: (i) => f(Math.max(0, s._l - i), s._c, s._h, s._a),
488
+ randomize: () => f(Math.random(), Math.random() * 0.4, Math.random() * 360, s._a),
489
+ clone: () => f(s._l, s._c, s._h, s._a),
490
+ contrastRatio: (i) => {
491
+ const a = s.getLuminance(), u = i.getLuminance(), h = Math.max(a, u), m = Math.min(a, u);
492
+ return (h + 0.05) / (m + 0.05);
493
+ },
494
+ getLuminance: () => {
495
+ const [i, a, u] = g([s._l, s._c, s._h], $, w), h = (m) => m <= 0.03928 ? m / 12.92 : ((m + 0.055) / 1.055) ** 2.4;
496
+ return 0.2126 * h(i) + 0.7152 * h(a) + 0.0722 * h(u);
497
+ },
498
+ getBrightness: () => s._l,
499
+ isLight: () => s._l > 0.5,
500
+ isDark: () => s._l <= 0.5,
501
+ toHsl: () => {
502
+ const [i, a, u] = g([s._l, s._c, s._h], $, w), { h, s: m, l: d } = Tt(i, a, u);
503
+ return { h: Number(h), s: Number(m.toFixed(2)), l: Number(d.toFixed(2)), a: Number(s._a.toFixed(1)) };
504
+ },
505
+ toHslString: () => {
506
+ const { h: i, s: a, l: u } = s.toHsl(), h = Number(s._a.toFixed(1));
507
+ return h < 1 ? `hsla(${i.toFixed(0)}, ${a.toFixed(0)}%, ${u.toFixed(0)}%, ${h})` : `hsl(${i.toFixed(0)}, ${a.toFixed(0)}%, ${u.toFixed(0)}%)`;
508
+ },
509
+ toHex: () => {
510
+ let i = s.toHexString().replace("#", "");
511
+ return n === 1 && (i = i.slice(0, 6)), i;
512
+ },
513
+ toHexString: () => {
514
+ const i = g([s._l, s._c, s._h], $, w);
515
+ let a = ut([...i, s._a]);
516
+ return n === 1 && (a = a.slice(0, 7)), a;
517
+ },
518
+ toRgb: () => {
519
+ const [i, a, u] = g([s._l, s._c, s._h], $, w);
520
+ return {
521
+ r: L(i),
522
+ g: L(a),
523
+ b: L(u),
524
+ a: Number(s._a.toFixed(1))
525
+ };
526
+ },
527
+ toRgbString: () => {
528
+ const i = g([s._l, s._c, s._h], $, w);
529
+ return bt([...i, Number(s._a.toFixed(1))], w);
530
+ },
531
+ toString: () => r === "hex" ? s.toHexString() : r === "hsl" ? s.toHslString() : r === "oklch" ? s.toOklchString() : r === "rgb" ? s.toRgbString() : "",
532
+ toOklchString: () => {
533
+ const i = (s._l * 100).toFixed(2), a = s._c.toFixed(2), u = s._h.toFixed(2), h = s._a;
534
+ return h < 1 ? `oklch(${i}% ${a} ${u} / ${h})` : `oklch(${i}% ${a} ${u})`;
535
+ },
536
+ toDisplayP3: () => {
537
+ const [i, a, u] = g([s._l, s._c, s._h], $, pt), h = i.toFixed(2), m = a.toFixed(2), d = u.toFixed(2), M = s._a;
538
+ return M < 1 ? `color(display-p3 ${h} ${m} ${d} / ${M})` : `color(display-p3 ${h} ${m} ${d})`;
539
+ },
540
+ toRec2020: () => {
541
+ const [i, a, u] = g([s._l, s._c, s._h], $, Nt), h = i.toFixed(2), m = a.toFixed(2), d = u.toFixed(2), M = s._a;
542
+ return M < 1 ? `color(rec2020 ${h} ${m} ${d} / ${M})` : `color(rec2020 ${h} ${m} ${d})`;
543
+ }
544
+ };
545
+ return s;
546
+ }
547
+ export {
548
+ Ft as HEX_REGEX,
549
+ v as HSL_REGEX,
550
+ I as OKLCH_REGEX,
551
+ Xt as RGB_REGEX,
552
+ zt as bumpColorValue,
553
+ Dt as clamp,
554
+ At as getAbsolutePosition,
555
+ Kt as getChannelBounds,
556
+ St as getFormat,
557
+ Ot as getPageXYFromEvent,
558
+ kt as getScrollXY,
559
+ jt as hslToHsv,
560
+ F as hslToRgb,
561
+ Ut as hsvToHsl,
562
+ Gt as hueyColor,
563
+ Z as isAlpha,
564
+ Bt as isChroma,
565
+ Rt as isHex,
566
+ Et as isHsl,
567
+ j as isHue,
568
+ yt as isHuey,
569
+ Ht as isOklch,
570
+ k as isPercentage,
571
+ Yt as isRgb,
572
+ vt as normalize,
573
+ Zt as parseHSL,
574
+ Ct as parseOKLCH,
575
+ Pt as resolveArrowDirection,
576
+ Tt as rgbToHsl,
577
+ It as roundToStep,
578
+ Vt as truncColorValue
579
+ };
package/dist/vite.svg ADDED
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@hueycolor/core",
3
+ "type": "module",
4
+ "version": "0.0.1-beta.3",
5
+ "private": false,
6
+ "description": "Core utility lib for color manipulation for huey's components",
7
+ "author": "Eihab Khan <yo@eihab.me>",
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/hueycolor/huey",
12
+ "directory": "packages/core"
13
+ },
14
+ "keywords": [
15
+ "color",
16
+ "colour",
17
+ "color-manipulation",
18
+ "hsl",
19
+ "rgb",
20
+ "hex",
21
+ "tinycolor",
22
+ "huey"
23
+ ],
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.js",
28
+ "require": "./dist/index.cjs"
29
+ }
30
+ },
31
+ "main": "./dist/index.cjs",
32
+ "module": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "dependencies": {
38
+ "tinycolor2": "^1.6.0"
39
+ },
40
+ "devDependencies": {
41
+ "typescript": "~5.8.3",
42
+ "vite": "^7.1.2",
43
+ "vite-plugin-dts": "^4.5.4"
44
+ },
45
+ "scripts": {
46
+ "dev": "vite",
47
+ "watch": "vite build --watch",
48
+ "build": "vite build",
49
+ "preview": "vite preview"
50
+ }
51
+ }