-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorPickerDialog.xaml.cs
More file actions
96 lines (78 loc) · 2.65 KB
/
ColorPickerDialog.xaml.cs
File metadata and controls
96 lines (78 loc) · 2.65 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
using System;
using System.Windows;
using System.Windows.Media;
using SR = FlowInk.Properties.Resources;
using Forms = System.Windows.Forms;
using Drawing = System.Drawing;
namespace FlowInk;
public partial class ColorPickerDialog : Window
{
private Color _selectedColor;
private readonly int[] _customColors;
public Color SelectedColor => _selectedColor;
public int[] CustomColors => _customColors;
public ColorPickerDialog(Color initialColor, int[]? customColors = null)
{
InitializeComponent();
_selectedColor = initialColor;
_customColors = customColors != null ? (int[])customColors.Clone() : new int[16];
AlphaSlider.Value = initialColor.A;
UpdatePreview();
}
private void ChooseBaseColorButton_Click(object sender, RoutedEventArgs e)
{
using var dialog = new Forms.ColorDialog
{
FullOpen = true,
AllowFullOpen = true,
AnyColor = true,
SolidColorOnly = false,
Color = Drawing.Color.FromArgb(
255,
_selectedColor.R,
_selectedColor.G,
_selectedColor.B),
CustomColors = (int[])_customColors.Clone()
};
if (dialog.ShowDialog() != Forms.DialogResult.OK)
{
return;
}
Array.Copy(dialog.CustomColors, _customColors, Math.Min(dialog.CustomColors.Length, _customColors.Length));
_selectedColor = Color.FromArgb(
_selectedColor.A,
dialog.Color.R,
dialog.Color.G,
dialog.Color.B);
UpdatePreview();
}
private void AlphaSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!IsLoaded)
{
return;
}
_selectedColor = Color.FromArgb(
(byte)Math.Round(AlphaSlider.Value),
_selectedColor.R,
_selectedColor.G,
_selectedColor.B);
UpdatePreview();
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
_selectedColor = Color.FromArgb(
(byte)Math.Round(AlphaSlider.Value),
_selectedColor.R,
_selectedColor.G,
_selectedColor.B);
DialogResult = true;
}
private void UpdatePreview()
{
PreviewBrush.Color = _selectedColor;
ColorValueTextBlock.Text = $"#{_selectedColor.A:X2}{_selectedColor.R:X2}{_selectedColor.G:X2}{_selectedColor.B:X2}";
int alphaPercent = (int)Math.Round(_selectedColor.A * 100.0 / 255.0);
AlphaValueTextBlock.Text = string.Format(SR.TransparencyFormat, alphaPercent);
}
}