Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/color picker fixes #454

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 91 additions & 17 deletions src/Orc.Controls/Controls/ColorPicker/ColorBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ public ColorBoard()
/// The tracking hsv.
/// </summary>
private bool _trackingHsv;

private List<PredefinedColor> _colors;

#endregion

#region Public Events
Expand Down Expand Up @@ -413,34 +416,39 @@ public override void OnApplyTemplate()
{
throw Log.ErrorAndCreateException<InvalidOperationException>("Can't find template part 'PART_B1GradientStop'");
}

_aTextBox = GetTemplateChild("PART_ATextBox") as TextBox;
if (_aTextBox is null)
{
throw Log.ErrorAndCreateException<InvalidOperationException>("Can't find template part 'PART_ATextBox'");
}
_aTextBox.LostFocus += OnATextBoxLostFocus;
_aTextBox.TextChanged += OnArgbTextBoxTextChanged;
_aTextBox.TextChanged += IsHexadecimal;

_rTextBox = GetTemplateChild("PART_RTextBox") as TextBox;
if (_rTextBox is null)
{
throw Log.ErrorAndCreateException<InvalidOperationException>("Can't find template part 'PART_RTextBox'");
}
_rTextBox.LostFocus += OnRTextBoxLostFocus;
_rTextBox.TextChanged += OnArgbTextBoxTextChanged;

_gTextBox = GetTemplateChild("PART_GTextBox") as TextBox;
if (_gTextBox is null)
{
throw Log.ErrorAndCreateException<InvalidOperationException>("Can't find template part 'PART_GTextBox'");
}
_gTextBox.LostFocus += OnGTextBoxLostFocus;
_gTextBox.TextChanged += OnArgbTextBoxTextChanged;

_bTextBox = GetTemplateChild("PART_BTextBox") as TextBox;
if (_bTextBox is null)
{
throw Log.ErrorAndCreateException<InvalidOperationException>("Can't find template part 'PART_BTextBox'");
}
_bTextBox.LostFocus += OnBTextBoxLostFocus;
_bTextBox.TextChanged += OnArgbTextBoxTextChanged;

/*Color*/
_colorComboBox = GetTemplateChild("PART_ColorComboBox") as ComboBox;
Expand Down Expand Up @@ -566,6 +574,7 @@ private void OnColorBoardKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
OnColorTextBoxLostFocus(sender, e);
OnDoneClicked();
}
}
Expand Down Expand Up @@ -735,6 +744,7 @@ private void InitializePredefined()
}

var list = PredefinedColor.All;
_colors = list;
_dictionaryColor = new Dictionary<Color, PredefinedColorItem>();
foreach (var color in list)
{
Expand Down Expand Up @@ -910,6 +920,7 @@ private void OnColorComboBoxSelectionChanged(object sender, SelectionChangedEven

if (_colorComboBox.SelectedItem is PredefinedColorItem colorItem)
{
_colorComboBox.SetCurrentValue(Selector.SelectedItemProperty, null);
SetCurrentValue(ColorProperty, colorItem.Color);
}
}
Expand Down Expand Up @@ -979,7 +990,11 @@ private void OnATextBoxLostFocus(object sender, RoutedEventArgs e)
if (int.TryParse(_aTextBox.Text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var value))
{
_aSlider.SetCurrentValue(RangeBase.ValueProperty, (double)value);
return;
}
_aSlider.SetCurrentValue(RangeBase.ValueProperty, (double)255);
_aTextBox.Clear();
_aTextBox.AppendText("FF");
}

/// <summary>
Expand All @@ -997,7 +1012,11 @@ private void OnBTextBoxLostFocus(object sender, RoutedEventArgs e)
if (int.TryParse(_bTextBox.Text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var value))
{
_bSlider.SetCurrentValue(RangeBase.ValueProperty, (double)value);
return;
}
_bSlider.SetCurrentValue(RangeBase.ValueProperty, (double)255);
_bTextBox.Clear();
_bTextBox.AppendText("FF");
}

/// <summary>
Expand Down Expand Up @@ -1036,27 +1055,54 @@ private void OnColorTextBoxLostFocus(object sender, RoutedEventArgs e)
{
return;
}

var text = _colorTextBox.Text.TrimStart('#');
if (uint.TryParse(text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var value))
if (_colorTextBox.Text.StartsWith("#"))
{
var b = (byte)(value & 0xFF);
value >>= 8;
var g = (byte)(value & 0xFF);
value >>= 8;
var r = (byte)(value & 0xFF);
value >>= 8;
var a = (byte)(value & 0xFF);

if (text.Length <= 6)
var text = _colorTextBox.Text.TrimStart('#');
if (uint.TryParse(text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var value))
{
a = 0xFF;
var b = (byte)(value & 0xFF);
value >>= 8;
var g = (byte)(value & 0xFF);
value >>= 8;
var r = (byte)(value & 0xFF);
value >>= 8;
var a = (byte)(value & 0xFF);

if (text.Length <= 6)
{
a = 0xFF;
}

var color = Color.FromArgb(a, r, g, b);

SetCurrentValue(ColorProperty, color);

try
{
var match = _colors.First(c => c.Value == color).Name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FirstOrDefault instead? then there won't be exceptions (better for performance, etc)

if (match is not null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!string.IsNullOrWhiteSpace(match))

{
_colorTextBox.Clear();
_colorTextBox.AppendText(match);
}
return;
}
catch
{
SetCurrentValue(ColorProperty, Colors.White);
return;
}
}
}

var color = Color.FromArgb(a, r, g, b);
SetCurrentValue(ColorProperty, color);
try
{
var color = (Color)ColorConverter.ConvertFromString(_colorTextBox.Text);
var match = _colors.First(c => c.Value == color);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FirstOrDefault, safely handle exceptions


SetCurrentValue(ColorProperty, match.Value);
}
else
catch
{
SetCurrentValue(ColorProperty, Colors.White);
}
Expand All @@ -1077,7 +1123,11 @@ private void OnGTextBoxLostFocus(object sender, RoutedEventArgs e)
if (int.TryParse(_gTextBox.Text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var value))
{
_gSlider.SetCurrentValue(RangeBase.ValueProperty, (double)value);
return;
}
_gSlider.SetCurrentValue(RangeBase.ValueProperty, (double)255);
_gTextBox.Clear();
_gTextBox.AppendText("FF");
}

/// <summary>
Expand All @@ -1095,7 +1145,11 @@ private void OnRTextBoxLostFocus(object sender, RoutedEventArgs e)
if (int.TryParse(_rTextBox.Text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var value))
{
_rSlider.SetCurrentValue(RangeBase.ValueProperty, (double)value);
return;
}
_rSlider.SetCurrentValue(RangeBase.ValueProperty, (double)255);
_rTextBox.Clear();
_rTextBox.AppendText("FF");
}

/// <summary>
Expand All @@ -1113,6 +1167,26 @@ private void OnThemeColorsSelectionChanged(object sender, SelectionChangedEventA
var c = ((PredefinedColorItem)_themeColorsListBox.SelectedItem).Color;
UpdateControls(c, true, true, true);
}

private void OnArgbTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
if (((TextBox)sender).MaxLength == ((TextBox)sender).Text.Length)
{
var ue = e.OriginalSource as FrameworkElement;
e.Handled = true;
ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}

private void IsHexadecimal(object sender, TextChangedEventArgs e)
{
var regex = new System.Text.RegularExpressions.Regex("^[a-fA-F0-9]+$");

if (!regex.IsMatch(((TextBox)sender).Text))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cast sender to TextBox just once

{
((TextBox)sender).Clear();
}
}
#endregion
}
}
Loading