extract-colors
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -1,1 +0,1 @@ | ||
class Color{constructor(t,o,r,e){this.isColor=!0,this.red=t,this.green=o,this.blue=r,this.hex=e,this.count=1}distance(t){return Math.abs(t.red-this.red)+Math.abs(t.green-this.green)+Math.abs(t.blue-this.blue)}getWeight(t){return this.count+this.getSaturation()*t}getSaturation(){return void 0===this.saturation&&(this.saturation=Math.max(Math.abs(this.red-this.green),Math.abs(this.red-this.blue),Math.abs(this.green-this.blue))),this.saturation}}class ColorGroup{constructor(){this.count=1,this.children={}}addGroup(t){return this.children[t]?this.children[t].count++:this.children[t]=new ColorGroup,this.children[t]}addColor(t,o,r,e){return this.children[t]?this.children[t].count++:this.children[t]=new Color(o,r,e,t),this.children[t]}getList(){return Object.keys(this.children).map(t=>this.children[t])}getMaxWeight(t){if(void 0===this.maxWeight){const o=this.getList().map(o=>o.isColor?o.getWeight(t):o.getMaxWeight(t));o.sort((t,o)=>o-t),this.maxWeight=o[0]||0}return this.maxWeight}getMaxWeightColor(t){const o=this.getList();return o.sort((o,r)=>{if(o.isColor)return r.getWeight(t)-o.getWeight(t);return r.getMaxWeight(t)-o.getMaxWeight(t)}),o[0].isColor?o[0]:o[0].getMaxWeightColor(t)}getColors(t,o){const r=this.getList().map(t=>{const r=t.count;const e=t.getMaxWeightColor(o);e.count=r;return e});r.sort((t,r)=>r.getWeight(o)-t.getWeight(o));const e=[];return r.forEach(o=>{const r=e.find(r=>r.distance(o)<t);r?r.count+=o.count:e.push(o)}),e}}class ColorsExtractor{constructor(t,{pixels:pixels=ColorsExtractor.pixelsDefault,distance:distance=ColorsExtractor.distanceDefault,saturationImportance:saturationImportance=ColorsExtractor.saturationImportanceDefault,splitPower:splitPower=ColorsExtractor.splitPowerDefault,colorValidator:colorValidator=ColorsExtractor.colorValidatorDefault}={}){this.pixels=pixels,this.splitPower=splitPower,this.distance=distance,this.saturationImportance=saturationImportance,this.colorValidator=colorValidator}process(t){const o=new ColorGroup,r=this.splitPower;for(let e=0;e<t.length;e+=4){const n=t[e],i=t[e+1],a=t[e+2],s=t[e+3];if(this.colorValidator(n,i,a,s)){const t=n<<16|i<<8|a,e=(n>>4&15)<<2|(i>>4&15)<<1|a>>4&15,s=Math.round(n*(r-1)/255)*(r*r)+Math.round(i*(r-1)/255)*r+Math.round(a*(r-1)/255);o.addGroup(s).addGroup(e).addColor(t,n,i,a)}}return o.getColors(this.distance,this.saturationImportance)}extract(t){return this.process(t).map(t=>({hex:"#"+"0".repeat(6-t.hex.toString(16).length)+t.hex.toString(16),red:t.red,green:t.green,blue:t.blue,area:t.count/this.pixels,saturation:t.saturation/255}))}}ColorsExtractor.pixelsDefault=1e4,ColorsExtractor.distanceDefault=150,ColorsExtractor.saturationImportanceDefault=5,ColorsExtractor.splitPowerDefault=10,ColorsExtractor.colorValidatorDefault=((t,o,r,e=255)=>e>250);const{createCanvas:createCanvas,loadImage:loadImage}=require("canvas"),getImageData=(t,o)=>{const r=t.width*t.height;const e=r<o?t.width:Math.round(t.width*Math.sqrt(o/r));const n=r<o?t.height:Math.round(t.height*Math.sqrt(o/r));const i=createCanvas(e,n);const a=i.getContext("2d");a.drawImage(t,0,0,t.width,t.height,0,0,e,n);return a.getImageData(0,0,e,n)},extractColorsFromImageData=(t,o)=>{const r=new ColorsExtractor(o);return r.extract(t.data)},extractColorsFromSrc=(t,o)=>{const r=new Image;r.src=t;extractColorsFromImage(r,o);return loadImage(t).then(t=>{const r=getImageData(t,ColorsExtractor.pixelsDefault);return extractColorsFromImageData(r,o)})},extractColors=(t,o)=>{if(t.width&&t.height&&t.data&&t.data.length)return new Promise(r=>{r(extractColorsFromImageData(t,o))});return extractColorsFromSrc(t,o)};export default extractColors;export{extractColorsFromImageData,extractColorsFromSrc}; | ||
class Color{constructor(t,o,r,e){this.isColor=!0,this.red=t,this.green=o,this.blue=r,this.hex=e,this.count=1}distance(t){return Math.abs(t.red-this.red)+Math.abs(t.green-this.green)+Math.abs(t.blue-this.blue)}getWeight(t){return this.count+this.getSaturation()*t}getSaturation(){return void 0===this.saturation&&(this.saturation=Math.max(Math.abs(this.red-this.green),Math.abs(this.red-this.blue),Math.abs(this.green-this.blue))),this.saturation}}class ColorGroup{constructor(){this.count=1,this.children={}}addGroup(t){return this.children[t]?this.children[t].count++:this.children[t]=new ColorGroup,this.children[t]}addColor(t,o,r,e){return this.children[t]?this.children[t].count++:this.children[t]=new Color(o,r,e,t),this.children[t]}getList(){return Object.keys(this.children).map(t=>this.children[t])}getMaxWeight(t){if(void 0===this.maxWeight){const o=this.getList().map(o=>o.isColor?o.getWeight(t):o.getMaxWeight(t));o.sort((t,o)=>o-t),this.maxWeight=o[0]||0}return this.maxWeight}getMaxWeightColor(t){const o=this.getList();return o.sort((o,r)=>{if(o.isColor)return r.getWeight(t)-o.getWeight(t);return r.getMaxWeight(t)-o.getMaxWeight(t)}),o[0].isColor?o[0]:o[0].getMaxWeightColor(t)}getColors(t,o){const r=this.getList().map(t=>{const r=t.count;const e=t.getMaxWeightColor(o);e.count=r;return e});r.sort((t,r)=>r.getWeight(o)-t.getWeight(o));const e=[];return r.forEach(o=>{const r=e.find(r=>r.distance(o)<t);r?r.count+=o.count:e.push(o)}),e}}class ColorsExtractor{constructor(t,{pixels:pixels=ColorsExtractor.pixelsDefault,distance:distance=ColorsExtractor.distanceDefault,saturationImportance:saturationImportance=ColorsExtractor.saturationImportanceDefault,splitPower:splitPower=ColorsExtractor.splitPowerDefault,colorValidator:colorValidator=ColorsExtractor.colorValidatorDefault}={}){this.pixels=pixels,this.splitPower=splitPower,this.distance=distance,this.saturationImportance=saturationImportance,this.colorValidator=colorValidator}process(t){const o=new ColorGroup,r=this.splitPower;for(let e=0;e<t.length;e+=4){const a=t[e],n=t[e+1],i=t[e+2],s=t[e+3];if(this.colorValidator(a,n,i,s)){const t=a<<16|n<<8|i,e=(a>>4&15)<<2|(n>>4&15)<<1|i>>4&15,s=Math.round(a*(r-1)/255)*(r*r)+Math.round(n*(r-1)/255)*r+Math.round(i*(r-1)/255);o.addGroup(s).addGroup(e).addColor(t,a,n,i)}}return o.getColors(this.distance,this.saturationImportance)}extract(t){return this.process(t).map(t=>({hex:"#"+"0".repeat(6-t.hex.toString(16).length)+t.hex.toString(16),red:t.red,green:t.green,blue:t.blue,area:t.count/this.pixels,saturation:t.saturation/255}))}}ColorsExtractor.pixelsDefault=1e4,ColorsExtractor.distanceDefault=150,ColorsExtractor.saturationImportanceDefault=5,ColorsExtractor.splitPowerDefault=10,ColorsExtractor.colorValidatorDefault=((t,o,r,e=255)=>e>250);const{createCanvas:createCanvas,loadImage:loadImage}=require("canvas"),getImageData=(t,o)=>{const r=t.width*t.height;const e=r<o?t.width:Math.round(t.width*Math.sqrt(o/r));const a=r<o?t.height:Math.round(t.height*Math.sqrt(o/r));const n=createCanvas(e,a);const i=n.getContext("2d");i.drawImage(t,0,0,t.width,t.height,0,0,e,a);return i.getImageData(0,0,e,a)},extractColorsFromImageData=(t,o)=>{const r=new ColorsExtractor(o);return r.extract(t.data)},extractColorsFromSrc=(t,o)=>{const r=new Image;r.src=t;extractColorsFromImage(r,o);return loadImage(t).then(t=>{const r=getImageData(t,ColorsExtractor.pixelsDefault);return extractColorsFromImageData(r,o)})},extractColors=(t,o)=>{if(t.width&&t.height&&t.data&&t.data.length)return new Promise(r=>{r(extractColorsFromImageData(t,o))});return extractColorsFromSrc(t,o)};module.exports={extractColorsFromImageData:extractColorsFromImageData,extractColorsFromSrc:extractColorsFromSrc,extractColors:extractColors}; |
{ | ||
"name": "extract-colors", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Extract color palettes from images", | ||
@@ -5,0 +5,0 @@ "main": "dist/extract-colors.cjs.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10349
3