广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-04 dbfd4bc96205dd957827ee16c1149058fc2b88bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { colorNames } from '../constant';
import { getHex, getHsl, getRgb } from './colord';
 
/**
 * Get color name
 *
 * @param color
 */
export function getColorName(color: string) {
  const hex = getHex(color);
  const rgb = getRgb(color);
  const hsl = getHsl(color);
 
  let ndf = 0;
  let ndf1 = 0;
  let ndf2 = 0;
  let cl = -1;
  let df = -1;
 
  let name = '';
 
  colorNames.some((item, index) => {
    const [hexValue, colorName] = item;
 
    const match = hex === hexValue;
 
    if (match) {
      name = colorName;
    } else {
      const { r, g, b } = getRgb(hexValue);
      const { h, s, l } = getHsl(hexValue);
 
      ndf1 = (rgb.r - r) ** 2 + (rgb.g - g) ** 2 + (rgb.b - b) ** 2;
      ndf2 = (hsl.h - h) ** 2 + (hsl.s - s) ** 2 + (hsl.l - l) ** 2;
 
      ndf = ndf1 + ndf2 * 2;
      if (df < 0 || df > ndf) {
        df = ndf;
        cl = index;
      }
    }
 
    return match;
  });
 
  name = colorNames[cl][1];
 
  return name;
}