-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathColorPickerSlider.cs
More file actions
275 lines (245 loc) · 9.66 KB
/
ColorPickerSlider.cs
File metadata and controls
275 lines (245 loc) · 9.66 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using CommunityToolkit.WinUI.Helpers;
using Windows.UI;
namespace CommunityToolkit.WinUI.Controls.Primitives;
/// <summary>
/// A slider that represents a single color channel for use in the <see cref="ColorPicker"/>.
/// </summary>
public partial class ColorPickerSlider : Slider
{
// TODO Combine this with the ColorPicker field or make a property
internal Color CheckerBackgroundColor { get; set; } = Color.FromArgb(0x19, 0x80, 0x80, 0x80); // Overridden later
private Size oldSize = Size.Empty;
private Size measuredSize = Size.Empty;
private Size cachedSize = Size.Empty;
/***************************************************************************************
*
* Constructor/Destructor
*
***************************************************************************************/
/// <summary>
/// Initializes a new instance of the <see cref="ColorPickerSlider"/> class.
/// </summary>
public ColorPickerSlider()
: base()
{
this.DefaultStyleKey = typeof(ColorPickerSlider);
}
/***************************************************************************************
*
* Methods
*
***************************************************************************************/
/// <summary>
/// Update the slider's Foreground and Background brushes based on the current slider state and color.
/// </summary>
/// <remarks>
/// Manually refreshes the background gradient of the slider.
/// This is callable separately for performance reasons.
/// </remarks>
public void UpdateColors()
{
HsvColor hsvColor = this.HsvColor;
// Calculate and set the background
this.UpdateBackground(hsvColor);
// Calculate and set the foreground ensuring contrast with the background
Color rgbColor = hsvColor;
Color selectedRgbColor;
double sliderPercent = this.Value / (this.Maximum - this.Minimum);
if (this.ColorRepresentation == ColorRepresentation.Hsva)
{
if (this.IsAlphaMaxForced &&
this.ColorChannel != ColorChannel.Alpha)
{
hsvColor.Alpha = 1.0;
}
switch (this.ColorChannel)
{
case ColorChannel.Channel1:
{
var channelValue = Math.Clamp(sliderPercent * 360.0, 0.0, 360.0);
hsvColor.Hue = channelValue;
if (this.IsSaturationValueMaxForced)
{
hsvColor.Saturation = 1.0;
hsvColor.Value = 1.0;
}
break;
}
case ColorChannel.Channel2:
{
var channelValue = Math.Clamp(sliderPercent * 1.0, 0.0, 1.0);
hsvColor.Saturation = channelValue;
if (this.IsSaturationValueMaxForced)
{
hsvColor.Value = 1.0;
}
break;
}
case ColorChannel.Channel3:
{
var channelValue = Math.Clamp(sliderPercent * 1.0, 0.0, 1.0);
hsvColor.Value = channelValue;
if (this.IsSaturationValueMaxForced)
{
hsvColor.Saturation = 1.0;
}
break;
}
}
selectedRgbColor = hsvColor;
}
else
{
if (this.IsAlphaMaxForced &&
this.ColorChannel != ColorChannel.Alpha)
{
rgbColor = new Color()
{
R = rgbColor.R,
G = rgbColor.G,
B = rgbColor.B,
A = 255
};
}
byte channelValue = Convert.ToByte(Math.Clamp(sliderPercent * 255.0, 0.0, 255.0));
switch (this.ColorChannel)
{
case ColorChannel.Channel1:
rgbColor = new Color()
{
R = channelValue,
G = rgbColor.G,
B = rgbColor.B,
A = rgbColor.A
};
break;
case ColorChannel.Channel2:
rgbColor = new Color()
{
R = rgbColor.R,
G = channelValue,
B = rgbColor.B,
A = rgbColor.A
};
break;
case ColorChannel.Channel3:
rgbColor = new Color()
{
R = rgbColor.R,
G = rgbColor.G,
B = channelValue,
A = rgbColor.A
};
break;
}
selectedRgbColor = rgbColor;
}
var converter = new ContrastBrushConverter();
return;
}
/// <summary>
/// Generates a new background image for the color channel slider and applies it.
/// </summary>
private async void UpdateBackground(HsvColor color)
{
/* Updates may be requested when sliders are not in the visual tree.
* For first-time load this is handled by the Loaded event.
* However, after that problems may arise, consider the following case:
*
* (1) Backgrounds are drawn normally the first time on Loaded.
* Actual height/width are available.
* (2) The palette tab is selected which has no sliders
* (3) The picker flyout is closed
* (4) Externally the color is changed
* The color change will trigger slider background updates but
* with the flyout closed, actual height/width are zero.
* No zero size bitmap can be generated.
* (5) The picker flyout is re-opened by the user and the default
* last-opened tab will be viewed: palette.
* No loaded events will be fired for sliders. The color change
* event was already handled in (4). The sliders will never
* be updated.
*
* In this case the sliders become out of sync with the Color because there is no way
* to tell when they actually come into view. To work around this, force a re-render of
* the background with the last size of the slider. This last size will be when it was
* last loaded or updated.
*
* In the future additional consideration may be required for SizeChanged of the control.
* This work-around will also cause issues if display scaling changes in the special
* case where cached sizes are required.
*/
var width = Convert.ToInt32(this.ActualWidth);
var height = Convert.ToInt32(this.ActualHeight);
if (width == 0 || height == 0)
{
// Attempt to use the last size if it was available
if (this.cachedSize.IsEmpty == false)
{
width = Convert.ToInt32(this.cachedSize.Width);
height = Convert.ToInt32(this.cachedSize.Height);
}
}
else
{
this.cachedSize = new Size(width, height);
}
var bitmap = await ColorPickerRenderingHelpers.CreateChannelBitmapAsync(
width,
height,
this.Orientation,
this.ColorRepresentation,
this.ColorChannel,
color,
this.CheckerBackgroundColor,
this.IsAlphaMaxForced,
this.IsSaturationValueMaxForced);
if (bitmap != null)
{
this.Background = await ColorPickerRenderingHelpers.BitmapToBrushAsync(bitmap, width, height);
}
return;
}
/// <summary>
/// Measures the size in layout required for child elements and determines a size for the
/// FrameworkElement-derived class.
/// </summary>
/// <remarks>
///
/// Slider has some critical bugs:
///
/// * https://github.com/microsoft/microsoft-ui-xaml/issues/477
/// * https://social.msdn.microsoft.com/Forums/sqlserver/en-US/0d3a2e64-d192-4250-b583-508a02bd75e1/uwp-bug-crash-layoutcycleexception-because-of-slider-under-certain-circumstances?forum=wpdevelop
///
/// </remarks>
/// <param name="availableSize">The available size that this element can give to child elements.
/// Infinity can be specified as a value to indicate that the element will size to whatever content
/// is available.</param>
/// <returns>The size that this element determines it needs during layout,
/// based on its calculations of child element sizes.</returns>
protected override Size MeasureOverride(Size availableSize)
{
if (!Size.Equals(oldSize, availableSize))
{
measuredSize = base.MeasureOverride(availableSize);
oldSize = availableSize;
}
return measuredSize;
}
private void OnDependencyPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
{
if (object.ReferenceEquals(args.Property, ColorProperty))
{
// Sync with HSV (which is primary)
this.HsvColor = this.Color.ToHsv();
}
if (this.IsAutoUpdatingEnabled)
{
this.UpdateColors();
}
return;
}
}