UNPKG

color-classifier

Version:

Classify the color along the reference color. using algorithm the CIEDE2000, RGB, HSV.

9 lines 7.61 kB
/*! * color-classifier * Classify the color along the reference color. * * @author tsuyoshiwada * @license MIT * @version 0.0.1 */ !function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):r.ColorClassifier=e()}(this,function(){"use strict";function r(r,e){var t=[].concat(h.toConsumableArray(r));return t.sort(function(r,t){return r[e]<t[e]?-1:r[e]>t[e]?1:0}),t.shift()}function e(r,e){if(!e)return r;var t={};for(var n in r)t[n]=Math.round(r[n]);return t}function t(r,e){return r.hasOwnProperty(e)}function n(r){return t(r,"r")&&t(r,"g")&&t(r,"b")}function o(r){return r*Math.PI/180}function a(r){return r*(180/Math.PI)}function i(r,e){return Math.sqrt(r*r+e*e)}function f(r){return r*r}function u(r){return r*r*r*r*r*r*r}function l(r,e){if(r===e)return!0;var t=void 0,n=void 0;try{t=Object.keys(r),n=Object.keys(e)}catch(o){return!1}if(t.length!==n.length)return!1;for(var a=0;t.length-1;a++){var i=t[a];if(r[i]!==e[i])return!1}return("undefined"==typeof r?"undefined":h["typeof"](r))===("undefined"==typeof e?"undefined":h["typeof"](e))}var h={};h["typeof"]="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol?"symbol":typeof r},h.classCallCheck=function(r,e){if(!(r instanceof e))throw new TypeError("Cannot call a class as a function")},h.createClass=function(){function r(r,e){for(var t=0;t<e.length;t++){var n=e[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(r,n.key,n)}}return function(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}}(),h.defineProperty=function(r,e,t){return e in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r},h.slicedToArray=function(){function r(r,e){var t=[],n=!0,o=!1,a=void 0;try{for(var i,f=r[Symbol.iterator]();!(n=(i=f.next()).done)&&(t.push(i.value),!e||t.length!==e);n=!0);}catch(u){o=!0,a=u}finally{try{!n&&f["return"]&&f["return"]()}finally{if(o)throw a}}return t}return function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return r(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),h.toConsumableArray=function(r){if(Array.isArray(r)){for(var e=0,t=Array(r.length);e<r.length;e++)t[e]=r[e];return t}return Array.from(r)};var c,s=/^#([a-fA-F0-9]{3})$/,y=/^#([a-fA-F0-9]{6})$/,v=function(){function r(e){h.classCallCheck(this,r),this.original=e,n(e)?(this.rgb=e,this.hex=r.rgbToHex(e)):(this.hex=r.normalizeHex(e),this.rgb=r.hexToRgb(this.hex)),this.hsv=r.rgbToHsv(this.rgb)}return h.createClass(r,null,[{key:"normalizeHex",value:function(r){if(y.test(r))return r;if(s.test(r)){var e=r.slice(1,2),t=r.slice(2,3),n=r.slice(3,4);return"#"+(e+e)+(t+t)+(n+n)}return null}},{key:"hexToRgb",value:function(r){var e=this.normalizeHex(r);if(null==e)return null;var t=e.match(y),n=parseInt(t[1],16),o=n>>16&255,a=n>>8&255,i=255&n;return{r:o,g:a,b:i}}},{key:"rgbToHex",value:function(r){var e=r.r,t=r.g,n=r.b,o=((255&Math.round(e))<<16)+((255&Math.round(t))<<8)+(255&Math.round(n)),a=o.toString(16).toLowerCase();return"#"+("000000".substring(a.length)+a)}},{key:"rgbToHsv",value:function(r){var t=arguments.length<=1||void 0===arguments[1]?!0:arguments[1],n=r.r,o=r.g,a=r.b,i=Math.min(n,o,a),f=Math.max(n,o,a),u=f-i,l={};return 0===f?l.s=0:l.s=u/f*1e3/10,f===i?l.h=0:n===f?l.h=(o-a)/u:o===f?l.h=2+(a-n)/u:l.h=4+(n-o)/u,l.h=Math.min(60*l.h,360),l.h=l.h<0?l.h+360:l.h,l.v=f/255*1e3/10,e(l,t)}},{key:"rgbToXyz",value:function(r){var t=arguments.length<=1||void 0===arguments[1]?!0:arguments[1],n=r.r/255,o=r.g/255,a=r.b/255,i=n>.04045?Math.pow((n+.055)/1.055,2.4):n/12.92,f=o>.04045?Math.pow((o+.055)/1.055,2.4):o/12.92,u=a>.04045?Math.pow((a+.055)/1.055,2.4):a/12.92,l=100*(.4124*i+.3576*f+.1805*u),h=100*(.2126*i+.7152*f+.0722*u),c=100*(.0193*i+.1192*f+.9505*u);return e({x:l,y:h,z:c},t)}},{key:"rgbToLab",value:function(t){var n=arguments.length<=1||void 0===arguments[1]?!0:arguments[1],o=r.rgbToXyz(t,!1),a=o.x,i=o.y,f=o.z;a/=95.047,i/=100,f/=108.883,a=a>.008856?Math.pow(a,1/3):7.787*a+16/116,i=i>.008856?Math.pow(i,1/3):7.787*i+16/116,f=f>.008856?Math.pow(f,1/3):7.787*f+16/116;var u=116*i-16,l=500*(a-i),h=200*(i-f);return e({l:u,a:l,b:h},n)}}]),r}(),b=Math.abs,g=Math.atan2,p=Math.cos,d=Math.min,m=Math.exp,w=Math.sqrt,C=Math.sin,k={CIEDE2000:"CIEDE2000",HSV:"HSV",RGB:"RGB"},M=(c={},h.defineProperty(c,k.CIEDE2000,"ciede2000"),h.defineProperty(c,k.HSV,"hsv"),h.defineProperty(c,k.RGB,"rgb"),c),T=1,A=1,x=1,E=u(25),P=function(){function r(){h.classCallCheck(this,r)}return h.createClass(r,null,[{key:"diff",value:function(e,t,n){var o=M[e];return r[o](t,n)}},{key:"ciede2000",value:function(e,t){var n=v.rgbToLab(e.rgb),o=v.rgbToLab(t.rgb);return r._ciede2000(n.l,n.a,n.b,o.l,o.a,o.b)}},{key:"_ciede2000",value:function(r,e,t,n,l,h){var c=i(e,t),s=i(l,h),y=(c+s)/2,v=.5*(1-w(u(y)/(u(y)+E))),d=(1+v)*e,k=(1+v)*l,M=w(f(d)+f(t)),P=w(f(k)+f(h)),H=a(g(t,d)),S=0===t&&0===d?0:H>0?H:H+360,I=a(g(h,k)),O=0===h&&0===k?0:I>0?I:I+360,z=n-r,j=P-M,R=2*w(M*P)*C(o(c*s===0?0:b(O-S)<=180?O-S:O-S>180?O-S-360:O-S+360)/2),B=(r+n)/2,D=(M+P)/2,L=void 0;L=c*s===0?S+O:b(S-O)<=180?(S+O)/2:b(S-O)>180&&360>S+O?(S+O+360)/2:(S+O-360)/2;var F=1-.17*p(o(L-30))+.24*p(o(2*L))+.32*p(o(3*L+6))-.2*p(o(4*L-63)),G=30*m(-f((L-275)/25)),V=w(u(D)/(u(D)+E)),q=1+.015*f(B-50)/w(20+f(B-50)),W=1+.045*D,X=1+.015*D*F,$=-2*V*C(o(2*G));return w(f(z/(q*T))+f(j/(W*A))+f(R/(X*x))+$*(j/(W*A))*(R/(X*x)))}},{key:"hsv",value:function(r,e){var t=r.hsv,n=e.hsv,o=0;return o=t.h>n.h?d(t.h-n.h,n.h-t.h+360):d(n.h-t.h,t.h-n.h+360),w(f(o)+f(t.s-n.s)+f(t.v-n.v))}},{key:"rgb",value:function(r,e){var t=r.rgb,n=e.rgb;return w(f(t.r-n.r)+f(t.g-n.g)+f(t.b-n.b))}}]),r}(),H=["#000000","#808080","#c0c0c0","#ffffff","#800000","#ff0000","#008000","#00ff00","#808000","#ffff00","#008080","#00ffff","#000080","#0000ff","#800080","#ff00ff"],S=["#000000","#808080","#ffffff","#ff0000","#ffa500","#ffff00","#008000","#00ffff","#0000ff","#800080"],I=Object.freeze({W3C:H,RAINBOW:S}),O=function(){function e(){var r=arguments.length<=0||void 0===arguments[0]?H:arguments[0],t=arguments.length<=1||void 0===arguments[1]?k.CIEDE2000:arguments[1];h.classCallCheck(this,e),this.setPalette(r),this.setAlgorithmType(t)}return h.createClass(e,null,[{key:"throwError",value:function(r){throw new Error("[ColorClassifier] "+r)}}]),h.createClass(e,[{key:"setPalette",value:function(r){Array.isArray(r)||e.throwError("palette is should be a Array."),this.palette=r.map(function(r){return new v(r)})}},{key:"getPalette",value:function(){return this.palette}},{key:"setAlgorithmType",value:function(r){k.hasOwnProperty(r)||e.throwError(r+" is an undefined algorithm type."),this.algorithmType=r}},{key:"getAlgorithmType",value:function(){return this.algorithmType}},{key:"classify",value:function(e){var t=arguments.length<=1||void 0===arguments[1]?"rgb":arguments[1],n=this.palette,o=this.algorithmType,a=new v(e),i=[];return n.forEach(function(r){i.push({distance:P.diff(o,r,a),color:"raw"===t?r:r[t]})}),r(i,"distance").color}},{key:"classifyFromArray",value:function(r){var e=this,t=arguments.length<=1||void 0===arguments[1]?"rgb":arguments[1],n=[],o=[];return r.forEach(function(r){var t=new v(r),n=e.classify(t.rgb,"raw");o.push({palette:n,color:t})}),o.forEach(function(r){var e=r.palette,o=r.color,a=n.filter(function(r){return l(r.palette,e[t])}),i=h.slicedToArray(a,1),f=i[0];f?f.colors.push(o[t]):n.push({palette:e[t],colors:[o[t]]})}),n}}]),e}();return O.Palette=I,O.AlgorithmTypes=k,O});