forked from arduano/ImageToMidi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetColorID.cs
More file actions
335 lines (306 loc) · 12 KB
/
GetColorID.cs
File metadata and controls
335 lines (306 loc) · 12 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
namespace ImageToMidi
{
public enum ColorIdMethod
{
RGB,
HSV,
Lab,
CIEDE2000,
HSL
// 未来可扩展:Lab, CIEDE2000等
}
public static class GetColorID
{
// 定义调色板Lab缓存结构
public class PaletteLabCache
{
public IList<Color> Palette { get; }
public (double l, double a, double b)[] LabValues { get; }
public PaletteLabCache(IList<Color> palette)
{
Palette = palette;
LabValues = new (double l, double a, double b)[palette.Count];
for (int i = 0; i < palette.Count; i++)
{
RgbToLab(palette[i].R, palette[i].G, palette[i].B, out double l, out double a, out double b);
LabValues[i] = (l, a, b);
}
}
}
public static int FindColorID(ColorIdMethod method, int r, int g, int b, PaletteLabCache cache)
{
switch (method)
{
case ColorIdMethod.RGB:
return FindColorID_RGB(r, g, b, cache.Palette);
case ColorIdMethod.HSV:
return FindColorID_HSV(r, g, b, cache.Palette);
case ColorIdMethod.Lab:
return FindColorID_Lab(r, g, b, cache);
case ColorIdMethod.CIEDE2000:
return FindColorID_CIEDE2000(r, g, b, cache);
case ColorIdMethod.HSL:
return FindColorID_HSL(r, g, b, cache.Palette);
default:
return FindColorID_RGB(r, g, b, cache.Palette);
}
}
public static int FindColorID_RGB(int r, int g, int b, IList<Color> palette)
{
int minIdx = 0;
double minDist = double.MaxValue;
for (int i = 0; i < palette.Count; i++)
{
var c = palette[i];
int dr = r - c.R, dg = g - c.G, db = b - c.B;
double dist = dr * dr + dg * dg + db * db;
if (dist < minDist)
{
minDist = dist;
minIdx = i;
}
}
return minIdx;
}
// HSV分别指: Hue色相, Saturation饱和度, Value明度
public static int FindColorID_HSV(int r, int g, int b, IList<Color> palette)
{
RgbToHsv(r, g, b, out double h1, out double s1, out double v1);
int minIdx = 0;
double minScore = double.MaxValue;
for (int i = 0; i < palette.Count; i++)
{
var c = palette[i];
RgbToHsv(c.R, c.G, c.B, out double h2, out double s2, out double v2);
double score;
if (v1 < 0.22 || s1 < 0.22)
{
score = Math.Abs(v1 - v2) * 2.0 + Math.Abs(s1 - s2) * 1.0;
}
else
{
double dh = Math.Abs(h1 - h2);
if (dh > 180) dh = 360 - dh;
dh /= 180.0;
double ds = Math.Abs(s1 - s2);
double dv = Math.Abs(v1 - v2);
score = dh * 2.0 + ds * 0.5 + dv * 0.5;
}
if (score < minScore)
{
minScore = score;
minIdx = i;
}
}
return minIdx;
}
// Lab算法
public static int FindColorID_Lab(int r, int g, int b, PaletteLabCache cache)
{
RgbToLab(r, g, b, out double l1, out double a1, out double b1);
int minIdx = 0;
double minDist = double.MaxValue;
for (int i = 0; i < cache.LabValues.Length; i++)
{
var (l2, a2, b2) = cache.LabValues[i];
double dist = LabDistance(l1, a1, b1, l2, a2, b2);
if (dist < minDist)
{
minDist = dist;
minIdx = i;
}
}
return minIdx;
}
// CIEDE2000算法
public static int FindColorID_CIEDE2000(int r, int g, int b, PaletteLabCache cache)
{
RgbToLab(r, g, b, out double l1, out double a1, out double b1);
int minIdx = 0;
double minDist = double.MaxValue;
for (int i = 0; i < cache.LabValues.Length; i++)
{
var (l2, a2, b2) = cache.LabValues[i];
double dist = CIEDE2000(l1, a1, b1, l2, a2, b2);
if (dist < minDist)
{
minDist = dist;
minIdx = i;
}
}
return minIdx;
}
// RGB转HSL
public static int FindColorID_HSL(int r, int g, int b, IList<Color> palette)
{
RgbToHsl(r, g, b, out double h1, out double s1, out double l1);
int minIdx = 0;
double minScore = double.MaxValue;
for (int i = 0; i < palette.Count; i++)
{
var c = palette[i];
RgbToHsl(c.R, c.G, c.B, out double h2, out double s2, out double l2);
double score;
if (l1 < 0.22 || s1 < 0.22)
{
score = Math.Abs(l1 - l2) * 10.0;
}
else
{
double dh = Math.Abs(h1 - h2);
if (dh > 180) dh = 360 - dh;
dh /= 180.0;
double ds = Math.Abs(s1 - s2);
double dl = Math.Abs(l1 - l2);
score = dh * 2.0 + ds * 0.5 + dl * 0.5;
}
if (score < minScore)
{
minScore = score;
minIdx = i;
}
}
return minIdx;
}
// RGB转Lab
public static void RgbToLab(int r, int g, int b, out double l, out double a, out double lab_b)
{
// 1. RGB转XYZ
double R = r / 255.0, G = g / 255.0, B = b / 255.0;
R = R > 0.04045 ? Math.Pow((R + 0.055) / 1.055, 2.4) : R / 12.92;
G = G > 0.04045 ? Math.Pow((G + 0.055) / 1.055, 2.4) : G / 12.92;
B = B > 0.04045 ? Math.Pow((B + 0.055) / 1.055, 2.4) : B / 12.92;
double X = (R * 0.4124 + G * 0.3576 + B * 0.1805) / 0.95047;
double Y = (R * 0.2126 + G * 0.7152 + B * 0.0722) / 1.00000;
double Z = (R * 0.0193 + G * 0.1192 + B * 0.9505) / 1.08883;
// 2. XYZ转Lab
Func<double, double> f = t => t > 0.008856 ? Math.Pow(t, 1.0 / 3.0) : (7.787 * t) + (16.0 / 116.0);
double fx = f(X);
double fy = f(Y);
double fz = f(Z);
l = 116.0 * fy - 16.0;
a = 500.0 * (fx - fy);
lab_b = 200.0 * (fy - fz);
}
// Lab欧氏距离
public static double LabDistance(double l1, double a1, double b1, double l2, double a2, double b2)
{
double dl = l1 - l2;
double da = a1 - a2;
double db = b1 - b2;
return Math.Sqrt(dl * dl + da * da + db * db);
}
// CIEDE2000色差
// 参考: https://zh.wikipedia.org/wiki/%E9%A2%9C%E8%89%B2%E5%B7%AE%E5%BC%82
public static double CIEDE2000(double l1, double a1, double b1, double l2, double a2, double b2)
{
// 步骤略长,直接实现标准公式
double avgLp = (l1 + l2) / 2.0;
double c1 = Math.Sqrt(a1 * a1 + b1 * b1);
double c2 = Math.Sqrt(a2 * a2 + b2 * b2);
double avgC = (c1 + c2) / 2.0;
double G = 0.5 * (1 - Math.Sqrt(Math.Pow(avgC, 7) / (Math.Pow(avgC, 7) + Math.Pow(25.0, 7))));
double a1p = (1 + G) * a1;
double a2p = (1 + G) * a2;
double c1p = Math.Sqrt(a1p * a1p + b1 * b1);
double c2p = Math.Sqrt(a2p * a2p + b2 * b2);
double h1p = Math.Atan2(b1, a1p);
if (h1p < 0) h1p += 2 * Math.PI;
double h2p = Math.Atan2(b2, a2p);
if (h2p < 0) h2p += 2 * Math.PI;
double dLp = l2 - l1;
double dCp = c2p - c1p;
double dhp;
if (c1p * c2p == 0)
dhp = 0;
else
{
double dh = h2p - h1p;
if (dh > Math.PI) dh -= 2 * Math.PI;
if (dh < -Math.PI) dh += 2 * Math.PI;
dhp = 2 * Math.Sqrt(c1p * c2p) * Math.Sin(dh / 2.0);
}
double avgLp_ = (l1 + l2) / 2.0;
double avgCp = (c1p + c2p) / 2.0;
double hSum = h1p + h2p;
double avgHp;
if (c1p * c2p == 0)
avgHp = hSum;
else
{
double dh = Math.Abs(h1p - h2p);
if (dh > Math.PI)
avgHp = (hSum + 2 * Math.PI) / 2.0;
else
avgHp = hSum / 2.0;
}
double T = 1
- 0.17 * Math.Cos(avgHp - Math.PI / 6)
+ 0.24 * Math.Cos(2 * avgHp)
+ 0.32 * Math.Cos(3 * avgHp + Math.PI / 30)
- 0.20 * Math.Cos(4 * avgHp - 21 * Math.PI / 60);
double dTheta = 30 * Math.PI / 180 * Math.Exp(-Math.Pow((avgHp * 180 / Math.PI - 275) / 25, 2));
double Rc = 2 * Math.Sqrt(Math.Pow(avgCp, 7) / (Math.Pow(avgCp, 7) + Math.Pow(25.0, 7)));
double Sl = 1 + ((0.015 * Math.Pow(avgLp_ - 50, 2)) / Math.Sqrt(20 + Math.Pow(avgLp_ - 50, 2)));
double Sc = 1 + 0.045 * avgCp;
double Sh = 1 + 0.015 * avgCp * T;
double Rt = -Math.Sin(2 * dTheta) * Rc;
double dE = Math.Sqrt(
Math.Pow(dLp / Sl, 2) +
Math.Pow(dCp / Sc, 2) +
Math.Pow(dhp / Sh, 2) +
Rt * (dCp / Sc) * (dhp / Sh)
);
return dE;
}
// HSV工具
public static void RgbToHsv(int r, int g, int b, out double h, out double s, out double v)
{
double R = r / 255.0, G = g / 255.0, B = b / 255.0;
double max = Math.Max(R, Math.Max(G, B));
double min = Math.Min(R, Math.Min(G, B));
v = max;
double delta = max - min;
s = max == 0 ? 0 : delta / max;
if (delta == 0)
h = 0;
else if (max == R)
h = 60 * (((G - B) / delta) % 6);
else if (max == G)
h = 60 * (((B - R) / delta) + 2);
else
h = 60 * (((R - G) / delta) + 4);
if (h < 0) h += 360;
}
// RGB转HSL
public static void RgbToHsl(int r, int g, int b, out double h, out double s, out double l)
{
double R = r / 255.0, G = g / 255.0, B = b / 255.0;
double max = Math.Max(R, Math.Max(G, B));
double min = Math.Min(R, Math.Min(G, B));
l = (max + min) / 2.0;
if (max == min)
{
h = 0;
s = 0;
}
else
{
double d = max - min;
s = l > 0.5 ? d / (2.0 - max - min) : d / (max + min);
if (max == R)
h = 60 * (((G - B) / d) % 6);
else if (max == G)
h = 60 * (((B - R) / d) + 2);
else
h = 60 * (((R - G) / d) + 4);
if (h < 0) h += 360;
}
}
}
}