UNPKG

ntc-ts

Version:

"Name That Color" as a typescript library!

13 lines (8 loc) 2.66 kB
(function () { 'use strict'; const e=[["000000","Black"],["0000FF","Blue"],["00FF00","Green"],["00FFFF","Cyan / Aqua"],["315BA1","Azure"],["370202","Chocolate"],["483C32","Taupe"],["4F69C6","Indigo"],["660099","Purple"],["706555","Coffee"],["888D65","Avocado"],["967BB6","Lavender Purple"],["A9C6C2","Opal"],["AF8F2C","Alpine"],["DA3287","Cerise"],["EB9373","Apricot"],["ED9121","Carrot Orange"],["F5F5DC","Beige"],["FDE910","Lemon"],["FF0000","Red"],["FF007F","Rose"],["FF00FF","Magenta / Fuchsia"],["FF681F","Orange"],["FF69B4","Hot Pink"],["FF7F50","Coral"],["FF8C69","Salmon"],["FFC0CB","Pink"],["FFDB58","Mustard"],["FFDDAF","Caramel"],["FFE5B4","Peach"],["FFFDD0","Cream"],["FFFF00","Yellow"],["FFFFF0","Ivory"],["FFFFFF","White"]];function F(){return F=Object.assign||function(e){for(var a=1;a<arguments.length;a++){var F=arguments[a];for(var r in F)Object.prototype.hasOwnProperty.call(F,r)&&(e[r]=F[r]);}return e},F.apply(this,arguments)}let r={},n=[["000000","Black"]];function i(e,a=1){return [parseInt(`0x${e.substring(1,3)}`,16)/a,parseInt(`0x${e.substring(3,5)}`,16)/a,parseInt(`0x${e.substring(5,7)}`,16)/a]}function C(e){const a=i(e,255),F=a[0],r=a[1],n=a[2],o=Math.min(F,Math.min(r,n)),C=Math.max(F,Math.max(r,n)),B=C-o;let l=0,D=0;const E=(o+C)/2;return E>0&&E<1&&(D=B/(E<.5?2*E:2-2*E)),B>0&&(C===F&&C!==r&&(l+=(r-n)/B),C===r&&C!==n&&(l+=2+(n-F)/B),C===n&&C!==F&&(l+=4+(F-r)/B),l/=6),[Math.round(255*l),Math.round(255*D),Math.round(255*E)]}function B(e,a,F=!1){return {exactMatch:F,name:a,rgb:e}}function l(e){n=[...e],D();}function D(){r=F({},{});}function E(e){if(null==e)return B(null,"not-a-color",!1);if((e=e.toUpperCase()).length<3||e.length>7)return B(null,"not-a-color",!1);if(e.length%3==0&&(e=`#${e}`),4===e.length&&(e=`#${e.substr(1,1)}${e.substr(1,1)}${e.substr(2,1)}${e.substr(2,1)}${e.substr(3,1)}${e.substr(3,1)}`),void 0!==r[e])return r[e];const a=i(e),F=a[0],o=a[1],l=a[2],D=C(e),E=D[0],t=D[1],A=D[2];let u=0,s=0,d=0,h=-1,c=-1;for(let a=0;a<n.length;a++){let D=n[a];const S=`#${D[0]}`,p=String(D[1]);if(void 0===D[2]){const e=i(S),a=C(S);D=D.concat(e),D=D.concat(a);}if(e===S)return r[e]=B(S,p,!0),r[e];const y=parseInt(String(D[2]),10),m=parseInt(String(D[3]),10),g=parseInt(String(D[4]),10),P=parseInt(String(D[5]),10),G=parseInt(String(D[6]),10),k=parseInt(String(D[7]),10);u=Math.pow(F-y,2)+Math.pow(o-m,2)+Math.pow(l-g,2),s=Math.pow(E-P,2)+Math.pow(t-G,2)+Math.pow(A-k,2),d=u+2*s,(c<0||c>d)&&(c=d,h=a);}if(h<0)return B(e,e,!1);const S=n[h],p=`#${S[0]}`,y=String(S[1]);return r[e]=B(p,y,!1),r[e]} l(e); const color = E('#F89EAB'); console.log('color for #F89EAB', color); })(); //# sourceMappingURL=bundle.js.map