-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColorNames.cs
211 lines (183 loc) · 6.06 KB
/
ColorNames.cs
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
using ColorNamesSharp.Tree;
using ColorNamesSharp.Utility;
namespace ColorNamesSharp;
public class ColorNames(List<NamedColor> namedColors)
{
public KDNode? ColorTreeRoot { get; } = KDTreeBuilder.Build(namedColors, 0);
private double MinDistance = double.PositiveInfinity;
private KDNode? BestNode = null;
/// <summary>
/// Gets a random color from the color list
/// </summary>
/// <returns>A NamedColor</returns>
public NamedColor? GetRandomNamedColor()
{
if (namedColors.Count == 0)
return null;
Random random = new();
int index = random.Next(namedColors.Count);
return namedColors[index];
}
/// <summary>
/// Sets the BestNode to the closest possible match of the given lab color values.
/// </summary>
/// <param name="node"></param>
/// <param name="l"></param>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="depth"></param>
private void SearchNearest(KDNode? node, float l, float a, float b, int depth)
{
if (node == null)
return;
int axis = depth % 3;
(float, float, float) nodeLab = node.Color.Lab;
float dim = axis switch
{
0 => l - nodeLab.Item1,
1 => a - nodeLab.Item2,
_ => b - nodeLab.Item3
};
double dist = Math.Sqrt(
(l - nodeLab.Item1) * (l - nodeLab.Item1) +
(a - nodeLab.Item2) * (a - nodeLab.Item2) +
(b - nodeLab.Item3) * (b - nodeLab.Item3));
if (dist < MinDistance)
{
MinDistance = dist;
BestNode = node;
}
KDNode? firstChild = dim <= 0 ? node.Left : node.Right;
KDNode? secondChild = dim <= 0 ? node.Right : node.Left;
SearchNearest(firstChild, l, a, b, depth + 1);
if (dim * dim < MinDistance)
SearchNearest(secondChild, l, a, b, depth + 1);
}
/// <summary>
/// Finds the colosest color for given RGB color values
/// </summary>
/// <param name="RGB">A touple with the RGB color values</param>
/// <returns>A NamedColor</returns>
public NamedColor? FindClosestColor((short, short, short) RGB)
{
MinDistance = double.PositiveInfinity;
BestNode = null;
(float l, float a, float b) = ColorConverter.RGBToLab(RGB);
SearchNearest(ColorTreeRoot, l, a, b, 0);
return BestNode?.Color;
}
/// <summary>
/// Finds the closest color for given RGB color values
/// </summary>
/// <param name="r"></param>
/// <param name="g"></param>
/// <param name="b"></param>
/// <returns></returns>
public NamedColor? FindClosestColor(short r, short g, short b)
{
return FindClosestColor((r, g, b));
}
/// <summary>
/// Finds the closest color for given hex color value
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
public NamedColor? FindClosestColor(string hex)
{
(short r, short g, short b) = ColorConverter.HexToRgb(hex);
return FindClosestColor(r, g, b);
}
/// <summary>
/// Finds the closest color for a given NamedColor
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public NamedColor? FindClosestColor(NamedColor color)
{
return FindClosestColor(color.Rgb);
}
/// <summary>
/// Finds the closest color for given lab color values
/// </summary>
/// <param name="l"></param>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public NamedColor? FindClosestColor(float l, float a, float b)
{
MinDistance = double.PositiveInfinity;
BestNode = null;
SearchNearest(ColorTreeRoot, l, a, b, 0);
return BestNode?.Color;
}
/// <summary>
/// Finds the closest color for given lab color values
/// </summary>
/// <param name="lab"></param>
/// <returns></returns>
public NamedColor? FindClosestColor((float, float, float) lab)
{
return FindClosestColor(lab.Item1, lab.Item2, lab.Item3);
}
/// <summary>
/// Finds the closest color name for given RGB color values
/// </summary>
/// <param name="RGB"></param>
/// <returns></returns>
public string FindClosestColorName((short, short, short) RGB)
{
NamedColor? color = FindClosestColor(RGB);
return color?.Name ?? "Unknown";
}
/// <summary>
/// Finds the closest color name for given RGB color values
/// </summary>
/// <param name="r"></param>
/// <param name="g"></param>
/// <param name="b"></param>
/// <returns></returns>
public string FindClosestColorName(short r, short g, short b)
{
return FindClosestColorName((r, g, b));
}
/// <summary>
/// Finds the closest color name for given hex color value
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
public string FindClosestColorName(string hex)
{
NamedColor? color = FindClosestColor(hex);
return color?.Name ?? "Unknown";
}
/// <summary>
/// Finds the closest color name for a given NamedColor
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public string FindClosestColorName(NamedColor color)
{
return FindClosestColorName(color.Rgb);
}
/// <summary>
/// Finds the closest color name for given lab color values
/// </summary>
/// <param name="l"></param>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public string FindClosestColorName(float l, float a, float b)
{
NamedColor? color = FindClosestColor(l, a, b);
return color?.Name ?? "Unknown";
}
/// <summary>
/// Finds the closest color name for given lab color values
/// </summary>
/// <param name="lab"></param>
/// <returns></returns>
public string FindClosestColorName((float, float, float) lab)
{
return FindClosestColorName(lab.Item1, lab.Item2, lab.Item3);
}
}