-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHsvColor.cs
59 lines (51 loc) · 1.74 KB
/
HsvColor.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
using System;
using TaleWorlds.Library;
namespace Int19h.Bannerlord.PettyKingdoms {
internal struct HsvColor {
public double Hue, Saturation, Value;
public HsvColor(double hue, double saturation, double value) {
Hue = hue;
Saturation = saturation;
Value = value;
}
public HsvColor(uint rgba)
: this(Color.FromUint(rgba)) {
}
public HsvColor(Color color) {
var r = color.Red;
var g = color.Green;
var b = color.Blue;
Value = Math.Max(Math.Max(r, g), b);
var c = Value - Math.Min(Math.Min(r, g), b);
Hue =
(c == 0) ? 0 :
(Value == r) ? (60) * (0 + (g - b) / c) :
(Value == g) ? (60) * (2 + (b - r) / c) :
(Value == b) ? (60) * (4 + (r - g) / c) :
0;
Saturation = (Value == 0) ? 0 : (c / Value);
}
public Color ToColor() {
var c = Value * Saturation;
var hi = (Hue / 60);
var x = c * (1 - Math.Abs(hi % 2 - 1));
double r, g, b;
if (hi < 1) {
(r, g, b) = (c, x, 0);
} else if (hi < 2) {
(r, g, b) = (x, c, 0);
} else if (hi < 3) {
(r, g, b) = (0, c, x);
} else if (hi < 4) {
(r, g, b) = (0, x, c);
} else if (hi < 5) {
(r, g, b) = (x, 0, c);
} else {
(r, g, b) = (c, 0, x);
}
var m = Value - c;
return new Color((float)(r + m), (float)(g + m), (float)(b + m));
}
public uint ToUInt32() => ToColor().ToUnsignedInteger();
}
}