@atomicservice/as-icon-generator 1.0.12
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/README.md +24 -0
- package/dist/index.d.mts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# atomicservice icon generator 睫毛图工具
|
|
2
|
+
|
|
3
|
+
## 安装
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
npm install @atomicservice/as-icon-generator
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## 使用指南
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
import { AsIconGenerator } from '@atomicservice/as-icon-generator';
|
|
13
|
+
|
|
14
|
+
const asIconGenerator = new AsIconGenerator({ srcImage })
|
|
15
|
+
|
|
16
|
+
// 获取颜色
|
|
17
|
+
const colors = await asIconGenerator.getColors();
|
|
18
|
+
asIconGenerator.setColor(colors[0]);
|
|
19
|
+
|
|
20
|
+
// 生成图片, agc: true 表示同时生成agc中使用的图标
|
|
21
|
+
const result = await asIconGenerator.generate({ agc: true })
|
|
22
|
+
// result.outputImage
|
|
23
|
+
// result.agcImage
|
|
24
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
declare class IconMaker {
|
|
2
|
+
private srcImg;
|
|
3
|
+
private eventHandlers;
|
|
4
|
+
on(event: string, cb: any): void;
|
|
5
|
+
emit(event: string, ...args: any[]): void;
|
|
6
|
+
setSrc(imgUrl: string): Promise<HTMLImageElement>;
|
|
7
|
+
hasSrc(): boolean;
|
|
8
|
+
make(selectedColor: string): Promise<{
|
|
9
|
+
preview512: HTMLCanvasElement;
|
|
10
|
+
preview216: HTMLCanvasElement;
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare class AsIconGenerator {
|
|
15
|
+
private srcImg;
|
|
16
|
+
private selectedColor;
|
|
17
|
+
maker: IconMaker;
|
|
18
|
+
constructor(options: any);
|
|
19
|
+
getColors(): Promise<never[]>;
|
|
20
|
+
setColor(color: any): void;
|
|
21
|
+
generate(imgOptions: any): Promise<{
|
|
22
|
+
outputImage: string;
|
|
23
|
+
agcImage: string;
|
|
24
|
+
}>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { AsIconGenerator };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
declare class IconMaker {
|
|
2
|
+
private srcImg;
|
|
3
|
+
private eventHandlers;
|
|
4
|
+
on(event: string, cb: any): void;
|
|
5
|
+
emit(event: string, ...args: any[]): void;
|
|
6
|
+
setSrc(imgUrl: string): Promise<HTMLImageElement>;
|
|
7
|
+
hasSrc(): boolean;
|
|
8
|
+
make(selectedColor: string): Promise<{
|
|
9
|
+
preview512: HTMLCanvasElement;
|
|
10
|
+
preview216: HTMLCanvasElement;
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare class AsIconGenerator {
|
|
15
|
+
private srcImg;
|
|
16
|
+
private selectedColor;
|
|
17
|
+
maker: IconMaker;
|
|
18
|
+
constructor(options: any);
|
|
19
|
+
getColors(): Promise<never[]>;
|
|
20
|
+
setColor(color: any): void;
|
|
21
|
+
generate(imgOptions: any): Promise<{
|
|
22
|
+
outputImage: string;
|
|
23
|
+
agcImage: string;
|
|
24
|
+
}>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { AsIconGenerator };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var W=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var q=Object.prototype.hasOwnProperty;var U=(r,n)=>{for(var o in n)W(r,o,{get:n[o],enumerable:!0})},X=(r,n,o,a)=>{if(n&&typeof n=="object"||typeof n=="function")for(let l of V(n))!q.call(r,l)&&l!==o&&W(r,l,{get:()=>n[l],enumerable:!(a=B(n,l))||a.enumerable});return r};var F=r=>X(W({},"__esModule",{value:!0}),r);var ut={};U(ut,{AsIconGenerator:()=>R});module.exports=F(ut);var j=({red:r,green:n,blue:o})=>{let a=r/255,l=n/255,e=o/255,t=Math.max(a,l,e),c=Math.min(a,l,e),s=0,E=0,u=(t+c)/2;if(t===c)s=0,E=0;else{let _=t-c;E=u>.5?_/(2-t-c):_/(t+c),Math.abs(t-a)<=Number.EPSILON?s=(l-e)/_+(l<e?6:0):Math.abs(t-l)<=Number.EPSILON?s=(e-a)/_+2:Math.abs(t-e)<=Number.EPSILON&&(s=(a-l)/_+4),s/=6}return[s*360,E*100,u*100]};function Q({x:r,y:n,r:o,ctx:a}){a.beginPath(),a.arc(r,n,o,0,Math.PI*2,!1),a.closePath()}var J=(r,n)=>new Promise(o=>{let a=new Image;a.onload=()=>{o(a);let l=Math.max(a.width,a.height),e=n/l;a.width=a.width*e,a.height=a.height*e},a.src=r}),T=r=>{let n=r?.getContext("2d");return n?(n.imageSmoothingEnabled=!0,n.imageSmoothingQuality="high",n):null},K=async(r,n)=>{let o=T(n);o&&(o.clearRect(0,0,512,512),n.width=512,nt(n),o.drawImage(r,n.width/2-r.width/2,n.height/2-r.height/2,358,358))},Z=({centerX:r,centerY:n,r:o,thetaDegrees:a})=>{let l=a*(Math.PI/180),e=r+o*Math.cos(l),t=n+o*Math.sin(l);return{x:e,y:t}},$=({ctx:r,degrees:n,color:o})=>{let{x:a,y:l}=Z({centerX:256,centerY:256,r:230,thetaDegrees:n});r.beginPath(),r.arc(a,l,38/2,0,2*Math.PI,!1),r.fillStyle=o,r.fill(),r.closePath()},tt=async r=>{let n=({x:h,y:I})=>{let m=h-179,g=I-358/2,C=Math.sqrt(m*m+g*g);return Math.abs(E-C)<=2},o=document.createElement("canvas");o.width=358,o.height=358;let a=T(o);if(!a)return 2;a.drawImage(r,0,0,358,358);let l=a.getImageData(0,0,358,358),e=l.data,t=0,c=0,s=0,E=358/2,u=e.length;for(let h=0;h<u;h+=4){let I=h/4%358,m=Math.floor(h/4/358);if(n({x:I,y:m})){let[,g,C]=j({red:e[h],green:e[h+1],blue:e[h+2]}),p=g>=0&&g<=2&&C>=96&&C<=100,f=g>=0&&g<=100&&C>=0&&C<=8;s+=1,t+=p?1:0,c+=f?1:0}}let _=s*.01;return a.putImageData(l,0,0),c>=_&&t>=_||s>=2*(t+c)?2:s-c<=_?1:s-t<=_?0:2},P=({ctx:r,color:n})=>{r.beginPath();let o=358/2-6/2+1;r.lineWidth=6,r.strokeStyle=n,r.arc(256,256,o,0,2*Math.PI,!1),r.stroke(),r.closePath()},et=({canvas:r,color:n,borderType:o})=>{let a=T(r);if(!a||(a.clearRect(0,0,512,512),r.width=512,!n))return;a.beginPath();let l=38+358/2+26/2;a.lineCap="round",a.lineWidth=26,a.strokeStyle=n,a.arc(256,256,l,0,Math.PI,!1),a.stroke(),a.closePath();for(let e=1;e<=9;e++)$({ctx:a,degrees:180+18*e,color:n});if(n.toUpperCase()==="#E5E5E5"||n.toUpperCase()==="#2E2E2E"){P({ctx:a,color:n});return}o!==2&&P({ctx:a,color:o===0?"#E5E5E5":"#2E2E2E"})},nt=async r=>{let n=T(r);n&&(n.clearRect(0,0,512,512),Q({x:256,y:256,r:358/2,ctx:n}),n.clip())};function rt(r,n,o){let a=T(r);if(a===null)return"";let l=[n,o];for(let e=0;e<l.length;e++)a.drawImage(l[e],0,0);return r}var D=class{constructor(){this.srcImg=null;this.eventHandlers={}}on(n,o){this.eventHandlers[n]||(this.eventHandlers[n]=[]),this.eventHandlers[n].push(o)}emit(n,...o){this.eventHandlers[n]?.forEach(a=>a?.(...o))}async setSrc(n){return J(n,358).then(o=>(this.srcImg=o,this.emit("imgLoaded",o),o))}hasSrc(){return!!this.srcImg}async make(n){if(!this.srcImg)throw new Error("src img is not ready");let o=this.srcImg,a=await tt(o),l=512,e=216,t=O(l,l),c=O(l,l);K(o,t),et({canvas:c,color:n,borderType:a});let s=O(l,l);rt(s,t,c);let E=O(e,e);return T(E).drawImage(s,0,0,l,l,0,0,e,e),{preview512:s,preview216:E}}};function O(r,n){let o=document.createElement("canvas");return o.width=r,o.height=n,o}var y=function(r,n){return r<n?-1:r>n?1:0},k=function(r){return r.reduce(function(n,o){return n+o},0)},ot=(function(){function r(o){this.colors=o}var n=r.prototype;return n.palette=function(){return this.colors},n.map=function(o){return o},r})(),at=(function(){function r(e,t,c){return(e<<10)+(t<<5)+c}function n(e){var t=[],c=!1;function s(){t.sort(e),c=!0}return{push:function(E){t.push(E),c=!1},peek:function(E){return c||s(),E===void 0&&(E=t.length-1),t[E]},pop:function(){return c||s(),t.pop()},size:function(){return t.length},map:function(E){return t.map(E)},debug:function(){return c||s(),t}}}function o(e,t,c,s,E,u,_){var h=this;h.r1=e,h.r2=t,h.g1=c,h.g2=s,h.b1=E,h.b2=u,h.histo=_}function a(){this.vboxes=new n(function(e,t){return y(e.vbox.count()*e.vbox.volume(),t.vbox.count()*t.vbox.volume())})}function l(e,t){if(t.count()){var c=t.r2-t.r1+1,s=t.g2-t.g1+1,E=Math.max.apply(null,[c,s,t.b2-t.b1+1]);if(t.count()==1)return[t.copy()];var u,_,h,I,m=0,g=[],C=[];if(E==c)for(u=t.r1;u<=t.r2;u++){for(I=0,_=t.g1;_<=t.g2;_++)for(h=t.b1;h<=t.b2;h++)I+=e[r(u,_,h)]||0;g[u]=m+=I}else if(E==s)for(u=t.g1;u<=t.g2;u++){for(I=0,_=t.r1;_<=t.r2;_++)for(h=t.b1;h<=t.b2;h++)I+=e[r(_,u,h)]||0;g[u]=m+=I}else for(u=t.b1;u<=t.b2;u++){for(I=0,_=t.r1;_<=t.r2;_++)for(h=t.g1;h<=t.g2;h++)I+=e[r(_,h,u)]||0;g[u]=m+=I}return g.forEach(function(p,f){C[f]=m-p}),(function(p){var f,v,H,A,d,b=p+"1",N=p+"2",S=0;for(u=t[b];u<=t[N];u++)if(g[u]>m/2){for(H=t.copy(),A=t.copy(),d=(f=u-t[b])<=(v=t[N]-u)?Math.min(t[N]-1,~~(u+v/2)):Math.max(t[b],~~(u-1-f/2));!g[d];)d++;for(S=C[d];!S&&g[d-1];)S=C[--d];return H[N]=d,A[b]=H[N]+1,[H,A]}})(E==c?"r":E==s?"g":"b")}}return o.prototype={volume:function(e){var t=this;return t._volume&&!e||(t._volume=(t.r2-t.r1+1)*(t.g2-t.g1+1)*(t.b2-t.b1+1)),t._volume},count:function(e){var t=this,c=t.histo;if(!t._count_set||e){var s,E,u,_=0;for(s=t.r1;s<=t.r2;s++)for(E=t.g1;E<=t.g2;E++)for(u=t.b1;u<=t.b2;u++)_+=c[r(s,E,u)]||0;t._count=_,t._count_set=!0}return t._count},copy:function(){var e=this;return new o(e.r1,e.r2,e.g1,e.g2,e.b1,e.b2,e.histo)},avg:function(e){var t=this,c=t.histo;if(!t._avg||e){var s,E,u,_,h=0,I=0,m=0,g=0;if(t.r1===t.r2&&t.g1===t.g2&&t.b1===t.b2)t._avg=[t.r1<<3,t.g1<<3,t.b1<<3];else{for(E=t.r1;E<=t.r2;E++)for(u=t.g1;u<=t.g2;u++)for(_=t.b1;_<=t.b2;_++)h+=s=c[r(E,u,_)]||0,I+=s*(E+.5)*8,m+=s*(u+.5)*8,g+=s*(_+.5)*8;t._avg=h?[~~(I/h),~~(m/h),~~(g/h)]:[~~(8*(t.r1+t.r2+1)/2),~~(8*(t.g1+t.g2+1)/2),~~(8*(t.b1+t.b2+1)/2)]}}return t._avg},contains:function(e){var t=this,c=e[0]>>3;return gval=e[1]>>3,bval=e[2]>>3,c>=t.r1&&c<=t.r2&&gval>=t.g1&&gval<=t.g2&&bval>=t.b1&&bval<=t.b2}},a.prototype={push:function(e){this.vboxes.push({vbox:e,color:e.avg()})},palette:function(){return this.vboxes.map(function(e){return e.color})},size:function(){return this.vboxes.size()},map:function(e){for(var t=this.vboxes,c=0;c<t.size();c++)if(t.peek(c).vbox.contains(e))return t.peek(c).color;return this.nearest(e)},nearest:function(e){for(var t,c,s,E=this.vboxes,u=0;u<E.size();u++)((c=Math.sqrt(Math.pow(e[0]-E.peek(u).color[0],2)+Math.pow(e[1]-E.peek(u).color[1],2)+Math.pow(e[2]-E.peek(u).color[2],2)))<t||t===void 0)&&(t=c,s=E.peek(u).color);return s},forcebw:function(){var e=this.vboxes;e.sort(function(E,u){return y(k(E.color),k(u.color))});var t=e[0].color;t[0]<5&&t[1]<5&&t[2]<5&&(e[0].color=[0,0,0]);var c=e.length-1,s=e[c].color;s[0]>251&&s[1]>251&&s[2]>251&&(e[c].color=[255,255,255])}},{quantize:function(e,t){if(!Number.isInteger(t)||t<1||t>256)throw new Error("Invalid maximum color count. It must be an integer between 1 and 256.");if(!e.length||t<2||t>256||!e.length||t<2||t>256)return!1;for(var c=[],s=new Set,E=0;E<e.length;E++){var u=e[E],_=u.join(",");s.has(_)||(s.add(_),c.push(u))}if(c.length<=t)return new ot(c);var h=(function(f){var v,H=new Array(32768);return f.forEach(function(A){v=r(A[0]>>3,A[1]>>3,A[2]>>3),H[v]=(H[v]||0)+1}),H})(e);h.forEach(function(){});var I=(function(f,v){var H,A,d,b=1e6,N=0,S=1e6,Y=0,w=1e6,M=0;return f.forEach(function(x){(H=x[0]>>3)<b?b=H:H>N&&(N=H),(A=x[1]>>3)<S?S=A:A>Y&&(Y=A),(d=x[2]>>3)<w?w=d:d>M&&(M=d)}),new o(b,N,S,Y,w,M,v)})(e,h),m=new n(function(f,v){return y(f.count(),v.count())});function g(f,v){for(var H,A=f.size(),d=0;d<1e3;){if(A>=v||d++>1e3)return;if((H=f.pop()).count()){var b=l(h,H),N=b[0],S=b[1];if(!N)return;f.push(N),S&&(f.push(S),A++)}else f.push(H),d++}}m.push(I),g(m,.75*t);for(var C=new n(function(f,v){return y(f.count()*f.volume(),v.count()*v.volume())});m.size();)C.push(m.pop());g(C,t);for(var p=new a;C.size();)p.push(C.pop());return p}}})().quantize,G=function(r){this.canvas=document.createElement("canvas"),this.context=this.canvas.getContext("2d"),this.width=this.canvas.width=r.naturalWidth,this.height=this.canvas.height=r.naturalHeight,this.context.drawImage(r,0,0,this.width,this.height)};G.prototype.getImageData=function(){return this.context.getImageData(0,0,this.width,this.height)};var L=function(){};L.prototype.getColor=function(r,n){return n===void 0&&(n=10),this.getPalette(r,5,n)[0]},L.prototype.getPalette=function(r,n,o){var a=(function(c){var s=c.colorCount,E=c.quality;if(s!==void 0&&Number.isInteger(s)){if(s===1)throw new Error("colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()");s=Math.max(s,2),s=Math.min(s,20)}else s=10;return(E===void 0||!Number.isInteger(E)||E<1)&&(E=10),{colorCount:s,quality:E}})({colorCount:n,quality:o}),l=new G(r),e=(function(c,s,E){for(var u,_,h,I,m,g=c,C=[],p=0;p<s;p+=E)_=g[0+(u=4*p)],h=g[u+1],I=g[u+2],((m=g[u+3])===void 0||m>=125)&&(_>250&&h>250&&I>250||C.push([_,h,I]));return C})(l.getImageData().data,l.width*l.height,a.quality),t=at(e,a.colorCount);return t?t.palette():null},L.prototype.getColorFromUrl=function(r,n,o){var a=this,l=document.createElement("img");l.addEventListener("load",function(){var e=a.getPalette(l,5,o);n(e[0],r)}),l.src=r},L.prototype.getImageData=function(r,n){var o=new XMLHttpRequest;o.open("GET",r,!0),o.responseType="arraybuffer",o.onload=function(){if(this.status==200){var a=new Uint8Array(this.response);i=a.length;for(var l=new Array(i),e=0;e<a.length;e++)l[e]=String.fromCharCode(a[e]);var t=l.join(""),c=window.btoa(t);n("data:image/png;base64,"+c)}},o.send()},L.prototype.getColorAsync=function(r,n,o){var a=this;this.getImageData(r,function(l){var e=document.createElement("img");e.addEventListener("load",function(){var t=a.getPalette(e,5,o);n(t[0],this)}),e.src=l})};function it(r,n,o){r/=255,n/=255,o/=255;let a=Math.max(r,n,o),l=Math.min(r,n,o),e=a-l,t=0;e!==0&&(a===r?t=(n-o)/e%6:a===n?t=(o-r)/e+2:t=(r-n)/e+4,t*=60,t<0&&(t+=360));let c=a===0?0:e/a;return{h:t,s:c,v:a}}function st(r){return"#"+r.map(n=>n.toString(16).padStart(2,"0")).join("")}function ct(r){let{s:n,v:o}=it(r[0],r[1],r[2]);return n<=.02&&o>=.96?[229,229,229]:o<=.08?[46,46,46]:st(r)}async function z(r){return(await new L().getPalette(r,5)).map(ct).filter((l,e,t)=>t.indexOf(l)===e).slice(0,3)}var R=class{constructor(n){this.srcImg=null;this.selectedColor="";this.maker=new D;this.srcImg=n.srcImg}async getColors(){let n=[];if(!this.srcImg)throw new Error("srcImg is null");return this.maker.on("imgLoaded",o=>{z(o).then(a=>{n=a,this.selectedColor=a[0]})}),await this.maker.setSrc(this.srcImg),n}setColor(n){this.selectedColor=n}async generate(n){let o={outputImage:"",agcImage:""};return this.maker.hasSrc()&&this.selectedColor&&await this.maker.make(this.selectedColor).then(({preview512:a,preview216:l})=>{n?.agc&&(o.agcImage=l.toDataURL("image/png")),o.outputImage=a.toDataURL("image/png")}),o}};0&&(module.exports={AsIconGenerator});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var z=({red:r,green:n,blue:o})=>{let a=r/255,E=n/255,e=o/255,t=Math.max(a,E,e),c=Math.min(a,E,e),s=0,l=0,u=(t+c)/2;if(t===c)s=0,l=0;else{let _=t-c;l=u>.5?_/(2-t-c):_/(t+c),Math.abs(t-a)<=Number.EPSILON?s=(E-e)/_+(E<e?6:0):Math.abs(t-E)<=Number.EPSILON?s=(e-a)/_+2:Math.abs(t-e)<=Number.EPSILON&&(s=(a-E)/_+4),s/=6}return[s*360,l*100,u*100]};function B({x:r,y:n,r:o,ctx:a}){a.beginPath(),a.arc(r,n,o,0,Math.PI*2,!1),a.closePath()}var V=(r,n)=>new Promise(o=>{let a=new Image;a.onload=()=>{o(a);let E=Math.max(a.width,a.height),e=n/E;a.width=a.width*e,a.height=a.height*e},a.src=r}),T=r=>{let n=r?.getContext("2d");return n?(n.imageSmoothingEnabled=!0,n.imageSmoothingQuality="high",n):null},q=async(r,n)=>{let o=T(n);o&&(o.clearRect(0,0,512,512),n.width=512,Q(n),o.drawImage(r,n.width/2-r.width/2,n.height/2-r.height/2,358,358))},U=({centerX:r,centerY:n,r:o,thetaDegrees:a})=>{let E=a*(Math.PI/180),e=r+o*Math.cos(E),t=n+o*Math.sin(E);return{x:e,y:t}},X=({ctx:r,degrees:n,color:o})=>{let{x:a,y:E}=U({centerX:256,centerY:256,r:230,thetaDegrees:n});r.beginPath(),r.arc(a,E,38/2,0,2*Math.PI,!1),r.fillStyle=o,r.fill(),r.closePath()},F=async r=>{let n=({x:h,y:I})=>{let m=h-179,g=I-358/2,C=Math.sqrt(m*m+g*g);return Math.abs(l-C)<=2},o=document.createElement("canvas");o.width=358,o.height=358;let a=T(o);if(!a)return 2;a.drawImage(r,0,0,358,358);let E=a.getImageData(0,0,358,358),e=E.data,t=0,c=0,s=0,l=358/2,u=e.length;for(let h=0;h<u;h+=4){let I=h/4%358,m=Math.floor(h/4/358);if(n({x:I,y:m})){let[,g,C]=z({red:e[h],green:e[h+1],blue:e[h+2]}),p=g>=0&&g<=2&&C>=96&&C<=100,f=g>=0&&g<=100&&C>=0&&C<=8;s+=1,t+=p?1:0,c+=f?1:0}}let _=s*.01;return a.putImageData(E,0,0),c>=_&&t>=_||s>=2*(t+c)?2:s-c<=_?1:s-t<=_?0:2},W=({ctx:r,color:n})=>{r.beginPath();let o=358/2-6/2+1;r.lineWidth=6,r.strokeStyle=n,r.arc(256,256,o,0,2*Math.PI,!1),r.stroke(),r.closePath()},j=({canvas:r,color:n,borderType:o})=>{let a=T(r);if(!a||(a.clearRect(0,0,512,512),r.width=512,!n))return;a.beginPath();let E=38+358/2+26/2;a.lineCap="round",a.lineWidth=26,a.strokeStyle=n,a.arc(256,256,E,0,Math.PI,!1),a.stroke(),a.closePath();for(let e=1;e<=9;e++)X({ctx:a,degrees:180+18*e,color:n});if(n.toUpperCase()==="#E5E5E5"||n.toUpperCase()==="#2E2E2E"){W({ctx:a,color:n});return}o!==2&&W({ctx:a,color:o===0?"#E5E5E5":"#2E2E2E"})},Q=async r=>{let n=T(r);n&&(n.clearRect(0,0,512,512),B({x:256,y:256,r:358/2,ctx:n}),n.clip())};function J(r,n,o){let a=T(r);if(a===null)return"";let E=[n,o];for(let e=0;e<E.length;e++)a.drawImage(E[e],0,0);return r}var D=class{constructor(){this.srcImg=null;this.eventHandlers={}}on(n,o){this.eventHandlers[n]||(this.eventHandlers[n]=[]),this.eventHandlers[n].push(o)}emit(n,...o){this.eventHandlers[n]?.forEach(a=>a?.(...o))}async setSrc(n){return V(n,358).then(o=>(this.srcImg=o,this.emit("imgLoaded",o),o))}hasSrc(){return!!this.srcImg}async make(n){if(!this.srcImg)throw new Error("src img is not ready");let o=this.srcImg,a=await F(o),E=512,e=216,t=O(E,E),c=O(E,E);q(o,t),j({canvas:c,color:n,borderType:a});let s=O(E,E);J(s,t,c);let l=O(e,e);return T(l).drawImage(s,0,0,E,E,0,0,e,e),{preview512:s,preview216:l}}};function O(r,n){let o=document.createElement("canvas");return o.width=r,o.height=n,o}var y=function(r,n){return r<n?-1:r>n?1:0},R=function(r){return r.reduce(function(n,o){return n+o},0)},K=(function(){function r(o){this.colors=o}var n=r.prototype;return n.palette=function(){return this.colors},n.map=function(o){return o},r})(),Z=(function(){function r(e,t,c){return(e<<10)+(t<<5)+c}function n(e){var t=[],c=!1;function s(){t.sort(e),c=!0}return{push:function(l){t.push(l),c=!1},peek:function(l){return c||s(),l===void 0&&(l=t.length-1),t[l]},pop:function(){return c||s(),t.pop()},size:function(){return t.length},map:function(l){return t.map(l)},debug:function(){return c||s(),t}}}function o(e,t,c,s,l,u,_){var h=this;h.r1=e,h.r2=t,h.g1=c,h.g2=s,h.b1=l,h.b2=u,h.histo=_}function a(){this.vboxes=new n(function(e,t){return y(e.vbox.count()*e.vbox.volume(),t.vbox.count()*t.vbox.volume())})}function E(e,t){if(t.count()){var c=t.r2-t.r1+1,s=t.g2-t.g1+1,l=Math.max.apply(null,[c,s,t.b2-t.b1+1]);if(t.count()==1)return[t.copy()];var u,_,h,I,m=0,g=[],C=[];if(l==c)for(u=t.r1;u<=t.r2;u++){for(I=0,_=t.g1;_<=t.g2;_++)for(h=t.b1;h<=t.b2;h++)I+=e[r(u,_,h)]||0;g[u]=m+=I}else if(l==s)for(u=t.g1;u<=t.g2;u++){for(I=0,_=t.r1;_<=t.r2;_++)for(h=t.b1;h<=t.b2;h++)I+=e[r(_,u,h)]||0;g[u]=m+=I}else for(u=t.b1;u<=t.b2;u++){for(I=0,_=t.r1;_<=t.r2;_++)for(h=t.g1;h<=t.g2;h++)I+=e[r(_,h,u)]||0;g[u]=m+=I}return g.forEach(function(p,f){C[f]=m-p}),(function(p){var f,v,H,A,d,b=p+"1",N=p+"2",S=0;for(u=t[b];u<=t[N];u++)if(g[u]>m/2){for(H=t.copy(),A=t.copy(),d=(f=u-t[b])<=(v=t[N]-u)?Math.min(t[N]-1,~~(u+v/2)):Math.max(t[b],~~(u-1-f/2));!g[d];)d++;for(S=C[d];!S&&g[d-1];)S=C[--d];return H[N]=d,A[b]=H[N]+1,[H,A]}})(l==c?"r":l==s?"g":"b")}}return o.prototype={volume:function(e){var t=this;return t._volume&&!e||(t._volume=(t.r2-t.r1+1)*(t.g2-t.g1+1)*(t.b2-t.b1+1)),t._volume},count:function(e){var t=this,c=t.histo;if(!t._count_set||e){var s,l,u,_=0;for(s=t.r1;s<=t.r2;s++)for(l=t.g1;l<=t.g2;l++)for(u=t.b1;u<=t.b2;u++)_+=c[r(s,l,u)]||0;t._count=_,t._count_set=!0}return t._count},copy:function(){var e=this;return new o(e.r1,e.r2,e.g1,e.g2,e.b1,e.b2,e.histo)},avg:function(e){var t=this,c=t.histo;if(!t._avg||e){var s,l,u,_,h=0,I=0,m=0,g=0;if(t.r1===t.r2&&t.g1===t.g2&&t.b1===t.b2)t._avg=[t.r1<<3,t.g1<<3,t.b1<<3];else{for(l=t.r1;l<=t.r2;l++)for(u=t.g1;u<=t.g2;u++)for(_=t.b1;_<=t.b2;_++)h+=s=c[r(l,u,_)]||0,I+=s*(l+.5)*8,m+=s*(u+.5)*8,g+=s*(_+.5)*8;t._avg=h?[~~(I/h),~~(m/h),~~(g/h)]:[~~(8*(t.r1+t.r2+1)/2),~~(8*(t.g1+t.g2+1)/2),~~(8*(t.b1+t.b2+1)/2)]}}return t._avg},contains:function(e){var t=this,c=e[0]>>3;return gval=e[1]>>3,bval=e[2]>>3,c>=t.r1&&c<=t.r2&&gval>=t.g1&&gval<=t.g2&&bval>=t.b1&&bval<=t.b2}},a.prototype={push:function(e){this.vboxes.push({vbox:e,color:e.avg()})},palette:function(){return this.vboxes.map(function(e){return e.color})},size:function(){return this.vboxes.size()},map:function(e){for(var t=this.vboxes,c=0;c<t.size();c++)if(t.peek(c).vbox.contains(e))return t.peek(c).color;return this.nearest(e)},nearest:function(e){for(var t,c,s,l=this.vboxes,u=0;u<l.size();u++)((c=Math.sqrt(Math.pow(e[0]-l.peek(u).color[0],2)+Math.pow(e[1]-l.peek(u).color[1],2)+Math.pow(e[2]-l.peek(u).color[2],2)))<t||t===void 0)&&(t=c,s=l.peek(u).color);return s},forcebw:function(){var e=this.vboxes;e.sort(function(l,u){return y(R(l.color),R(u.color))});var t=e[0].color;t[0]<5&&t[1]<5&&t[2]<5&&(e[0].color=[0,0,0]);var c=e.length-1,s=e[c].color;s[0]>251&&s[1]>251&&s[2]>251&&(e[c].color=[255,255,255])}},{quantize:function(e,t){if(!Number.isInteger(t)||t<1||t>256)throw new Error("Invalid maximum color count. It must be an integer between 1 and 256.");if(!e.length||t<2||t>256||!e.length||t<2||t>256)return!1;for(var c=[],s=new Set,l=0;l<e.length;l++){var u=e[l],_=u.join(",");s.has(_)||(s.add(_),c.push(u))}if(c.length<=t)return new K(c);var h=(function(f){var v,H=new Array(32768);return f.forEach(function(A){v=r(A[0]>>3,A[1]>>3,A[2]>>3),H[v]=(H[v]||0)+1}),H})(e);h.forEach(function(){});var I=(function(f,v){var H,A,d,b=1e6,N=0,S=1e6,Y=0,w=1e6,M=0;return f.forEach(function(x){(H=x[0]>>3)<b?b=H:H>N&&(N=H),(A=x[1]>>3)<S?S=A:A>Y&&(Y=A),(d=x[2]>>3)<w?w=d:d>M&&(M=d)}),new o(b,N,S,Y,w,M,v)})(e,h),m=new n(function(f,v){return y(f.count(),v.count())});function g(f,v){for(var H,A=f.size(),d=0;d<1e3;){if(A>=v||d++>1e3)return;if((H=f.pop()).count()){var b=E(h,H),N=b[0],S=b[1];if(!N)return;f.push(N),S&&(f.push(S),A++)}else f.push(H),d++}}m.push(I),g(m,.75*t);for(var C=new n(function(f,v){return y(f.count()*f.volume(),v.count()*v.volume())});m.size();)C.push(m.pop());g(C,t);for(var p=new a;C.size();)p.push(C.pop());return p}}})().quantize,P=function(r){this.canvas=document.createElement("canvas"),this.context=this.canvas.getContext("2d"),this.width=this.canvas.width=r.naturalWidth,this.height=this.canvas.height=r.naturalHeight,this.context.drawImage(r,0,0,this.width,this.height)};P.prototype.getImageData=function(){return this.context.getImageData(0,0,this.width,this.height)};var L=function(){};L.prototype.getColor=function(r,n){return n===void 0&&(n=10),this.getPalette(r,5,n)[0]},L.prototype.getPalette=function(r,n,o){var a=(function(c){var s=c.colorCount,l=c.quality;if(s!==void 0&&Number.isInteger(s)){if(s===1)throw new Error("colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()");s=Math.max(s,2),s=Math.min(s,20)}else s=10;return(l===void 0||!Number.isInteger(l)||l<1)&&(l=10),{colorCount:s,quality:l}})({colorCount:n,quality:o}),E=new P(r),e=(function(c,s,l){for(var u,_,h,I,m,g=c,C=[],p=0;p<s;p+=l)_=g[0+(u=4*p)],h=g[u+1],I=g[u+2],((m=g[u+3])===void 0||m>=125)&&(_>250&&h>250&&I>250||C.push([_,h,I]));return C})(E.getImageData().data,E.width*E.height,a.quality),t=Z(e,a.colorCount);return t?t.palette():null},L.prototype.getColorFromUrl=function(r,n,o){var a=this,E=document.createElement("img");E.addEventListener("load",function(){var e=a.getPalette(E,5,o);n(e[0],r)}),E.src=r},L.prototype.getImageData=function(r,n){var o=new XMLHttpRequest;o.open("GET",r,!0),o.responseType="arraybuffer",o.onload=function(){if(this.status==200){var a=new Uint8Array(this.response);i=a.length;for(var E=new Array(i),e=0;e<a.length;e++)E[e]=String.fromCharCode(a[e]);var t=E.join(""),c=window.btoa(t);n("data:image/png;base64,"+c)}},o.send()},L.prototype.getColorAsync=function(r,n,o){var a=this;this.getImageData(r,function(E){var e=document.createElement("img");e.addEventListener("load",function(){var t=a.getPalette(e,5,o);n(t[0],this)}),e.src=E})};function $(r,n,o){r/=255,n/=255,o/=255;let a=Math.max(r,n,o),E=Math.min(r,n,o),e=a-E,t=0;e!==0&&(a===r?t=(n-o)/e%6:a===n?t=(o-r)/e+2:t=(r-n)/e+4,t*=60,t<0&&(t+=360));let c=a===0?0:e/a;return{h:t,s:c,v:a}}function tt(r){return"#"+r.map(n=>n.toString(16).padStart(2,"0")).join("")}function et(r){let{s:n,v:o}=$(r[0],r[1],r[2]);return n<=.02&&o>=.96?[229,229,229]:o<=.08?[46,46,46]:tt(r)}async function k(r){return(await new L().getPalette(r,5)).map(et).filter((E,e,t)=>t.indexOf(E)===e).slice(0,3)}var G=class{constructor(n){this.srcImg=null;this.selectedColor="";this.maker=new D;this.srcImg=n.srcImg}async getColors(){let n=[];if(!this.srcImg)throw new Error("srcImg is null");return this.maker.on("imgLoaded",o=>{k(o).then(a=>{n=a,this.selectedColor=a[0]})}),await this.maker.setSrc(this.srcImg),n}setColor(n){this.selectedColor=n}async generate(n){let o={outputImage:"",agcImage:""};return this.maker.hasSrc()&&this.selectedColor&&await this.maker.make(this.selectedColor).then(({preview512:a,preview216:E})=>{n?.agc&&(o.agcImage=E.toDataURL("image/png")),o.outputImage=a.toDataURL("image/png")}),o}};export{G as AsIconGenerator};
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@atomicservice/as-icon-generator",
|
|
3
|
+
"version": "1.0.12",
|
|
4
|
+
"author": "atomicservice",
|
|
5
|
+
"description": "atomicservice icon generator",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"module": "./dist/index.mjs",
|
|
11
|
+
"exports": {
|
|
12
|
+
"require": "./dist/index.js",
|
|
13
|
+
"import": "./dist/index.mjs"
|
|
14
|
+
},
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "^22.15.18",
|
|
19
|
+
"@typescript-eslint/eslint-plugin": "^8.32.0",
|
|
20
|
+
"@typescript-eslint/parser": "^8.32.0",
|
|
21
|
+
"eslint": "^9.26.0",
|
|
22
|
+
"rimraf": "^6.0.1",
|
|
23
|
+
"tsup": "^8.4.0",
|
|
24
|
+
"vitest": "3.1.3"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"colorthief": "2.6.0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"dev": "tsup --sourcemap --no-minify --dts",
|
|
31
|
+
"build": "tsup --minify",
|
|
32
|
+
"lint": "eslint src --ext ts",
|
|
33
|
+
"lint:fix": "eslint src --ext ts --fix",
|
|
34
|
+
"watch": "tsup --watch ./src",
|
|
35
|
+
"clean": "rimraf ./dist",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"release": "pnpm run build && pnpm pack"
|
|
38
|
+
}
|
|
39
|
+
}
|