color-2-name
Version:
Finds the closest color name to a given hex, rgb and hsl color (with and without alpha). It uses the Euclidean distance formula to calculate the distance between colors in the RGB color space
2 lines (1 loc) • 9.23 kB
JavaScript
;var color2name=(()=>{var y=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var W=(e,r)=>{for(var n in r)y(e,n,{get:r[n],enumerable:!0})},X=(e,r,n,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of P(r))!j.call(e,o)&&o!==n&&y(e,o,{get:()=>r[o],enumerable:!(t=q(r,o))||t.enumerable});return e};var z=e=>X(y({},"__esModule",{value:!0}),e);var ce={};W(ce,{closest:()=>x,closestRGB:()=>le,colorParsers:()=>A,distance:()=>U,getColor:()=>T,getColors:()=>M,isDark:()=>ae,isLight:()=>se,parseColor:()=>S,rgbToHex:()=>ue});var K=[[255,255,255,"white"],[0,0,0,"black"],[255,0,0,"red"],[0,128,0,"green"],[0,0,255,"blue"],[255,165,0,"orange"],[128,128,128,"grey"],[255,255,0,"yellow"],[255,0,255,"magenta"],[154,205,50,"yellowgreen"],[192,192,192,"silver"],[0,255,0,"lime"],[128,0,128,"purple"],[255,99,71,"tomato"],[64,224,208,"turquoise"],[255,127,80,"coral"],[0,255,255,"cyan"],[255,250,240,"floralwhite"],[255,192,203,"pink"],[34,139,34,"forestgreen"],[245,245,220,"beige"],[255,0,255,"fuchsia"],[220,220,220,"gainsboro"],[248,248,255,"ghostwhite"],[255,215,0,"gold"],[218,165,32,"goldenrod"],[173,255,47,"greenyellow"],[238,130,238,"violet"],[245,222,179,"wheat"],[245,245,245,"whitesmoke"],[139,0,0,"darkred"],[240,248,255,"aliceblue"],[205,92,92,"indianred"],[75,0,130,"indigo"],[250,235,215,"antiquewhite"],[0,255,255,"aqua"],[127,255,212,"aquamarine"],[240,255,255,"azure"],[255,228,196,"bisque"],[255,235,205,"blanchedalmond"],[138,43,226,"blueviolet"],[165,42,42,"brown"],[222,184,135,"burlywood"],[95,158,160,"cadetblue"],[127,255,0,"chartreuse"],[210,105,30,"chocolate"],[100,149,237,"cornflowerblue"],[255,248,220,"cornsilk"],[220,20,60,"crimson"],[0,0,139,"darkblue"],[0,139,139,"darkcyan"],[184,134,11,"darkgoldenrod"],[169,169,169,"darkgrey"],[0,100,0,"darkgreen"],[189,183,107,"darkkhaki"],[139,0,139,"darkmagenta"],[85,107,47,"darkolivegreen"],[255,140,0,"darkorange"],[153,50,204,"darkorchid"],[233,150,122,"darksalmon"],[143,188,143,"darkseagreen"],[72,61,139,"darkslateblue"],[47,79,79,"darkslategrey"],[0,206,209,"darkturquoise"],[148,0,211,"darkviolet"],[255,20,147,"deeppink"],[0,191,255,"deepskyblue"],[105,105,105,"dimgrey"],[30,144,255,"dodgerblue"],[178,34,34,"firebrick"],[240,255,240,"honeydew"],[255,105,180,"hotpink"],[255,255,240,"ivory"],[240,230,140,"khaki"],[230,230,250,"lavender"],[255,240,245,"lavenderblush"],[124,252,0,"lawngreen"],[255,250,205,"lemonchiffon"],[173,216,230,"lightblue"],[240,128,128,"lightcoral"],[224,255,255,"lightcyan"],[250,250,210,"lightgoldenrodyellow"],[144,238,144,"lightgreen"],[211,211,211,"lightgrey"],[255,182,193,"lightpink"],[255,160,122,"lightsalmon"],[32,178,170,"lightseagreen"],[135,206,250,"lightskyblue"],[119,136,153,"lightslategrey"],[176,196,222,"lightsteelblue"],[255,255,224,"lightyellow"],[50,205,50,"limegreen"],[250,240,230,"linen"],[128,0,0,"maroon"],[102,205,170,"mediumaquamarine"],[0,0,205,"mediumblue"],[186,85,211,"mediumorchid"],[147,112,219,"mediumpurple"],[60,179,113,"mediumseagreen"],[123,104,238,"mediumslateblue"],[0,250,154,"mediumspringgreen"],[72,209,204,"mediumturquoise"],[199,21,133,"mediumvioletred"],[25,25,112,"midnightblue"],[245,255,250,"mintcream"],[255,228,225,"mistyrose"],[255,228,181,"moccasin"],[255,222,173,"navajowhite"],[0,0,128,"navy"],[253,245,230,"oldlace"],[128,128,0,"olive"],[107,142,35,"olivedrab"],[255,69,0,"orangered"],[218,112,214,"orchid"],[238,232,170,"palegoldenrod"],[152,251,152,"palegreen"],[175,238,238,"paleturquoise"],[219,112,147,"palevioletred"],[255,239,213,"papayawhip"],[255,218,185,"peachpuff"],[205,133,63,"peru"],[221,160,221,"plum"],[176,224,230,"powderblue"],[102,51,153,"rebeccapurple"],[188,143,143,"rosybrown"],[65,105,225,"royalblue"],[139,69,19,"saddlebrown"],[250,128,114,"salmon"],[244,164,96,"sandybrown"],[46,139,87,"seagreen"],[255,245,238,"seashell"],[160,82,45,"sienna"],[135,206,235,"skyblue"],[106,90,205,"slateblue"],[112,128,144,"slategrey"],[255,250,250,"snow"],[0,255,127,"springgreen"],[70,130,180,"steelblue"],[210,180,140,"tan"],[0,128,128,"teal"],[216,191,216,"thistle"]],m=K;var $=/^#([\da-f]{3,8})/i,E=/^rgba?\(([^)]+)\)/i,F=/^hsla?\(([^)]+)\)/i,_=/^-?\d*\.?\d+$/i,J=/(\/\*[^*]*\*\/)|(\/\/.*)/g,O=[[255,255,255,"white"],[0,0,0,"black"]],H=[[255,0,0,"red"],[0,255,0,"green"],[0,0,255,"blue"]];function d(e){return e.split(e.includes(",")?",":" ").map(r=>r.trim())}function w(e){let r=Number.parseFloat(e)||0;for(e.indexOf("deg")>-1?r=Number.parseFloat(e.substring(0,e.length-3)):e.indexOf("rad")>-1?r=Math.round(Number.parseFloat(e.substring(0,e.length-3))*(180/Math.PI)):e.indexOf("turn")>-1&&(r=Math.round(Number.parseFloat(e.substring(0,e.length-4))*360));r<0;)r+=360;return r>=360&&(r%=360),r}function g(e,r=0,n=0){return Math.min(Math.max(Math.round(e),r),n)}function Q(e,r){let n=/calc\(([^)]+)\)/,t=e.match(n);return c(t?t[1]:e,r)}function h(e){let r=e.replace(J,""),n=r.indexOf("("),t=r.lastIndexOf(")");return r.slice(n+1,t).trim()}function Y(e,r){return Number.parseFloat(e)/100*r}function p(e,r){return e==="infinity"?(console.warn(r||`Positive infinity value has been set to 255: ${e}`),255):(e==="currentColor"&&console.warn(r||`The "currentColor" value has been set to 0: ${e}`),e==="transparent"&&console.warn(r||`The "transparent" value has been set to 0: ${e}`),e==="NaN"&&console.warn(r||`"NaN" value has been set to 0: ${e}`),e==="-infinity"&&console.warn(r||`"Negative" infinity value has been set to 0: ${e}`),e==="none"&&console.warn(r||`The none keyword is invalid in legacy color syntax: ${e}`),0)}function c(e,r=255){let n=typeof e=="string"?e?.trim():"0";return _.test(n)?g(Number.parseFloat(n)||0,0,r):n.endsWith("%")?Y(n,r)||0:n.endsWith("deg")||n.endsWith("rad")||n.endsWith("turn")?w(n):n.startsWith("calc")?g(Q(n,r),0,r):p(n,`Invalid value: ${e}`)}function L(e,r="Invalid RGB color"){return console.warn(r),[e[0]??0,e[1]??0,e[2]??0]}function k(e){let r=h(e),n=d(r);return n.length!==3&&n.length!==4?L(n,`Too few values to define rgb: ${e} -> ${r}`):[n[0],n[1],n[2]]}function C(e){return{r:g(Math.round(c(e[0])),0,255)||0,g:g(Math.round(c(e[1])),0,255)||0,b:g(Math.round(c(e[2])),0,255)||0}}function G(e){return`rgb(${e.r},${e.g},${e.b})`}function Z(e){return Array.from(e).map(r=>(r+r).toUpperCase())}function ee(e){return!!e.match(/^[0-9a-f]{3,8}$/i)}function I(e){let r=e.substring(1),n=[];return r&&(r.length===3||r.length===4?n=Z(r):(r.length===6||r.length===8)&&(n=(r.match(/../g)||[]).map(t=>t))),n.length?(n?.forEach((t,o)=>{ee(t)?n[o]=t.toUpperCase():console.warn(`Invalid Hex value: ${t}`)}),n):(console.warn(`Invalid Hex: ${e}`),L(n))}function V(e){return{r:Number.parseInt(e[0],16),g:Number.parseInt(e[1],16),b:Number.parseInt(e[2],16)}}function B(e){return e.toString(16).padStart(2,"0")}function f(e){return`#${B(e?.r)}${B(e?.g)}${B(e?.b)}`}function re(e,r="Invalid HSL color"){return console.warn(r),[e[0]??0,e[1]??0,e[2]]}function D(e){let r=h(e),n=d(r);return n.length!==3&&n.length!==4&&(n=re(n)),[n[0],n[1],n[2]]}var ne=e=>`Invalid angle: ${e} - The none keyword is invalid in legacy color syntax `;function te(e){return{h:p(e[0],ne(e[0]))||Math.round(w(e[0]))||0,s:p(e[1])||c(e[1],100)||0,l:p(e[2])||c(e[2],100)||0}}function oe(e,r,n){return n<1?[e,r,0]:n<2?[r,e,0]:n<3?[0,e,r]:n<4?[0,r,e]:n<5?[r,0,e]:[e,0,r]}function N(e){let r=te(e),n=r.s/100,t=r.l/100,o=(1-Math.abs(2*t-1))*n,i=o*(1-Math.abs(r.h/60%2-1)),s=t-o/2,[u,a,l]=oe(o,i,r.h/60%6);return u=Math.round((u+s)*255),a=Math.round((a+s)*255),l=Math.round((l+s)*255),{r:u,g:a,b:l}}function v({r:e,g:r,b:n}){e/=255,r/=255,n/=255;let t=Math.min(e,r,n),o=Math.max(e,r,n),i=o-t,s=0,u=0,a=0;return i===0?s=0:o===e?s=(r-n)/i%6:o===r?s=(n-e)/i+2:s=(e-r)/i+4,s=Math.round(s*60),s<0&&(s+=360),a=(o+t)/2,u=i===0?0:i/(1-Math.abs(2*a-1)),u=+(u*100).toFixed(1),a=+(a*100).toFixed(1),ie({h:s,s:u,l:a})}function ie(e){return`hsl(${e.h},${e.s}%,${e.l}%)`}function T(e,r=m){let n=r.find(t=>t[3]===e);if(typeof n<"u"){let[t,o,i]=n;return{hex:f({r:t,g:o,b:i}),rgb:G({r:t,g:o,b:i}),hsl:v({r:t,g:o,b:i})}}throw new Error(`Error: invalid color ${e} or empty colorSet`)}function M(){return m.map(e=>{let[r,n,t,o]=e,i={r,g:n,b:t};return{name:o,hex:f(i),rgb:G(i),hsl:v(i)}})}var A=[{regex:$,parser:I,converter:V},{regex:E,parser:k,converter:C},{regex:F,parser:D,converter:N}],b=new Map;function x(e,r=m,n){let t=`${e}-${n?.info?"info":"basic"}`;if(b.has(t))return b.get(t);let o=Number.MAX_SAFE_INTEGER,i={name:"error",color:"#F00"};if(r.length<1)return i;let s=S(e),u=r.length;for(let a=0;a<u;a++){let l=r[a],R=U([s.r,s.g,s.b],l,!0);if(R<o&&(o=R,i.name=l[3],i.color=`rgb(${l[0]},${l[1]},${l[2]})`),R===0)break}if(n?.info){let l={...T(i.name,r),...i,gap:Math.sqrt(o)};return b.set(t,l),l}return b.set(t,i),i}function S(e){for(let{regex:r,parser:n,converter:t}of A)if(r.test(e)){let o=n(e);return t(o)}throw new Error(`Invalid color: ${e}`)}function se(e){return x(e,O).name==="white"}function ae(e){return x(e,O).name==="black"}function le(e){return x(e,H).name}function U(e,r,n=!1){let t=r[0]-e[0],o=r[1]-e[1],i=r[2]-e[2],s=t*t+o*o+i*i;return n?s:Math.sqrt(s)}function ue(e){if(E.test(e)){let r=k(e),n=C(r);return f(n)}throw new Error(`Invalid color: ${e}`)}return z(ce);})();