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

Color picker wpf #37149

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
More changes
  • Loading branch information
mantaionut committed Jan 29, 2025
commit 66324e8330a646a07bd71779cc5162813738d69b
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
xmlns:p="clr-namespace:ColorPicker.Properties"
Width="440"
Height="380"

Title="{x:Static p:Resources.CP_Title}"
AutomationProperties.Name="{x:Static p:Resources.cp_editor}"
ResizeMode="NoResize"
Topmost="True"
Original file line number Diff line number Diff line change
@@ -329,20 +329,23 @@
Grid.Row="6"
Grid.Column="1"
AutomationProperties.Name="{x:Static p:Resources.Red_value}"
PreviewTextInput="TextBox_PreviewTextInput"
TextBoxBase.TextChanged="RGBNumberBox_TextChanged" />
<TextBox
x:Name="GNumberBox"
Grid.Row="6"
Grid.Column="2"
Margin="4,0,0,0"
AutomationProperties.Name="{x:Static p:Resources.Green_value}"
PreviewTextInput="TextBox_PreviewTextInput"
TextBoxBase.TextChanged="RGBNumberBox_TextChanged" />
<TextBox
x:Name="BNumberBox"
Grid.Row="6"
Grid.Column="3"
Margin="4,0,0,0"
AutomationProperties.Name="{x:Static p:Resources.Blue_value}"
PreviewTextInput="TextBox_PreviewTextInput"
TextBoxBase.TextChanged="RGBNumberBox_TextChanged" />

<TextBlock
Original file line number Diff line number Diff line change
@@ -356,14 +356,19 @@ private void HexCode_GotKeyboardFocus(object sender, KeyboardFocusChangedEventAr
(sender as System.Windows.Controls.TextBox).SelectAll();
}

private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !System.Text.RegularExpressions.Regex.IsMatch(e.Text, "^[0-9]+$");
Copy link
Preview

Copilot AI Feb 19, 2025

Choose a reason for hiding this comment

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

The regex should allow hexadecimal characters (0-9, a-f, A-F) for color codes. Update the regex to: "^[0-9a-fA-F]+$".

Suggested change
e.Handled = !System.Text.RegularExpressions.Regex.IsMatch(e.Text, "^[0-9]+$");
e.Handled = !System.Text.RegularExpressions.Regex.IsMatch(e.Text, "^[0-9a-fA-F]+$");

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

}

private void RGBNumberBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (!_ignoreRGBChanges)
{
var numberBox = sender as TextBox;
byte r = GetValueFromNumberBox(RNumberBox);
byte g = GetValueFromNumberBox(GNumberBox);
byte b = GetValueFromNumberBox(BNumberBox);
byte r = numberBox.Name == "RNumberBox" ? GetValueFromNumberBox(numberBox, _currentColor.R) : _currentColor.R;
byte g = numberBox.Name == "GNumberBox" ? GetValueFromNumberBox(numberBox, _currentColor.G) : _currentColor.G;
byte b = numberBox.Name == "BNumberBox" ? GetValueFromNumberBox(numberBox, _currentColor.B) : _currentColor.B;

_ignoreRGBChanges = true;
SetColorFromTextBoxes(System.Drawing.Color.FromArgb(r, g, b));
@@ -377,7 +382,7 @@ private void RGBNumberBox_TextChanged(object sender, TextChangedEventArgs e)
/// </summary>
/// <param name="numberBox">numberBox control which value we want to get</param>
/// <returns>Validated value as per numberbox conditions, if content is invalid it returns previous value</returns>
private static byte GetValueFromNumberBox(TextBox numberBox)
private static byte GetValueFromNumberBox(TextBox numberBox, byte previousValue)
{
int minimum = 0;
int maximum = 255;
@@ -394,7 +399,7 @@ private static byte GetValueFromNumberBox(TextBox numberBox)
}

// not valid input, return previous value
return 0;
return previousValue;
}

public static double? ParseDouble(string text)