-
Notifications
You must be signed in to change notification settings - Fork 4
/
HSV.cs
125 lines (106 loc) · 4.63 KB
/
HSV.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
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is Fury².
The Initial Developer of the Original Code is K. Gadd.
Original Author: K. Gadd ([email protected])
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace HeapProfiler {
public static class HSV {
public const UInt16 HueUnit = 600;
public const UInt16 HueMin = 0, HueMax = (HueUnit * 6) - 1;
public const UInt16 SaturationMin = 0, SaturationMax = 2550;
public const UInt16 ValueMin = 0, ValueMax = 2550;
static byte ClampByte (int value) {
if (value < 0)
return 0;
else if (value > 255)
return 255;
else
return (byte)value;
}
public static Color ColorFromHSV (UInt16 hue, UInt16 saturation, UInt16 value) {
return ColorFromHSVA(hue, saturation, value, 255);
}
public static Color ColorFromHSVA (UInt16 hue, UInt16 saturation, UInt16 value, byte alpha) {
if (value <= ValueMin)
return Color.FromArgb(alpha, 0, 0, 0);
if ((value >= ValueMax) && (saturation <= SaturationMin))
return Color.FromArgb(alpha, 255, 255, 255);
if (value > ValueMax)
value = ValueMax;
if (saturation > SaturationMax)
saturation = SaturationMax;
int range = value * 255 / ValueMax;
if (saturation <= SaturationMin)
return Color.FromArgb(alpha, range, range, range);
int segment = hue / HueUnit;
int remainder = hue - (segment * HueUnit);
int colorRange = (saturation) * range / SaturationMax;
int c = (SaturationMax - saturation) * range / SaturationMax;
int b = (remainder * colorRange) / HueUnit + c;
int rb = colorRange - b + (c * 2);
int a = colorRange + c;
switch (segment) {
case 0:
return Color.FromArgb(alpha, ClampByte(a), ClampByte(b), ClampByte(c));
case 1:
return Color.FromArgb(alpha, ClampByte(rb), ClampByte(a), ClampByte(c));
case 2:
return Color.FromArgb(alpha, ClampByte(c), ClampByte(a), ClampByte(b));
case 3:
return Color.FromArgb(alpha, ClampByte(c), ClampByte(rb), ClampByte(a));
case 4:
return Color.FromArgb(alpha, ClampByte(b), ClampByte(c), ClampByte(a));
case 5:
return Color.FromArgb(alpha, ClampByte(a), ClampByte(c), ClampByte(rb));
default:
throw new ArgumentException("Invalid color");
}
}
public static void GetHSV (this Color color, out UInt16 hue, out UInt16 saturation, out UInt16 value) {
byte alpha;
GetHSVA(color, out hue, out saturation, out value, out alpha);
}
public static void GetHSVA (this Color color, out UInt16 hue, out UInt16 saturation, out UInt16 value, out byte alpha) {
alpha = color.A;
int min = Math.Min(color.R, Math.Min(color.G, color.B));
int max = Math.Max(color.R, Math.Max(color.G, color.B));
if (max == min) {
hue = HueMin;
saturation = SaturationMin;
value = (UInt16)(min * ValueMax / 255);
return;
} else if (max == 0) {
hue = HueMin;
saturation = SaturationMin;
value = ValueMin;
return;
}
int b = max - min, d;
value = (UInt16)(max * ValueMax / 255);
saturation = (UInt16)(b * SaturationMax / max);
if (color.R == max) {
d = color.G - color.B;
hue = (UInt16)(d * HueUnit / b);
} else if (color.G == max) {
d = color.B - color.R;
hue = (UInt16)((d * HueUnit / b) + (2 * HueUnit));
} else /* if (color.B == max) */ {
d = color.R - color.G;
hue = (UInt16)((d * HueUnit / b) + (4 * HueUnit));
}
}
}
}