forked from az64/mm-rando
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hue.cs
102 lines (90 loc) · 3.41 KB
/
Hue.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
using System;
using System.Drawing;
namespace MMRando
{
public partial class ROMFuncs
{
// reference: https://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors
/// <summary>
/// Creates a Color from alpha, hue, saturation and brightness.
/// </summary>
/// <param name="alpha">The alpha channel value.</param>
/// <param name="hue">The hue value.</param>
/// <param name="saturation">The saturation value.</param>
/// <param name="brightness">The brightness value.</param>
/// <returns>A Color with the given values.</returns>
public static Color FromAHSB(int alpha, float hue, float saturation, float brightness)
{
if (0 > alpha || 255 < alpha)
{
throw new ArgumentOutOfRangeException("alpha", alpha,
"Value must be within a range of 0 - 255.");
}
if (0f > hue || 360f < hue)
{
throw new ArgumentOutOfRangeException("hue", hue,
"Value must be within a range of 0 - 360.");
}
if (0f > saturation || 1f < saturation)
{
throw new ArgumentOutOfRangeException("saturation", saturation,
"Value must be within a range of 0 - 1.");
}
if (0f > brightness || 1f < brightness)
{
throw new ArgumentOutOfRangeException("brightness", brightness,
"Value must be within a range of 0 - 1.");
}
if (0 == saturation)
{
return Color.FromArgb(alpha, Convert.ToInt32(brightness * 255),
Convert.ToInt32(brightness * 255), Convert.ToInt32(brightness * 255));
}
float fMax, fMid, fMin;
int iSextant, iMax, iMid, iMin;
if (0.5 < brightness)
{
fMax = brightness - (brightness * saturation) + saturation;
fMin = brightness + (brightness * saturation) - saturation;
}
else
{
fMax = brightness + (brightness * saturation);
fMin = brightness - (brightness * saturation);
}
iSextant = (int)Math.Floor(hue / 60f);
if (300f <= hue)
{
hue -= 360f;
}
hue /= 60f;
hue -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f);
if (0 == iSextant % 2)
{
fMid = hue * (fMax - fMin) + fMin;
}
else
{
fMid = fMin - hue * (fMax - fMin);
}
iMax = Convert.ToInt32(fMax * 255);
iMid = Convert.ToInt32(fMid * 255);
iMin = Convert.ToInt32(fMin * 255);
switch (iSextant)
{
case 1:
return Color.FromArgb(alpha, iMid, iMax, iMin);
case 2:
return Color.FromArgb(alpha, iMin, iMax, iMid);
case 3:
return Color.FromArgb(alpha, iMin, iMid, iMax);
case 4:
return Color.FromArgb(alpha, iMid, iMin, iMax);
case 5:
return Color.FromArgb(alpha, iMax, iMin, iMid);
default:
return Color.FromArgb(alpha, iMax, iMid, iMin);
}
}
}
}