Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

extract-colors

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extract-colors - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

2

dist/extract-colors.cjs.js

@@ -1,1 +0,1 @@

class Color{constructor(t,o,r,e=t<<16|o<<8|r){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))/765}getWeight(t,o){return this.count/o+this.getSaturation()*t}getSaturation(){return void 0===this.saturation&&(this.saturation=Math.max(Math.abs(this.red-this.green)/255,Math.abs(this.red-this.blue)/255,Math.abs(this.green-this.blue)/255)),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,o){if(void 0===this.maxWeight){const r=this.getList().map(r=>r.isColor?r.getWeight(t,o):r.getMaxWeight(t,o));r.sort((t,o)=>o-t),this.maxWeight=r[0]||0}return this.maxWeight}getMaxWeightColor(t,o){const r=this.getList();return r.sort((r,e)=>{if(r.isColor)return e.getWeight(t,o)-r.getWeight(t,o);return e.getMaxWeight(t,o)-r.getMaxWeight(t,o)}),r[0].isColor?r[0]:r[0].getMaxWeightColor(t,o)}getMaxCountColor(){const t=this.getList();return t.sort((t,o)=>{if(t.isColor)return o.count-t.count;return o.getMaxCountColor()-t.getMaxCountColor()}),t[0].isColor?t[0]:t[0].getMaxCountColor()}getColors(t,o,r){const e=this.getList().map(t=>{const o=t.count;const r=t.getMaxCountColor();r.count=o;return r});e.sort((t,e)=>e.getWeight(o,r)-t.getWeight(o,r));const n=[];return e.forEach(o=>{const r=n.find(r=>r.distance(o)<t);r?r.count+=o.count:n.push(o)}),n}}const testUint=(t,o,r=0,e=Number.MAX_SAFE_INTEGER)=>{if(!Number.isInteger(o)||o<r||o>e)throw new Error(t+" is invalid");return parseInt(o)},testNumber=(t,o,r=0,e=Number.MAX_VALUE)=>{if(Number(o)!=o||o<r||o>e)throw new Error(t+" is invalid");return Number(o)},testFunction=(t,o)=>{if(!o||"[object Function]"!=={}.toString.call(o))throw new Error(t+" is invalid");return o};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=testNumber("distance",distance,0,1),this.saturationImportance=testNumber("saturationImportance",saturationImportance,0),this.colorValidator=testFunction("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,this.pixels)}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=.2,ColorsExtractor.saturationImportanceDefault=.2,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)=>loadImage(t).then(t=>{const r=new ColorsExtractor(o);const e=getImageData(t,r.pixels);return r.extract(e.data)}),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,o,r,e=t<<16|o<<8|r){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))/765}getWeight(t,o){return this.count/o*(1-t)+this.getSaturation()*t}getSaturation(){return void 0===this.saturation&&(this.saturation=Math.max(Math.abs(this.red-this.green)/255,Math.abs(this.red-this.blue)/255,Math.abs(this.green-this.blue)/255)),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,o){if(void 0===this.maxWeight){const r=this.getList().map(r=>r.isColor?r.getWeight(t,o):r.getMaxWeight(t,o));r.sort((t,o)=>o-t),this.maxWeight=r[0]||0}return this.maxWeight}getMaxWeightColor(t,o){const r=this.getList();return r.sort((r,e)=>{if(r.isColor)return e.getWeight(t,o)-r.getWeight(t,o);return e.getMaxWeight(t,o)-r.getMaxWeight(t,o)}),r[0].isColor?r[0]:r[0].getMaxWeightColor(t,o)}getMaxCountColor(){const t=this.getList();return t.sort((t,o)=>{if(t.isColor)return o.count-t.count;return o.getMaxCountColor()-t.getMaxCountColor()}),t[0].isColor?t[0]:t[0].getMaxCountColor()}getColors(t,o,r){const e=this.getList().map(t=>{const o=t.count;const r=t.getMaxCountColor();r.count=o;return r});e.sort((t,e)=>e.getWeight(o,r)-t.getWeight(o,r));const n=[];return e.forEach(o=>{const r=n.find(r=>r.distance(o)<t);r?r.count+=o.count:n.push(o)}),n}}const testUint=(t,o,r=0,e=Number.MAX_SAFE_INTEGER)=>{if(!Number.isInteger(o)||o<r||o>e)throw new Error(t+" is invalid");return parseInt(o)},testNumber=(t,o,r=0,e=Number.MAX_VALUE)=>{if(Number(o)!=o||o<r||o>e)throw new Error(t+" is invalid");return Number(o)},testFunction=(t,o)=>{if(!o||"[object Function]"!=={}.toString.call(o))throw new Error(t+" is invalid");return o};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=testNumber("distance",distance,0,1),this.saturationImportance=testNumber("saturationImportance",saturationImportance,0,1),this.colorValidator=testFunction("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,this.pixels)}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=.2,ColorsExtractor.saturationImportanceDefault=.2,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)=>loadImage(t).then(t=>{const r=new ColorsExtractor(o);const e=getImageData(t,r.pixels);return r.extract(e.data)}),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};

