-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColourPickerVM.cs
More file actions
178 lines (171 loc) · 6.05 KB
/
ColourPickerVM.cs
File metadata and controls
178 lines (171 loc) · 6.05 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
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
extern alias MountAndBlade;
using MountAndBlade.System.Numerics;
using TaleWorlds.GauntletUI.BaseTypes;
using TaleWorlds.Library;
namespace InGameUIDesigner
{
public class ColourPickerVM : ViewModel
{
[DataSourceProperty]
public Color FinalColour
{
get => _finalColour;
set
{
if (value != _finalColour)
{
_finalColour = value;
OnPropertyChangedWithValue(value, "FinalColour");
}
}
}
[DataSourceProperty]
public float Value
{
get => _value;
set
{
if (value != _value)
{
_value = value;
OnPropertyChangedWithValue(value, "Value");
UpdateColourHSVtoRGB();
}
}
}
[DataSourceProperty]
public float HueSaturationPickerXOffset
{
get => _hueSaturationPickerXOffset;
set
{
if (value != _hueSaturationPickerXOffset)
{
_hueSaturationPickerXOffset = value;
OnPropertyChangedWithValue(value, "HueSaturationPickerXOffset");
}
}
}
[DataSourceProperty]
public float HueSaturationPickerYOffset
{
get => _hueSaturationPickerYOffset;
set
{
if (value != _hueSaturationPickerYOffset)
{
_hueSaturationPickerYOffset = value;
OnPropertyChangedWithValue(value, "HueSaturationPickerYOffset");
}
}
}
public ColourPickerVM(UIEditorPopUpsVM owner)
{
_owner = owner;
_hue = 0f;
_saturation = 0f;
_value = 1f;
HueSaturationPickerXOffset = 0f;
HueSaturationPickerYOffset = 0f;
}
public void SetColourPickerWidget(Widget hueSaturationPickerWidget)
{
_hueSaturationPicker = hueSaturationPickerWidget;
}
public void PickHueAndSaturation()
{
var localMousePos = -_hueSaturationPicker.GetLocalPoint(_hueSaturationPicker.EventManager.MousePosition);
localMousePos += _hueSaturationPicker.Size / 2f;
var maxVec = Vector2.Normalize(localMousePos) * _hueSaturationPicker.Size.X / 2f;
if (localMousePos.LengthSquared() > maxVec.LengthSquared()) localMousePos = maxVec;
HueSaturationPickerXOffset = -localMousePos.X * _hueSaturationPicker.Context.InverseScale;
HueSaturationPickerYOffset = -localMousePos.Y * _hueSaturationPicker.Context.InverseScale;
var angle = MathF.Atan2(localMousePos.Y, localMousePos.X);
if (angle < 0) angle = angle + MathF.PI;
if (localMousePos.Y < 0f) angle += MathF.PI;
angle = MathF.RadToDeg * angle;
_saturation = MathF.Clamp(2 * localMousePos.Length() / _hueSaturationPicker.Size.X, 0f, 1f);
_hue = MathF.Clamp(angle, 0f, 360f);
UpdateColourHSVtoRGB();
}
public void UpdateHSVFromColour()
{
var r = FinalColour.Red;
var g = FinalColour.Green;
var b = FinalColour.Blue;
var m_max = MathF.Max(r, g, b);
var m_min = MathF.Min(r, g, b);
var delta = m_max - m_min;
if (delta == 0) _hue = 0;
else if (m_max == r) _hue = (g - b) / delta;
else if (m_max == g) _hue = 2f + (b - r) / delta;
else _hue = 4f + (r - g) / delta;
_hue *= 60f;
_hue = MathF.AngleClamp(_hue * MathF.DegToRad) * MathF.RadToDeg;
var value = m_max;
_saturation = value == 0 ? 0 : delta / value;
var radius = _hueSaturationPicker.SuggestedWidth / 2f;
HueSaturationPickerXOffset = -radius * _saturation * MathF.Cos(_hue * MathF.DegToRad);
HueSaturationPickerYOffset = -radius * _saturation * MathF.Sin(_hue * MathF.DegToRad);
Value = 1 - value;
}
private void UpdateColourHSVtoRGB()
{
// Value Slider is reversed. (Full / 1 is black, and Empty / 0 is white)
var value = 1f - Value;
// I don't understand any of that. I just lifted it off of Wikipedia. It's 2 A.M.
var chroma = value * _saturation;
var hueDash = _hue / 60f;
var x = chroma * (1 - MathF.Abs(hueDash % 2 - 1));
var r1 = 0f;
var g1 = 0f;
var b1 = 0f;
switch (hueDash)
{
case < 1 :
r1 = chroma;
g1 = x;
break;
case < 2:
r1 = x;
g1 = chroma;
break;
case < 3:
g1 = chroma;
b1 = x;
break;
case < 4:
g1 = x;
b1 = chroma;
break;
case < 5:
r1 = x;
b1 = chroma;
break;
case < 6:
r1 = chroma;
b1 = x;
break;
}
var m = value - chroma;
var r = (r1 + m);
var g = (g1 + m);
var b = (b1 + m);
FinalColour = Color.FromVector3(new Vec3(r, g, b));
}
public void PickColour()
{
_owner.OnColourPicked(FinalColour);
_owner.ClosePopUps();
}
private Color _finalColour;
private float _value;
private float _hueSaturationPickerXOffset;
private float _hueSaturationPickerYOffset;
private float _hue;
private float _saturation;
private UIEditorPopUpsVM _owner;
private Widget _hueSaturationPicker;
private bool _dontUpdateColour;
}
}