-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorHandler.cs
241 lines (208 loc) · 6.94 KB
/
ColorHandler.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
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
// PascalsTriangle, by Ken Getz July 2003
//http://msdn.microsoft.com/en-us/magazine/cc164113.aspx
using System;
using System.Drawing;
namespace Pascals
{
public class ColorHandler
{
// Handle conversions between RGB and HSV
// (and Color types, as well).
public struct RGB
{
// All values are between 0 and 255.
public int Red;
public int Green;
public int Blue;
public RGB(int R, int G, int B)
{
Red = R;
Green = G;
Blue = B;
}
public override string ToString()
{
return String.Format("({0}, {1}, {2})", Red, Green, Blue);
}
}
public struct HSV
{
// All values are between 0 and 255.
public int Hue;
public int Saturation;
public int value;
public HSV(int H, int S, int V)
{
Hue = H;
Saturation = S;
value = V;
}
public override string ToString()
{
return String.Format("({0}, {1}, {2})", Hue, Saturation, value);
}
}
public static RGB HSVtoRGB(int H, int S, int V)
{
// H, S, and V must all be between 0 and 255.
return HSVtoRGB(new HSV(H, S, V));
}
public static Color HSVtoColor(HSV hsv)
{
RGB RGB = HSVtoRGB(hsv);
return Color.FromArgb(RGB.Red, RGB.Green, RGB.Blue);
}
public static Color HSVtoColor(int H, int S, int V)
{
return HSVtoColor(new HSV(H, S, V));
}
public static RGB HSVtoRGB(HSV HSV)
{
// HSV contains values scaled as in the color wheel:
// that is, all from 0 to 255.
// for ( this code to work, HSV.Hue needs
// to be scaled from 0 to 360 (it//s the angle of the selected
// point within the circle). HSV.Saturation and HSV.value must be
// scaled to be between 0 and 1.
double h;
double s;
double v;
double r = 0;
double g = 0;
double b = 0;
// Scale Hue to be between 0 and 360. Saturation
// and value scale to be between 0 and 1.
h = ((double)HSV.Hue / 255 * 360) % 360;
s = (double)HSV.Saturation / 255;
v = (double)HSV.value / 255;
if (s == 0)
{
// If s is 0, all colors are the same.
// This is some flavor of gray.
r = v;
g = v;
b = v;
}
else
{
double p;
double q;
double t;
double fractionalSector;
int sectorNumber;
double sectorPos;
// The color wheel consists of 6 sectors.
// Figure out which sector you//re in.
sectorPos = h / 60;
sectorNumber = (int)(Math.Floor(sectorPos));
// get the fractional part of the sector.
// That is, how many degrees into the sector
// are you?
fractionalSector = sectorPos - sectorNumber;
// Calculate values for the three axes
// of the color.
p = v * (1 - s);
q = v * (1 - (s * fractionalSector));
t = v * (1 - (s * (1 - fractionalSector)));
// Assign the fractional colors to r, g, and b
// based on the sector the angle is in.
switch (sectorNumber)
{
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
case 5:
r = v;
g = p;
b = q;
break;
}
}
// return an RGB structure, with values scaled
// to be between 0 and 255.
return new RGB((int)(r * 255), (int)(g * 255), (int)(b * 255));
}
public static HSV RGBtoHSV(RGB RGB)
{
// In this function, R, G, and B values must be scaled
// to be between 0 and 1.
// HSV.Hue will be a value between 0 and 360, and
// HSV.Saturation and value are between 0 and 1.
// The code must scale these to be between 0 and 255 for
// the purposes of this application.
double min;
double max;
double delta;
double r = (double)RGB.Red / 255;
double g = (double)RGB.Green / 255;
double b = (double)RGB.Blue / 255;
double h;
double s;
double v;
min = Math.Min(Math.Min(r, g), b);
max = Math.Max(Math.Max(r, g), b);
v = max;
delta = max - min;
if (max == 0 || delta == 0)
{
// R, G, and B must be 0, or all the same.
// In this case, S is 0, and H is undefined.
// Using H = 0 is as good as any...
s = 0;
h = 0;
}
else
{
s = delta / max;
if (r == max)
{
// Between Yellow and Magenta
h = (g - b) / delta;
}
else if (g == max)
{
// Between Cyan and Yellow
h = 2 + (b - r) / delta;
}
else
{
// Between Magenta and Cyan
h = 4 + (r - g) / delta;
}
}
// Scale h to be between 0 and 360.
// This may require adding 360, if the value
// is negative.
h *= 60;
if (h < 0)
{
h += 360;
}
// Scale to the requirements of this
// application. All values are between 0 and 255.
return new HSV((int)(h / 360 * 255), (int)(s * 255), (int)(v * 255));
}
}
}