@@ -1,1 +0,1 @@

!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):(t=t||self,n(t.extractColors={}))}(this,function(t){"use strict";class n{constructor(t,n,e,i=t<<16|n<<8|e){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))/765}getWeight(t,n){return this.count/n+this.getSaturation()*t}getSaturation(){return void 0===this.saturation&&(this.saturation=Math.max(Math.abs(this.red-this.green)/255,Math.abs(this.red-this.blue)/255,Math.abs(this.green-this.blue)/255)),this.saturation}}class e{constructor(){this.count=1,this.children={}}addGroup(t){return this.children[t]?this.children[t].count++:this.children[t]=new e,this.children[t]}addColor(t,e,i,o){return this.children[t]?this.children[t].count++:this.children[t]=new n(e,i,o,t),this.children[t]}getList(){return Object.keys(this.children).map(t=>this.children[t])}getMaxWeight(t,n){if(void 0===this.maxWeight){const e=this.getList().map(e=>e.isColor?e.getWeight(t,n):e.getMaxWeight(t,n));e.sort((t,n)=>n-t),this.maxWeight=e[0]||0}return this.maxWeight}getMaxWeightColor(t,n){const e=this.getList();return e.sort((e,i)=>{if(e.isColor)return i.getWeight(t,n)-e.getWeight(t,n);return i.getMaxWeight(t,n)-e.getMaxWeight(t,n)}),e[0].isColor?e[0]:e[0].getMaxWeightColor(t,n)}getMaxCountColor(){const t=this.getList();return t.sort((t,n)=>{if(t.isColor)return n.count-t.count;return n.getMaxCountColor()-t.getMaxCountColor()}),t[0].isColor?t[0]:t[0].getMaxCountColor()}getColors(t,n,e){const i=this.getList().map(t=>{const n=t.count;const e=t.getMaxCountColor();e.count=n;return e});i.sort((t,i)=>i.getWeight(n,e)-t.getWeight(n,e));const o=[];return i.forEach(n=>{const e=o.find(e=>e.distance(n)<t);e?e.count+=n.count:o.push(n)}),o}}const i=(t,n,e=0,i=Number.MAX_SAFE_INTEGER)=>{if(!Number.isInteger(n)||n<e||n>i)throw new Error(t+" is invalid");return parseInt(n)},o=(t,n,e=0,i=Number.MAX_VALUE)=>{if(Number(n)!=n||n<e||n>i)throw new Error(t+" is invalid");return Number(n)},r=(t,n)=>{if(!n||"[object Function]"!=={}.toString.call(n))throw new Error(t+" is invalid");return n};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=o("distance",distance,0,1),this.saturationImportance=o("saturationImportance",saturationImportance,0),this.colorValidator=r("colorValidator",colorValidator)}process(t){const n=new e,i=this.splitPower;for(let e=0;e<t.length;e+=4){const o=t[e],r=t[e+1],s=t[e+2],a=t[e+3];if(this.colorValidator(o,r,s,a)){const t=o<<16|r<<8|s,e=(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);n.addGroup(a).addGroup(e).addColor(t,o,r,s)}}return n.getColors(this.distance,this.saturationImportance,this.pixels)}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=.2,s.saturationImportanceDefault=.2,s.splitPowerDefault=10,s.colorValidatorDefault=((t,n,e,i=255)=>i>250);const a=(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 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,n)=>{const e=new s(n);return e.extract(t.data)},u=(t,n)=>{t.crossOrigin=n&&n.crossOrigin||null;return new Promise(e=>{const i=(t,n)=>{const i=new s(n);const o=a(t,i.pixels);e(i.extract(o.data))};if(t.complete)i(t,n);else{const e=()=>{t.removeEventListener("load",e);i(t,n)};t.addEventListener("load",e)}})},h=(t,n)=>{const e=new Image;e.src=t;return u(e,n)},l=(t,n)=>{if(t instanceof ImageData)return new Promise(e=>{e(c(t,n))});if(t instanceof Image)return u(t,n);return h(t,n)};t.default=l,t.extractColorsFromImage=u,t.extractColorsFromImageData=c,t.extractColorsFromSrc=h,Object.defineProperty(t,"__esModule",{value:!0})});
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):(t=t||self,n(t.extractColors={}))}(this,function(t){"use strict";class n{constructor(t,n,e,i=t<<16|n<<8|e){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))/765}getWeight(t,n){return this.count/n*(1-t)+this.getSaturation()*t}getSaturation(){return void 0===this.saturation&&(this.saturation=Math.max(Math.abs(this.red-this.green)/255,Math.abs(this.red-this.blue)/255,Math.abs(this.green-this.blue)/255)),this.saturation}}class e{constructor(){this.count=1,this.children={}}addGroup(t){return this.children[t]?this.children[t].count++:this.children[t]=new e,this.children[t]}addColor(t,e,i,o){return this.children[t]?this.children[t].count++:this.children[t]=new n(e,i,o,t),this.children[t]}getList(){return Object.keys(this.children).map(t=>this.children[t])}getMaxWeight(t,n){if(void 0===this.maxWeight){const e=this.getList().map(e=>e.isColor?e.getWeight(t,n):e.getMaxWeight(t,n));e.sort((t,n)=>n-t),this.maxWeight=e[0]||0}return this.maxWeight}getMaxWeightColor(t,n){const e=this.getList();return e.sort((e,i)=>{if(e.isColor)return i.getWeight(t,n)-e.getWeight(t,n);return i.getMaxWeight(t,n)-e.getMaxWeight(t,n)}),e[0].isColor?e[0]:e[0].getMaxWeightColor(t,n)}getMaxCountColor(){const t=this.getList();return t.sort((t,n)=>{if(t.isColor)return n.count-t.count;return n.getMaxCountColor()-t.getMaxCountColor()}),t[0].isColor?t[0]:t[0].getMaxCountColor()}getColors(t,n,e){const i=this.getList().map(t=>{const n=t.count;const e=t.getMaxCountColor();e.count=n;return e});i.sort((t,i)=>i.getWeight(n,e)-t.getWeight(n,e));const o=[];return i.forEach(n=>{const e=o.find(e=>e.distance(n)<t);e?e.count+=n.count:o.push(n)}),o}}const i=(t,n,e=0,i=Number.MAX_SAFE_INTEGER)=>{if(!Number.isInteger(n)||n<e||n>i)throw new Error(t+" is invalid");return parseInt(n)},o=(t,n,e=0,i=Number.MAX_VALUE)=>{if(Number(n)!=n||n<e||n>i)throw new Error(t+" is invalid");return Number(n)},r=(t,n)=>{if(!n||"[object Function]"!=={}.toString.call(n))throw new Error(t+" is invalid");return n};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=o("distance",distance,0,1),this.saturationImportance=o("saturationImportance",saturationImportance,0,1),this.colorValidator=r("colorValidator",colorValidator)}process(t){const n=new e,i=this.splitPower;for(let e=0;e<t.length;e+=4){const o=t[e],r=t[e+1],s=t[e+2],a=t[e+3];if(this.colorValidator(o,r,s,a)){const t=o<<16|r<<8|s,e=(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);n.addGroup(a).addGroup(e).addColor(t,o,r,s)}}return n.getColors(this.distance,this.saturationImportance,this.pixels)}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=.2,s.saturationImportanceDefault=.2,s.splitPowerDefault=10,s.colorValidatorDefault=((t,n,e,i=255)=>i>250);const a=(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 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,n)=>{const e=new s(n);return e.extract(t.data)},u=(t,n)=>{t.crossOrigin=n&&n.crossOrigin||null;return new Promise(e=>{const i=(t,n)=>{const i=new s(n);const o=a(t,i.pixels);e(i.extract(o.data))};if(t.complete)i(t,n);else{const e=()=>{t.removeEventListener("load",e);i(t,n)};t.addEventListener("load",e)}})},h=(t,n)=>{const e=new Image;e.src=t;return u(e,n)},l=(t,n)=>{if(t instanceof ImageData)return new Promise(e=>{e(c(t,n))});if(t instanceof Image)return u(t,n);return h(t,n)};t.default=l,t.extractColorsFromImage=u,t.extractColorsFromImageData=c,t.extractColorsFromSrc=h,Object.defineProperty(t,"__esModule",{value:!0})});
{
"name": "extract-colors",
"version": "1.0.1",
"version": "1.1.0",
"description": "Extract color palettes from images",

@@ -5,0 +5,0 @@ "main": "dist/extract-colors.cjs.js",

@@ -72,3 +72,3 @@ # Extract Colors

| distance | 0.2 | Number | From 0 to 1 is the color distance to not have near colors (1 distance is between White and Black) |
| saturationImportance | 0.2 | Number | Power of the saturation weight during the process (0 is not used, 1 is equal to the area and more is more important for the saturation) |
| saturationImportance | 0.2 | Number | Power of the saturation weight during the process (0 is not used, 1 is equal only saturaton and not area size) |
| splitPower | 10 | Integer | Approximation power in the first color splitting during process (from 2 to 16) |

@@ -75,0 +75,0 @@ | colorValidator | (red, green, blue, alpha = 255) => alpha > 250 | Function | Lamda function to disable some colors |

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc