forked from iskiselev/JSIL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLColor.cs
More file actions
69 lines (60 loc) · 2.44 KB
/
Copy pathHTMLColor.cs
File metadata and controls
69 lines (60 loc) · 2.44 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
// Used when generating barrier XML
using System;
namespace JSIL.Internal {
public static class HTMLColor {
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 string FromRGB (int r, int g, int b) {
return String.Format(
"#{0:x2}{1:x2}{2:x2}",
r, g, b
);
}
public static string FromHSV (UInt16 hue, UInt16 saturation, UInt16 value) {
if (value <= ValueMin)
return FromRGB(0, 0, 0);
if ((value >= ValueMax) && (saturation <= SaturationMin))
return FromRGB(255, 255, 255);
if (value > ValueMax)
value = ValueMax;
if (saturation > SaturationMax)
saturation = SaturationMax;
int range = value * 255 / ValueMax;
if (saturation <= SaturationMin)
return FromRGB(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 FromRGB(ClampByte(a), ClampByte(b), ClampByte(c));
case 1:
return FromRGB(ClampByte(rb), ClampByte(a), ClampByte(c));
case 2:
return FromRGB(ClampByte(c), ClampByte(a), ClampByte(b));
case 3:
return FromRGB(ClampByte(c), ClampByte(rb), ClampByte(a));
case 4:
return FromRGB(ClampByte(b), ClampByte(c), ClampByte(a));
case 5:
return FromRGB(ClampByte(a), ClampByte(c), ClampByte(rb));
default:
throw new ArgumentException("Invalid color");
}
}
}
}