UNPKG

@drgd/aura

Version:

Extract color palettes from any image. Works on both server and client.

2 lines 4.58 kB
var A=[{hex:"#E0E0E0",weight:.2},{hex:"#B0B0B0",weight:.2},{hex:"#808080",weight:.2},{hex:"#505050",weight:.15},{hex:"#303030",weight:.15},{hex:"#101010",weight:.1}];var l=class n{constructor(r,g,i,d=1){this.r=r;this.g=g;this.b=i;this.count=d}static average(r){if(r.length===0)return new n(0,0,0);let g=0,i={r:0,g:0,b:0};for(let o of r)i.r+=o.r*o.count,i.g+=o.g*o.count,i.b+=o.b*o.count,g+=o.count;if(g===0){if(r.length>0){let o=r.reduce((t,e)=>t+e.r,0)/r.length,a=r.reduce((t,e)=>t+e.g,0)/r.length,m=r.reduce((t,e)=>t+e.b,0)/r.length;return new n(Math.round(o),Math.round(a),Math.round(m))}return new n(0,0,0)}let d=Math.round(i.r/g),h=Math.round(i.g/g),s=Math.round(i.b/g);return new n(d,h,s,g===0?1:g)}};function C(n,r,g){return"#"+[n,r,g].map(i=>i.toString(16).padStart(2,"0")).join("")}var M=(n,r)=>Math.pow(n.r-r.r,2)+Math.pow(n.g-r.g,2)+Math.pow(n.b-r.b,2);function $(n,r,g=20){if(n.length===0)return[];if(n.length<=r)return n;let i=[n[Math.floor(Math.random()*n.length)]];for(;i.length<r;){let s=n.map(t=>Math.min(...i.map(e=>Math.pow(t.r-e.r,2)+Math.pow(t.g-e.g,2)+Math.pow(t.b-e.b,2)))),o=s.reduce((t,e)=>t+e,0),a=Math.random()*o,m=0;for(let t=0;t<s.length;t++)if(m+=s[t]??0,m>=a){i.push(n[t]);break}}let d=[...i],h=new Map;for(let s=0;s<g;s++){let o=Array.from({length:r},()=>[]),a=new Map;for(let e of n){let c=`${e.r},${e.g},${e.b}`,u=h.get(c);if(u===void 0){let p=1/0;u=0,d.forEach((x,E)=>{let b=M(e,x);b<p&&(p=b,u=E)})}a.set(c,u),o[u]?.push(e)}h=a;let m=o.map(e=>{if(e.length===0)return d[0];let c=l.average(e);return c.count=e.reduce((u,p)=>u+p.count,0),c});if(d.every((e,c)=>{let u=m[c];return e.r===u?.r&&e.g===u?.g&&e.b===u?.b}))return m.filter(e=>e!==void 0);d=m.filter(e=>e!==void 0)}return d}function w(n,r){if(r===0||n.length<=1)return n.length>0?[l.average(n)]:[];let g=n.reduce((t,e)=>({min:Math.min(t.min,e.r),max:Math.max(t.max,e.r)}),{min:255,max:0}),i=n.reduce((t,e)=>({min:Math.min(t.min,e.g),max:Math.max(t.max,e.g)}),{min:255,max:0}),d=n.reduce((t,e)=>({min:Math.min(t.min,e.b),max:Math.max(t.max,e.b)}),{min:255,max:0}),h=g.max-g.min,s=i.max-i.min,o=d.max-d.min,a;h>s&&h>o?a="r":s>o?a="g":a="b",n.sort((t,e)=>t[a]-e[a]);let m=Math.floor(n.length/2);return[...w(n.slice(0,m),r-1),...w(n.slice(m),r-1)]}var f=["image/jpeg","image/png","image/webp","image/avif"];async function R(n){let r;try{r=new URL(n)}catch{throw new Error("[@drgd/aura] - Invalid image URL format")}let g=r.hostname==="localhost"||r.hostname==="127.0.0.1",i=r.protocol==="http:",d=r.protocol==="https:",h=n.startsWith("data:");if(!(i&&g||d||h))throw new Error("[@drgd/aura] - Only HTTPS (or HTTP for localhost) and data URLs are supported");if(h){let a=n.split(",")[0]?.split(":")[1]?.split(";")[0];if(!a||!f.includes(a))throw new Error(`[@drgd/aura] - Invalid image type. Supported types: ${f.join(", ")}`);return!0}let s=new AbortController,o;try{try{let t=await fetch(n,{method:"HEAD",headers:{Accept:"image/*"},mode:"cors",credentials:"omit",signal:s.signal});if(t.ok){let e=t.headers.get("content-type");if(!e||!f.includes(e.toLowerCase()))throw new Error(`[@drgd/aura] - Invalid image type (from HEAD). Supported: ${f.join(", ")}`);let c=t.headers.get("content-length");if(c&&parseInt(c)>10485760)throw new Error(`[@drgd/aura] - Image size (from HEAD) exceeds ${10485760/(1024*1024)}MB`);return!0}else console.log(`[@drgd/aura] - HEAD request failed for ${n}: ${t.status} ${t.statusText}. Attempting GET.`)}catch(t){if(t.message.startsWith("[@drgd/aura] - Invalid image type (from HEAD)")||t.message.startsWith("[@drgd/aura] - Image size (from HEAD) exceeds")||s.signal.aborted)throw t}finally{s.signal.aborted||s.abort()}o=new AbortController;let a=await fetch(n,{method:"GET",headers:{Accept:"image/*"},mode:"cors",credentials:"omit",signal:o.signal});if(!a.ok)throw new Error(`[@drgd/aura] - Image is not accessible (GET ${a.status} ${a.statusText})`);let m=a.headers.get("content-type");if(!m||!f.includes(m.toLowerCase()))throw new Error(`[@drgd/aura] - Unsupported image type (from GET). Allowed: ${f.join(", ")}`);return a.body&&typeof a.body.cancel=="function"&&!a.body.locked&&a.body.cancel().catch(t=>{console.warn(`[@drgd/aura] - Error cancelling response body stream for ${n}: ${t.message}`)}),!0}catch(a){throw o&&!o.signal.aborted&&o.abort(),s.signal.aborted||s.abort(),a.message.startsWith("[@drgd/aura] -")?a:a.name==="AbortError"?new Error(`[@drgd/aura] - Image validation aborted: ${a.message}`):new Error(`[@drgd/aura] - Image validation failed: ${a.message}`)}}export{l as a,C as b,$ as c,w as d,R as e,A as f}; //# sourceMappingURL=chunk-VRJCZRLL.js.map