Skip to content

Commit

Permalink
Merge pull request #1357 from VladiStep/colorPickerFix
Browse files Browse the repository at this point in the history
The color picker fix and improvement.
  • Loading branch information
Grossley authored May 4, 2023
2 parents f65ad21 + c58f51a commit f7e65cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion UndertaleModTool/Controls/ColorPicker.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Border>
</Viewbox>

<local:TextBoxDark x:Name="ColorText" Grid.Column="1" MaxLength="9" ToolTip="A, B, G, R"/>
<local:TextBoxDark x:Name="ColorText" Grid.Column="1" MaxLength="9" ToolTip="#AABBGGRR"/>
<!-- "Text" binding is in the code of "ColorPicker" -->
</Grid>
</UserControl>
46 changes: 27 additions & 19 deletions UndertaleModTool/Controls/ColorPicker.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public ColorPicker()
ColorText.SetBinding(TextBox.TextProperty, binding);

ColorText.MaxLength = HasAlpha ? 9 : 7;
ColorText.ToolTip = $"{(HasAlpha ? "A, " : "")}B, G, R";
ColorText.ToolTip = $"#{(HasAlpha ? "AA" : "")}BBGGRR";
ToolTipService.SetInitialShowDelay(ColorText, 250);
}

private static void OnHasAlphaChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
Expand All @@ -77,7 +78,7 @@ private static void OnHasAlphaChanged(DependencyObject dependencyObject, Depende
colorPicker.ColorText.SetBinding(TextBox.TextProperty, binding);

colorPicker.ColorText.MaxLength = hasAlpha ? 9 : 7;
colorPicker.ColorText.ToolTip = $"{(hasAlpha ? "A, " : "")}B, G, R";
colorPicker.ColorText.ToolTip = $"#{(hasAlpha ? "AA" : "")}BBGGRR";
}
}

Expand All @@ -102,33 +103,40 @@ public class ColorTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
uint val = System.Convert.ToUInt32(value);
bool hasAlpha = bool.Parse((string)parameter);
return "#" + (hasAlpha ? val.ToString("X8") : val.ToString("X8")[2..]);
try
{
uint val = System.Convert.ToUInt32(value);
bool hasAlpha = bool.Parse((string)parameter);
return "#" + (hasAlpha ? val.ToString("X8") : val.ToString("X8")[2..]);
}
catch (Exception ex)
{
return new ValidationResult(false, ex.Message);
}
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string val = (string)value;
bool hasAlpha = bool.Parse((string)parameter);

if (val[0] != '#')
return new ValidationResult(false, "Invalid color string");
try
{
string val = (string)value;
bool hasAlpha = bool.Parse((string)parameter);

val = val[1..];
if (val.Length != (hasAlpha ? 8 : 6))
return new ValidationResult(false, "Invalid color string");
if (val[0] != '#')
return new ValidationResult(false, "Invalid color string");

if (!hasAlpha)
val = "FF" + val; // add alpha (255)
val = val[1..];
if (val.Length != (hasAlpha ? 8 : 6))
return new ValidationResult(false, "Invalid color string");

try
{
if (!hasAlpha)
val = "FF" + val; // add alpha (255)

return System.Convert.ToUInt32(val, 16);
}
catch (Exception e)
catch (Exception ex)
{
return new ValidationResult(false, e.Message);
return new ValidationResult(false, ex.Message);
}
}
}
Expand Down

0 comments on commit f7e65cb

Please sign in to comment.