Browse Source

Remove alpha in Hex color strings when IsAlphaEnabled or IsAlphaVisible is false

pull/10816/head
robloo 4 years ago
parent
commit
a401819251
  1. 6
      src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs
  2. 40
      src/Avalonia.Controls.ColorPicker/Converters/ColorToHexConverter.cs

6
src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs

@ -61,7 +61,11 @@ namespace Avalonia.Controls
{ {
if (_hexTextBox != null) if (_hexTextBox != null)
{ {
_hexTextBox.Text = ColorToHexConverter.ToHexString(Color, HexInputAlphaPosition); _hexTextBox.Text = ColorToHexConverter.ToHexString(
Color,
HexInputAlphaPosition,
includeAlpha: (IsAlphaEnabled && IsAlphaVisible),
includeSymbol: false);
} }
} }

40
src/Avalonia.Controls.ColorPicker/Converters/ColorToHexConverter.cs

@ -11,6 +11,18 @@ namespace Avalonia.Controls.Converters
/// </summary> /// </summary>
public class ColorToHexConverter : IValueConverter public class ColorToHexConverter : IValueConverter
{ {
/// <summary>
/// Gets or sets a value indicating whether the alpha component is visible in the Hex formatted text.
/// </summary>
/// <remarks>
/// 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'.
/// </remarks>
public bool IsAlphaVisible { get; set; } = true;
/// <summary> /// <summary>
/// Gets or sets the position of a color's alpha component relative to all other components. /// Gets or sets the position of a color's alpha component relative to all other components.
/// </summary> /// </summary>
@ -48,7 +60,7 @@ namespace Avalonia.Controls.Converters
return AvaloniaProperty.UnsetValue; return AvaloniaProperty.UnsetValue;
} }
return ToHexString(color, AlphaPosition, includeSymbol); return ToHexString(color, AlphaPosition, IsAlphaVisible, includeSymbol);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -67,26 +79,40 @@ namespace Avalonia.Controls.Converters
/// </summary> /// </summary>
/// <param name="color">The color to represent as a hex value string.</param> /// <param name="color">The color to represent as a hex value string.</param>
/// <param name="alphaPosition">The output position of the alpha component.</param> /// <param name="alphaPosition">The output position of the alpha component.</param>
/// <param name="includeAlpha">Whether the alpha component will be included in the hex string.</param>
/// <param name="includeSymbol">Whether the hex symbol '#' will be added.</param> /// <param name="includeSymbol">Whether the hex symbol '#' will be added.</param>
/// <returns>The input color converted to its hex value string.</returns> /// <returns>The input color converted to its hex value string.</returns>
public static string ToHexString( public static string ToHexString(
Color color, Color color,
AlphaComponentPosition alphaPosition, AlphaComponentPosition alphaPosition,
bool includeAlpha = true,
bool includeSymbol = false) bool includeSymbol = false)
{ {
uint intColor; 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 else
{ {
// Default is Leading alpha // In this case the alpha position no longer matters
intColor = ((uint)color.A << 24) | ((uint)color.R << 16) | ((uint)color.G << 8) | (uint)color.B; // 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) if (includeSymbol)
{ {
hexColor = '#' + hexColor; hexColor = '#' + hexColor;

Loading…
Cancel
Save