diff --git a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs
index 274e7f5851..7674b74b6a 100644
--- a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs
+++ b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs
@@ -61,7 +61,11 @@ namespace Avalonia.Controls
{
if (_hexTextBox != null)
{
- _hexTextBox.Text = ColorToHexConverter.ToHexString(Color, HexInputAlphaPosition);
+ _hexTextBox.Text = ColorToHexConverter.ToHexString(
+ Color,
+ HexInputAlphaPosition,
+ includeAlpha: (IsAlphaEnabled && IsAlphaVisible),
+ includeSymbol: false);
}
}
diff --git a/src/Avalonia.Controls.ColorPicker/Converters/ColorToHexConverter.cs b/src/Avalonia.Controls.ColorPicker/Converters/ColorToHexConverter.cs
index 8798f874f4..8257499d70 100644
--- a/src/Avalonia.Controls.ColorPicker/Converters/ColorToHexConverter.cs
+++ b/src/Avalonia.Controls.ColorPicker/Converters/ColorToHexConverter.cs
@@ -11,6 +11,18 @@ namespace Avalonia.Controls.Converters
///
public class ColorToHexConverter : IValueConverter
{
+ ///
+ /// Gets or sets a value indicating whether the alpha component is visible in the Hex formatted text.
+ ///
+ ///
+ /// When hidden the existing alpha component value is maintained. Also when hidden the user is still
+ /// able to input an 8-digit number with alpha. Alpha will be processed but then removed when displayed.
+ ///
+ /// Because this property only controls whether alpha is displayed (and it is still processed regardless)
+ /// it is termed 'Visible' instead of 'Enabled'.
+ ///
+ public bool IsAlphaVisible { get; set; } = true;
+
///
/// Gets or sets the position of a color's alpha component relative to all other components.
///
@@ -48,7 +60,7 @@ namespace Avalonia.Controls.Converters
return AvaloniaProperty.UnsetValue;
}
- return ToHexString(color, AlphaPosition, includeSymbol);
+ return ToHexString(color, AlphaPosition, IsAlphaVisible, includeSymbol);
}
///
@@ -67,26 +79,40 @@ namespace Avalonia.Controls.Converters
///
/// The color to represent as a hex value string.
/// The output position of the alpha component.
+ /// Whether the alpha component will be included in the hex string.
/// Whether the hex symbol '#' will be added.
/// The input color converted to its hex value string.
public static string ToHexString(
Color color,
AlphaComponentPosition alphaPosition,
+ bool includeAlpha = true,
bool includeSymbol = false)
{
uint intColor;
- if (alphaPosition == AlphaComponentPosition.Trailing)
+ string hexColor;
+
+ if (includeAlpha)
{
- intColor = ((uint)color.R << 24) | ((uint)color.G << 16) | ((uint)color.B << 8) | (uint)color.A;
+ if (alphaPosition == AlphaComponentPosition.Trailing)
+ {
+ intColor = ((uint)color.R << 24) | ((uint)color.G << 16) | ((uint)color.B << 8) | (uint)color.A;
+ }
+ else
+ {
+ // Default is Leading alpha (same as XAML)
+ intColor = ((uint)color.A << 24) | ((uint)color.R << 16) | ((uint)color.G << 8) | (uint)color.B;
+ }
+
+ hexColor = intColor.ToString("x8", CultureInfo.InvariantCulture).ToUpperInvariant();
}
else
{
- // Default is Leading alpha
- intColor = ((uint)color.A << 24) | ((uint)color.R << 16) | ((uint)color.G << 8) | (uint)color.B;
+ // In this case the alpha position no longer matters
+ // Both cases are calculated the same
+ intColor = ((uint)color.R << 16) | ((uint)color.G << 8) | (uint)color.B;
+ hexColor = intColor.ToString("x6", CultureInfo.InvariantCulture).ToUpperInvariant();
}
- string hexColor = intColor.ToString("x8", CultureInfo.InvariantCulture).ToUpperInvariant();
-
if (includeSymbol)
{
hexColor = '#' + hexColor;