extract-colors
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -1,1 +0,1 @@ | ||
"use strict";function _interopDefault(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var canvas=_interopDefault(require("canvas"));class Color{constructor(t,e,n,i){this.isColor=!0,this.red=t,this.green=e,this.blue=n,this.hex=i,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,e,n,i){return this.children[t]?this.children[t].count++:this.children[t]=new Color(e,n,i,t),this.children[t]}getList(){return Object.keys(this.children).map(t=>this.children[t])}getMaxWeight(t){if(void 0===this.maxWeight){const e=this.getList().map(e=>e.isColor?e.getWeight(t):e.getMaxWeight(t));e.sort((t,e)=>e-t),this.maxWeight=e[0]||0}return this.maxWeight}getMaxWeightColor(t){const e=this.getList();return e.sort((e,n)=>{if(e.isColor)return n.getWeight(t)-e.getWeight(t);return n.getMaxWeight(t)-e.getMaxWeight(t)}),e[0].isColor?e[0]:e[0].getMaxWeightColor(t)}getColors(t,e){const n=this.getList().map(t=>{const n=t.count;const i=t.getMaxWeightColor(e);i.count=n;return i});n.sort((t,n)=>n.getWeight(e)-t.getWeight(e));const i=[];return n.forEach(e=>{const n=i.find(n=>n.distance(e)<t);n?n.count+=e.count:i.push(e)}),i}}class ColorsExtractor{constructor(t,{pixels:pixels=1e4,distance:distance=150,saturationImportance:saturationImportance=5,splitPower:splitPower=10,colorValidator:colorValidator=((red,green,blue,alpha=255)=>alpha>250)}={}){this.pixels=pixels,this.splitPower=splitPower,this.distance=distance,this.saturationImportance=saturationImportance,this.colorValidator=colorValidator}process(t){const e=new ColorGroup,n=this.splitPower;for(let i=0;i<t.length;i+=4){const o=t[i],r=t[i+1],a=t[i+2],s=t[i+3];if(this.colorValidator(o,r,a,s)){const t=o<<16|r<<8|a,i=(o>>4&15)<<2|(r>>4&15)<<1|a>>4&15,s=Math.round(o*(n-1)/255)*(n*n)+Math.round(r*(n-1)/255)*n+Math.round(a*(n-1)/255);e.addGroup(s).addGroup(i).addColor(t,o,r,a)}}return e.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}))}}const{createCanvas:createCanvas,loadImage:loadImage}=canvas,getImageData=(t,e)=>{const n=t.width*t.height;const i=n<e?t.width:Math.round(t.width*Math.sqrt(e/n));const o=n<e?t.height:Math.round(t.height*Math.sqrt(e/n));const r=createCanvas(i,o);const a=r.getContext("2d");a.drawImage(t,0,0,t.width,t.height,0,0,i,o);return a.getImageData(0,0,i,o)};var extractColorsModule=(t,e)=>loadImage(t).then(t=>{const n=new ColorsExtractor(e);const i=getImageData(t,n.pixels).data;return n.extract(i)});module.exports=extractColorsModule; | ||
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}; |
@@ -1,1 +0,1 @@ | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(t=t||self,t.extractColors=n())}(this,function(){"use strict";class t{constructor(t,n,e,i){this.isColor=!0,this.red=t,this.green=n,this.blue=e,this.hex=i,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 n{constructor(){this.count=1,this.children={}}addGroup(t){return this.children[t]?this.children[t].count++:this.children[t]=new n,this.children[t]}addColor(n,e,i,o){return this.children[n]?this.children[n].count++:this.children[n]=new t(e,i,o,n),this.children[n]}getList(){return Object.keys(this.children).map(t=>this.children[t])}getMaxWeight(t){if(void 0===this.maxWeight){const n=this.getList().map(n=>n.isColor?n.getWeight(t):n.getMaxWeight(t));n.sort((t,n)=>n-t),this.maxWeight=n[0]||0}return this.maxWeight}getMaxWeightColor(t){const n=this.getList();return n.sort((n,e)=>{if(n.isColor)return e.getWeight(t)-n.getWeight(t);return e.getMaxWeight(t)-n.getMaxWeight(t)}),n[0].isColor?n[0]:n[0].getMaxWeightColor(t)}getColors(t,n){const e=this.getList().map(t=>{const e=t.count;const i=t.getMaxWeightColor(n);i.count=e;return i});e.sort((t,e)=>e.getWeight(n)-t.getWeight(n));const i=[];return e.forEach(n=>{const e=i.find(e=>e.distance(n)<t);e?e.count+=n.count:i.push(n)}),i}}class e{constructor(t,{pixels:pixels=1e4,distance:distance=150,saturationImportance:saturationImportance=5,splitPower:splitPower=10,colorValidator:colorValidator=((red,green,blue,alpha=255)=>alpha>250)}={}){this.pixels=pixels,this.splitPower=splitPower,this.distance=distance,this.saturationImportance=saturationImportance,this.colorValidator=colorValidator}process(t){const e=new n,i=this.splitPower;for(let n=0;n<t.length;n+=4){const o=t[n],s=t[n+1],r=t[n+2],h=t[n+3];if(this.colorValidator(o,s,r,h)){const t=o<<16|s<<8|r,n=(o>>4&15)<<2|(s>>4&15)<<1|r>>4&15,h=Math.round(o*(i-1)/255)*(i*i)+Math.round(s*(i-1)/255)*i+Math.round(r*(i-1)/255);e.addGroup(h).addGroup(n).addColor(t,o,s,r)}}return e.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}))}}const i=(t,n)=>{const e=t.width*t.height;const i=e<n?t.width:Math.round(t.width*Math.sqrt(n/e));const o=e<n?t.height:Math.round(t.height*Math.sqrt(n/e));const s=document.createElement("canvas");s.width=i;s.height=o;const r=s.getContext("2d");r.drawImage(t,0,0,t.width,t.height,0,0,i,o);return r.getImageData(0,0,i,o)};return(t,n)=>new Promise((o,s)=>{const r=new Image;const h=()=>{r.removeEventListener("load",h);const t=new e(n);const s=i(r,t.pixels).data;o(t.extract(s))};r.addEventListener("load",h);r.src=t})}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):(t=t||self,e(t.extractColors={}))}(this,function(t){"use strict";class e{constructor(t,e,n,i){this.isColor=!0,this.red=t,this.green=e,this.blue=n,this.hex=i,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 n{constructor(){this.count=1,this.children={}}addGroup(t){return this.children[t]?this.children[t].count++:this.children[t]=new n,this.children[t]}addColor(t,n,i,o){return this.children[t]?this.children[t].count++:this.children[t]=new e(n,i,o,t),this.children[t]}getList(){return Object.keys(this.children).map(t=>this.children[t])}getMaxWeight(t){if(void 0===this.maxWeight){const e=this.getList().map(e=>e.isColor?e.getWeight(t):e.getMaxWeight(t));e.sort((t,e)=>e-t),this.maxWeight=e[0]||0}return this.maxWeight}getMaxWeightColor(t){const e=this.getList();return e.sort((e,n)=>{if(e.isColor)return n.getWeight(t)-e.getWeight(t);return n.getMaxWeight(t)-e.getMaxWeight(t)}),e[0].isColor?e[0]:e[0].getMaxWeightColor(t)}getColors(t,e){const n=this.getList().map(t=>{const n=t.count;const i=t.getMaxWeightColor(e);i.count=n;return i});n.sort((t,n)=>n.getWeight(e)-t.getWeight(e));const i=[];return n.forEach(e=>{const n=i.find(n=>n.distance(e)<t);n?n.count+=e.count:i.push(e)}),i}}class i{constructor(t,{pixels:pixels=i.pixelsDefault,distance:distance=i.distanceDefault,saturationImportance:saturationImportance=i.saturationImportanceDefault,splitPower:splitPower=i.splitPowerDefault,colorValidator:colorValidator=i.colorValidatorDefault}={}){this.pixels=pixels,this.splitPower=splitPower,this.distance=distance,this.saturationImportance=saturationImportance,this.colorValidator=colorValidator}process(t){const e=new n,i=this.splitPower;for(let n=0;n<t.length;n+=4){const o=t[n],r=t[n+1],s=t[n+2],a=t[n+3];if(this.colorValidator(o,r,s,a)){const t=o<<16|r<<8|s,n=(o>>4&15)<<2|(r>>4&15)<<1|s>>4&15,a=Math.round(o*(i-1)/255)*(i*i)+Math.round(r*(i-1)/255)*i+Math.round(s*(i-1)/255);e.addGroup(a).addGroup(n).addColor(t,o,r,s)}}return e.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}))}}i.pixelsDefault=1e4,i.distanceDefault=150,i.saturationImportanceDefault=5,i.splitPowerDefault=10,i.colorValidatorDefault=((t,e,n,i=255)=>i>250);const o=(t,e)=>{const n=t.width*t.height;const i=n<e?t.width:Math.round(t.width*Math.sqrt(e/n));const o=n<e?t.height:Math.round(t.height*Math.sqrt(e/n));const r=document.createElement("canvas");r.width=i;r.height=o;const s=r.getContext("2d");s.drawImage(t,0,0,t.width,t.height,0,0,i,o);return s.getImageData(0,0,i,o)},r=(t,e)=>{const n=new i(e);return n.extract(t.data)},s=(t,e)=>new Promise(n=>{const s=(t,e)=>{const s=o(t,i.pixelsDefault);n(r(s,e))};if(t.complete)s(t,e);else{const n=()=>{t.removeEventListener("load",n);s(t,e)};t.addEventListener("load",n)}}),a=(t,e)=>{const n=new Image;n.src=t;s(n,e)},c=(t,e)=>{if(t instanceof ImageData)return new Promise(n=>{n(r(t,e))});if(t instanceof Image)return s(t,e);return a(t,e)};t.default=c,t.extractColorsFromImage=s,t.extractColorsFromImageData=r,t.extractColorsFromSrc=a,Object.defineProperty(t,"__esModule",{value:!0})}); |
{ | ||
"name": "extract-colors", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "Extract color palettes from images", | ||
"main": "index.js", | ||
"main": "dist/extract-colors.cjs.js", | ||
"browser": "dist/extract-colors.umd.js", | ||
@@ -7,0 +7,0 @@ "module": "dist/extract-colors.cjs.js", |
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
10293
30
2