-
Notifications
You must be signed in to change notification settings - Fork 4
/
HueSatLight.cs
292 lines (273 loc) · 6.01 KB
/
HueSatLight.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
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
using System;
using System.Windows.Media;
namespace StepDiagrammer
{
public class HueSatLight
{
#region private fields...
private double alpha = 1.0;
private double hue;
private double saturation;
private double lightness;
#endregion
// constructors...
#region HueSatLight
public HueSatLight()
{
hue = 0;
saturation = 0;
lightness = 0;
}
#endregion
#region HueSatLight(Color aColor)
public HueSatLight(Color color)
{
Hue = color.GetHue() / 360.0; // Convert from range of 0-360 to 0.0-1.0.
Lightness = color.GetBrightness();
Saturation = color.GetSaturation();
}
public HueSatLight(double hue, double saturation, double lightness)
{
this.hue = hue;
this.saturation = saturation;
this.lightness = lightness;
}
public HueSatLight(string htmlColorStr) : this((Color)ColorConverter.ConvertFromString(htmlColorStr))
{
}
#endregion
// public methods...
#region AsRGB
/// <summary>
/// Returns an equivalent Color instance.
/// </summary>
public Color AsRGB
{
get
{
double red = 0;
double green = 0;
double blue = 0;
if (Lightness == 0)
{
// Completely black.
red = green = blue = 0;
}
else
{
if (Saturation == 0)
{
// No color; somewhere on the gray scale.
red = green = blue = Lightness;
}
else
{
double temp2 = ((Lightness <= 0.5) ? Lightness * (1.0 + Saturation) : Lightness + Saturation - (Lightness * Saturation));
double temp1 = 2.0 * Lightness - temp2;
double[] hueShift = new double[] { Hue + 1.0 / 3.0, Hue, Hue - 1.0 / 3.0 };
double[] colorArray = new double[] { 0, 0, 0 };
for (int i = 0; i < 3; i++)
{
if (hueShift[i] < 0)
hueShift[i] += 1.0;
if (hueShift[i] > 1)
hueShift[i] -= 1.0;
if (6.0 * hueShift[i] < 1.0)
colorArray[i] = temp1 + (temp2 - temp1) * hueShift[i] * 6.0;
else if (2.0 * hueShift[i] < 1.0)
colorArray[i] = temp2;
else if (3.0 * hueShift[i] < 2.0)
colorArray[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - hueShift[i]) * 6.0);
else
colorArray[i] = temp1;
}
red = colorArray[0];
green = colorArray[1];
blue = colorArray[2];
}
}
return Color.FromArgb((byte)(alpha * 255.0), (byte)(255 * red), (byte)(255 * green), (byte)(255 * blue));
}
}
#endregion
#region AsGrayScale
/// <summary>
/// Returns an equivalent Color object converted to gray scale.
/// </summary>
public Color AsGrayScale
{
get
{
Color color = AsRGB;
//byte grayScaleLevel = (byte)(color.R * 0.299 + color.G * 0.587 + color.B * 0.114);
//byte grayScaleLevel = (byte)Math.Sqrt(0.299 * color.R * color.R + 0.587 * color.G * color.G + 0.114 * color.B * color.B); // http://alienryderflex.com/hsp.html
byte grayScaleLevel = (byte)Math.Sqrt(0.241 * color.R * color.R + 0.691 * color.G * color.G + 0.068 * color.B * color.B); // http://www.purplesquirrels.com.au/2012/03/as3-calculate-the-perceived-brightness-of-a-colour/ and http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
return Color.FromRgb(grayScaleLevel, grayScaleLevel, grayScaleLevel);
}
}
#endregion
#region GetRelativeLuminance
/// <summary>
/// Returns an the relative luminance (https://www.w3.org/TR/WCAG20/#relativeluminancedef) of this HueSatLight instance.
/// </summary>
public double GetRelativeLuminance()
{
Color color = AsRGB;
double RsRGB = color.R / 255.0;
double GsRGB = color.G / 255.0;
double BsRGB = color.B / 255.0;
double R;
double G;
double B;
if (RsRGB <= 0.03928)
R = RsRGB / 12.92;
else
R = Math.Pow(((RsRGB + 0.055) / 1.055), 2.4);
if (GsRGB <= 0.03928)
G = GsRGB / 12.92;
else
G = Math.Pow(((GsRGB + 0.055) / 1.055), 2.4);
if (BsRGB <= 0.03928)
B = BsRGB / 12.92;
else
B = Math.Pow(((BsRGB + 0.055) / 1.055), 2.4);
double luminance = 0.2126 * R + 0.7152 * G + 0.0722 * B;
return luminance;
}
#endregion
// public properties...
#region AsHtml
public string AsHtml
{
get
{
Color color = AsRGB;
return "#" + color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");
}
}
#endregion
#region IsLight
/// <summary>
/// Returns true if this color is brighter than medium gray.
/// </summary>
public bool IsLight
{
get
{
Color grayScale = AsGrayScale;
return (grayScale.R >= 128);
}
}
#endregion
#region IsDark
/// <summary>
/// Returns true if this color is darker than medium gray.
/// </summary>
public bool IsDark
{
get
{
Color grayScale = AsGrayScale;
return (grayScale.R < 128);
}
}
#endregion
#region IsGrayScale
/// <summary>
/// Returns true if saturation is at zero.
/// </summary>
public bool IsGrayScale
{
get
{
return saturation == 0.0;
}
}
#endregion
#region GrayScale
/// <summary>
/// Returns the percentage on the gray scale.
/// </summary>
public float GrayScale
{
get
{
Color color = AsGrayScale;
return color.R / 255.0f;
}
}
#endregion
#region Alpha
public double Alpha
{
get
{
return alpha;
}
set
{
if (value < 0)
alpha = 0;
else if (value > 1.0)
value = 1.0;
else
alpha = value;
}
}
#endregion
#region Hue
public double Hue
{
get
{
return hue;
}
set
{
if (value > 1)
hue = 1;
else if (value < 0)
hue = 0;
else
hue = value;
}
}
#endregion
#region Saturation
public double Saturation
{
get
{
return saturation;
}
set
{
if (value > 1)
saturation = 1;
else if (value < 0)
saturation = 0;
else
saturation = value;
}
}
#endregion
#region Lightness
public double Lightness
{
get
{
return lightness;
}
set
{
if (value > 1)
lightness = 1;
else if (value < 0)
lightness = 0;
else
lightness = value;
}
}
#endregion
}
}