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
1 lines • 1.39 kB
JavaScript
import {cleanDefinition,splitValues,colorValueFallbacks,convertToInt8,normalizeDegrees}from'./common.js';function d(n,t="Invalid HSL color"){return console.warn(t),[n[0]??0,n[1]??0,n[2]]}function v(n){const t=cleanDefinition(n);let e=splitValues(t);return e.length!==3&&e.length!==4&&(e=d(e)),[e[0],e[1],e[2]]}const p=n=>`Invalid angle: ${n} - The none keyword is invalid in legacy color syntax `;function h(n){return {h:colorValueFallbacks(n[0],p(n[0]))||Math.round(normalizeDegrees(n[0]))||0,s:colorValueFallbacks(n[1])||convertToInt8(n[1],100)||0,l:colorValueFallbacks(n[2])||convertToInt8(n[2],100)||0}}function L(n,t,e){return e<1?[n,t,0]:e<2?[t,n,0]:e<3?[0,n,t]:e<4?[0,t,n]:e<5?[t,0,n]:[n,0,t]}function E(n){const t=h(n),e=t.s/100,l=t.l/100,i=(1-Math.abs(2*l-1))*e,s=i*(1-Math.abs(t.h/60%2-1)),r=l-i/2;let[u,o,a]=L(i,s,t.h/60%6);return u=Math.round((u+r)*255),o=Math.round((o+r)*255),a=Math.round((a+r)*255),{r:u,g:o,b:a}}function y({r:n,g:t,b:e}){n/=255,t/=255,e/=255;const l=Math.min(n,t,e),i=Math.max(n,t,e),s=i-l;let r=0,u=0,o=0;return s===0?r=0:i===n?r=(t-e)/s%6:i===t?r=(e-n)/s+2:r=(n-t)/s+4,r=Math.round(r*60),r<0&&(r+=360),o=(i+l)/2,u=s===0?0:s/(1-Math.abs(2*o-1)),u=+(u*100).toFixed(1),o=+(o*100).toFixed(1),M({h:r,s:u,l:o})}function M(n){return `hsl(${n.h},${n.s}%,${n.l}%)`}export{M as HSL,d as fallbackHSL,h as getHslValues,E as hslToRgb,v as parseHsl,y as valuesToHsl};