extract-colors
Advanced tools
Comparing version 0.2.5 to 0.2.6
@@ -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 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)=>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}; | ||
class Color{constructor(t,r,o,e){this.isColor=!0,this.red=t,this.green=r,this.blue=o,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,r,o,e){return this.children[t]?this.children[t].count++:this.children[t]=new Color(r,o,e,t),this.children[t]}getList(){return Object.keys(this.children).map(t=>this.children[t])}getMaxWeight(t){if(void 0===this.maxWeight){const r=this.getList().map(r=>r.isColor?r.getWeight(t):r.getMaxWeight(t));r.sort((t,r)=>r-t),this.maxWeight=r[0]||0}return this.maxWeight}getMaxWeightColor(t){const r=this.getList();return r.sort((r,o)=>{if(r.isColor)return o.getWeight(t)-r.getWeight(t);return o.getMaxWeight(t)-r.getMaxWeight(t)}),r[0].isColor?r[0]:r[0].getMaxWeightColor(t)}getColors(t,r){const o=this.getList().map(t=>{const o=t.count;const e=t.getMaxWeightColor(r);e.count=o;return e});o.sort((t,o)=>o.getWeight(r)-t.getWeight(r));const e=[];return o.forEach(r=>{const o=e.find(o=>o.distance(r)<t);o?o.count+=r.count:e.push(r)}),e}}const testUint=(t,r,o=0,e=Number.MAX_SAFE_INTEGER)=>{if(!Number.isInteger(r)||r<o||r>e)throw new Error(t+" is invalid");return parseInt(r)},testNumber=(t,r,o=0,e=Number.MAX_VALUE)=>{if(Number(r)!=r||r<o||r>e)throw new Error(t+" is invalid");return Number(r)},testFunction=(t,r)=>{if(!r||"[object Function]"!=={}.toString.call(r))throw new Error(t+" is invalid");return r};class ColorsExtractor{constructor({pixels:pixels=ColorsExtractor.pixelsDefault,distance:distance=ColorsExtractor.distanceDefault,saturationImportance:saturationImportance=ColorsExtractor.saturationImportanceDefault,splitPower:splitPower=ColorsExtractor.splitPowerDefault,colorValidator:colorValidator=ColorsExtractor.colorValidatorDefault}={}){this.pixels=testUint("pixels",pixels,1),this.splitPower=testNumber("splitPower",splitPower,2,16),this.distance=testUint("distance",distance,1,762),this.saturationImportance=testNumber("saturationImportance",saturationImportance,0),this.colorValidator=testFunction("colorValidator",colorValidator)}process(t){const r=new ColorGroup,o=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*(o-1)/255)*(o*o)+Math.round(i*(o-1)/255)*o+Math.round(a*(o-1)/255);r.addGroup(s).addGroup(e).addColor(t,n,i,a)}}return r.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,r,o,e=255)=>e>250);const{createCanvas:createCanvas,loadImage:loadImage}=require("canvas"),getImageData=(t,r)=>{const o=t.width*t.height;const e=o<r?t.width:Math.round(t.width*Math.sqrt(r/o));const n=o<r?t.height:Math.round(t.height*Math.sqrt(r/o));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,r)=>{const o=new ColorsExtractor(r);return o.extract(t.data)},extractColorsFromSrc=(t,r)=>loadImage(t).then(t=>{const o=new ColorsExtractor(r);const e=getImageData(t,o.pixels);return o.extract(e.data)}),extractColors=(t,r)=>{if(t.width&&t.height&&t.data&&t.data.length)return new Promise(o=>{o(extractColorsFromImageData(t,r))});return extractColorsFromSrc(t,r)};module.exports={extractColorsFromImageData:extractColorsFromImageData,extractColorsFromSrc:extractColorsFromSrc,extractColors:extractColors}; |
@@ -1,1 +0,1 @@ | ||
!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})}); | ||
!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}}const i=(t,e,n=0,i=Number.MAX_SAFE_INTEGER)=>{if(!Number.isInteger(e)||e<n||e>i)throw new Error(t+" is invalid");return parseInt(e)},o=(t,e,n=0,i=Number.MAX_VALUE)=>{if(Number(e)!=e||e<n||e>i)throw new Error(t+" is invalid");return Number(e)},r=(t,e)=>{if(!e||"[object Function]"!=={}.toString.call(e))throw new Error(t+" is invalid");return e};class s{constructor({pixels:pixels=s.pixelsDefault,distance:distance=s.distanceDefault,saturationImportance:saturationImportance=s.saturationImportanceDefault,splitPower:splitPower=s.splitPowerDefault,colorValidator:colorValidator=s.colorValidatorDefault}={}){this.pixels=i("pixels",pixels,1),this.splitPower=o("splitPower",splitPower,2,16),this.distance=i("distance",distance,1,762),this.saturationImportance=o("saturationImportance",saturationImportance,0),this.colorValidator=r("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}))}}s.pixelsDefault=1e4,s.distanceDefault=150,s.saturationImportanceDefault=5,s.splitPowerDefault=10,s.colorValidatorDefault=((t,e,n,i=255)=>i>250);const a=(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)},c=(t,e)=>{const n=new s(e);return n.extract(t.data)},u=(t,e)=>new Promise(n=>{const i=(t,e)=>{const i=new s(e);const o=a(t,i.pixels);n(i.extract(o.data))};if(t.complete)i(t,e);else{const n=()=>{t.removeEventListener("load",n);i(t,e)};t.addEventListener("load",n)}}),h=(t,e)=>{const n=new Image;n.src=t;u(n,e)},l=(t,e)=>{if(t instanceof ImageData)return new Promise(n=>{n(c(t,e))});if(t instanceof Image)return u(t,e);return h(t,e)};t.default=l,t.extractColorsFromImage=u,t.extractColorsFromImageData=c,t.extractColorsFromSrc=h,Object.defineProperty(t,"__esModule",{value:!0})}); |
{ | ||
"name": "extract-colors", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "Extract color palettes from images", | ||
@@ -12,3 +12,3 @@ "main": "dist/extract-colors.cjs.js", | ||
"lint": "eslint ./src/extractColors.js", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node test/index.js" | ||
}, | ||
@@ -40,2 +40,3 @@ "repository": { | ||
"@rollup/plugin-node-resolve": "^6.1.0", | ||
"canvas": "^2.6.1", | ||
"eslint": "^6.8.0", | ||
@@ -42,0 +43,0 @@ "rollup": "^1.27.14", |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
11546
33
2
6