-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Color picker wpf #37149
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
Changes from 5 commits
fcef988
7afe2f6
32bf575
66324e8
97617d0
8fd15d3
56c0a19
44691d3
4182ea3
7d914cb
793f81d
5aeba14
525739b
75506f7
d891dfe
cfcdd41
4f54a55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
xmlns:helpers="clr-namespace:ColorPicker.Helpers" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:p="clr-namespace:ColorPicker.Properties" | ||
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" | ||
AutomationProperties.Name="{x:Static p:Resources.Color_Palette}" | ||
FocusManager.IsFocusScope="True" | ||
KeyboardNavigation.TabNavigation="Once" | ||
|
@@ -213,19 +212,27 @@ | |
Style="{DynamicResource ColorShadeButtonStyle}" | ||
ToolTipService.ToolTip="{x:Static p:Resources.Selected_color_tooltip}"> | ||
|
||
<ui:Flyout | ||
<Popup | ||
x:Name="DetailsFlyout" | ||
Margin="24,0,0,0" | ||
Closed="DetailsFlyout_Closed" | ||
Opened="DetailsFlyout_Opened" | ||
AllowsTransparency="True" | ||
StaysOpen="False" | ||
Placement="Top"> | ||
<Grid x:Name="detailsGrid" KeyboardNavigation.TabNavigation="Contained"> | ||
<Grid.ColumnDefinitions> | ||
<Border | ||
CornerRadius="10" | ||
mantaionut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" | ||
mantaionut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BorderThickness="1" | ||
Background="{DynamicResource ApplicationBackgroundBrush}" | ||
Padding="15"> | ||
<Grid x:Name="detailsGrid" KeyboardNavigation.TabNavigation="Contained"> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="44" /> | ||
<ColumnDefinition Width="86" /> | ||
<ColumnDefinition Width="86" /> | ||
<ColumnDefinition Width="86" /> | ||
</Grid.ColumnDefinitions> | ||
</Grid.ColumnDefinitions> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="8" /> | ||
|
@@ -317,38 +324,30 @@ | |
Text="RGB" | ||
TextAlignment="Center" /> | ||
|
||
<ui:NumberBox | ||
x:Name="RNumberBox" | ||
Grid.Row="6" | ||
Grid.Column="1" | ||
AutomationProperties.Name="{x:Static p:Resources.Red_value}" | ||
Maximum="255" | ||
Minimum="0" | ||
SpinButtonPlacementMode="Compact" | ||
TextBoxBase.TextChanged="RGBNumberBox_TextChanged" /> | ||
|
||
<ui:NumberBox | ||
x:Name="GNumberBox" | ||
Grid.Row="6" | ||
Grid.Column="2" | ||
Margin="4,0,0,0" | ||
AutomationProperties.Name="{x:Static p:Resources.Green_value}" | ||
Maximum="255" | ||
Minimum="0" | ||
SpinButtonPlacementMode="Compact" | ||
TextBoxBase.TextChanged="RGBNumberBox_TextChanged" /> | ||
|
||
<ui:NumberBox | ||
x:Name="BNumberBox" | ||
Grid.Row="6" | ||
Grid.Column="3" | ||
Margin="4,0,0,0" | ||
AutomationProperties.Name="{x:Static p:Resources.Blue_value}" | ||
Maximum="255" | ||
Minimum="0" | ||
SpinButtonPlacementMode="Compact" | ||
TextBoxBase.TextChanged="RGBNumberBox_TextChanged" /> | ||
|
||
<TextBox | ||
x:Name="RNumberBox" | ||
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 | ||
Grid.Row="8" | ||
Width="38" | ||
|
@@ -370,18 +369,20 @@ | |
TextChanged="HexCode_TextChanged" | ||
TextWrapping="Wrap" /> | ||
|
||
<ui:Button | ||
<Button | ||
x:Name="OKButton" | ||
Grid.Row="9" | ||
Grid.ColumnSpan="4" | ||
Margin="0,32,0,0" | ||
HorizontalAlignment="Stretch" | ||
Appearance="Primary" | ||
Background="{DynamicResource {x:Static SystemColors.AccentColorBrushKey}}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use WinUI but .net WPF. |
||
AutomationProperties.Name="{x:Static p:Resources.Select}" | ||
Click="OKButton_Click" | ||
Content="{x:Static p:Resources.Select}" /> | ||
</Grid> | ||
</ui:Flyout> | ||
|
||
</Grid> | ||
</Border> | ||
</Popup> | ||
</Button> | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,7 +14,6 @@ | |||||
|
||||||
using ColorPicker.Helpers; | ||||||
using ManagedCommon; | ||||||
using Wpf.Ui.Controls; | ||||||
|
||||||
using static System.Net.Mime.MediaTypeNames; | ||||||
|
||||||
|
@@ -76,9 +75,9 @@ private static void SelectedColorPropertyChanged(DependencyObject d, DependencyP | |||||
control._ignoreRGBChanges = true; | ||||||
|
||||||
control.HexCode.Text = ColorToHex(newColor); | ||||||
control.RNumberBox.Value = newColor.R; | ||||||
control.GNumberBox.Value = newColor.G; | ||||||
control.BNumberBox.Value = newColor.B; | ||||||
control.RNumberBox.Text = newColor.R.ToString(CultureInfo.InvariantCulture); | ||||||
control.GNumberBox.Text = newColor.G.ToString(CultureInfo.InvariantCulture); | ||||||
control.BNumberBox.Text = newColor.B.ToString(CultureInfo.InvariantCulture); | ||||||
control.SetColorFromTextBoxes(System.Drawing.Color.FromArgb(newColor.R, newColor.G, newColor.B)); | ||||||
|
||||||
control._ignoreRGBChanges = false; | ||||||
|
@@ -175,9 +174,9 @@ private void UpdateTextBoxesAndCurrentColor(Color currentColor) | |||||
|
||||||
if (!_ignoreRGBChanges) | ||||||
{ | ||||||
RNumberBox.Value = currentColor.R; | ||||||
GNumberBox.Value = currentColor.G; | ||||||
BNumberBox.Value = currentColor.B; | ||||||
RNumberBox.Text = currentColor.R.ToString(CultureInfo.InvariantCulture); | ||||||
GNumberBox.Text = currentColor.G.ToString(CultureInfo.InvariantCulture); | ||||||
BNumberBox.Text = currentColor.B.ToString(CultureInfo.InvariantCulture); | ||||||
} | ||||||
|
||||||
_currentColor = currentColor; | ||||||
|
@@ -231,7 +230,7 @@ private void OKButton_Click(object sender, RoutedEventArgs e) | |||||
{ | ||||||
SelectedColorChangedCommand.Execute(_currentColor); | ||||||
SessionEventHelper.Event.EditorColorAdjusted = true; | ||||||
DetailsFlyout.Hide(); | ||||||
DetailsFlyout.IsOpen = false; | ||||||
} | ||||||
|
||||||
private void DetailsFlyout_Closed(object sender, object e) | ||||||
|
@@ -357,15 +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]+$"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
} | ||||||
|
||||||
private void RGBNumberBox_TextChanged(object sender, TextChangedEventArgs e) | ||||||
{ | ||||||
if (!_ignoreRGBChanges) | ||||||
{ | ||||||
var numberBox = sender as NumberBox; | ||||||
|
||||||
byte r = numberBox.Name == "RNumberBox" ? GetValueFromNumberBox(numberBox) : (byte)RNumberBox.Value; | ||||||
byte g = numberBox.Name == "GNumberBox" ? GetValueFromNumberBox(numberBox) : (byte)GNumberBox.Value; | ||||||
byte b = numberBox.Name == "BNumberBox" ? GetValueFromNumberBox(numberBox) : (byte)BNumberBox.Value; | ||||||
var numberBox = sender as TextBox; | ||||||
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)); | ||||||
|
@@ -379,22 +382,24 @@ 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(NumberBox numberBox) | ||||||
private static byte GetValueFromNumberBox(TextBox numberBox, byte previousValue) | ||||||
{ | ||||||
int minimum = 0; | ||||||
int maximum = 255; | ||||||
double? parsedValue = ParseDouble(numberBox.Text); | ||||||
|
||||||
if (parsedValue != null) | ||||||
{ | ||||||
var parsedValueByte = (byte)parsedValue; | ||||||
|
||||||
if (parsedValueByte >= numberBox.Minimum && parsedValueByte <= numberBox.Maximum) | ||||||
if (parsedValueByte >= minimum && parsedValueByte <= maximum) | ||||||
{ | ||||||
return parsedValueByte; | ||||||
} | ||||||
} | ||||||
|
||||||
// not valid input, return previous value | ||||||
return (byte)numberBox.Value; | ||||||
return previousValue; | ||||||
} | ||||||
|
||||||
public static double? ParseDouble(string text) | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.