广丰卷烟厂数采质量分析系统
zhuguifei
2026-04-03 08114b7451615854cb01556a7c477e32e17f520e
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { getColorName, getDeltaE, getHsl, isValidColor, transformHslToHex } from '../shared';
import { colorPalettes } from '../constant';
import type {
  ColorPalette,
  ColorPaletteFamily,
  ColorPaletteFamilyWithNearestPalette,
  ColorPaletteMatch,
  ColorPaletteNumber
} from '../types';
 
/**
 * get recommended color palette by provided color
 *
 * @param color the provided color
 */
export function getRecommendedColorPalette(color: string) {
  const colorPaletteFamily = getRecommendedColorPaletteFamily(color);
 
  const colorMap = new Map<ColorPaletteNumber, ColorPalette>();
 
  colorPaletteFamily.palettes.forEach(palette => {
    colorMap.set(palette.number, palette);
  });
 
  const mainColor = colorMap.get(500)!;
  const matchColor = colorPaletteFamily.palettes.find(palette => palette.hex === color)!;
 
  const colorPalette: ColorPaletteMatch = {
    ...colorPaletteFamily,
    colorMap,
    main: mainColor,
    match: matchColor
  };
 
  return colorPalette;
}
 
/**
 * get recommended palette color by provided color
 *
 * @param color the provided color
 * @param number the color palette number
 */
export function getRecommendedPaletteColorByNumber(color: string, number: ColorPaletteNumber) {
  const colorPalette = getRecommendedColorPalette(color);
 
  const { hex } = colorPalette.colorMap.get(number)!;
 
  return hex;
}
 
/**
 * get color palette family by provided color and color name
 *
 * @param color the provided color
 */
export function getRecommendedColorPaletteFamily(color: string) {
  if (!isValidColor(color)) {
    throw new Error('Invalid color, please check color value!');
  }
 
  let colorName = getColorName(color);
 
  colorName = colorName.toLowerCase().replace(/\s/g, '-');
 
  const { h: h1, s: s1 } = getHsl(color);
 
  const { nearestLightnessPalette, palettes } = getNearestColorPaletteFamily(color, colorPalettes);
 
  const { number, hex } = nearestLightnessPalette;
 
  const { h: h2, s: s2 } = getHsl(hex);
 
  const deltaH = h1 - h2;
 
  const sRatio = s1 / s2;
 
  const colorPaletteFamily: ColorPaletteFamily = {
    name: colorName,
    palettes: palettes.map(palette => {
      let hexValue = color;
 
      const isSame = number === palette.number;
 
      if (!isSame) {
        const { h: h3, s: s3, l } = getHsl(palette.hex);
 
        const newH = deltaH < 0 ? h3 + deltaH : h3 - deltaH;
        const newS = s3 * sRatio;
 
        hexValue = transformHslToHex({
          h: newH,
          s: newS,
          l
        });
      }
 
      return {
        hex: hexValue,
        number: palette.number
      };
    })
  };
 
  return colorPaletteFamily;
}
 
/**
 * get nearest color palette family
 *
 * @param color color
 * @param families color palette families
 */
function getNearestColorPaletteFamily(color: string, families: ColorPaletteFamily[]) {
  const familyWithConfig = families.map(family => {
    const palettes = family.palettes.map(palette => {
      return {
        ...palette,
        delta: getDeltaE(color, palette.hex)
      };
    });
 
    const nearestPalette = palettes.reduce((prev, curr) => (prev.delta < curr.delta ? prev : curr));
 
    return {
      ...family,
      palettes,
      nearestPalette
    };
  });
 
  const nearestPaletteFamily = familyWithConfig.reduce((prev, curr) =>
    prev.nearestPalette.delta < curr.nearestPalette.delta ? prev : curr
  );
 
  const { l } = getHsl(color);
 
  const paletteFamily: ColorPaletteFamilyWithNearestPalette = {
    ...nearestPaletteFamily,
    nearestLightnessPalette: nearestPaletteFamily.palettes.reduce((prev, curr) => {
      const { l: prevLightness } = getHsl(prev.hex);
      const { l: currLightness } = getHsl(curr.hex);
 
      const deltaPrev = Math.abs(prevLightness - l);
      const deltaCurr = Math.abs(currLightness - l);
 
      return deltaPrev < deltaCurr ? prev : curr;
    })
  };
 
  return paletteFamily;
}