[ColorPicker] Add hexadecimal integer format (#21354)

* ColorPicker: Add hexadecimal integer format

* Fix typo: integral -> integer

* Rename as Hex Int
This commit is contained in:
Marius Bughiu
2022-10-30 22:07:33 +02:00
committed by GitHub
parent db191b8b75
commit 6431ccd370
5 changed files with 32 additions and 1 deletions

View File

@@ -48,6 +48,7 @@ namespace ColorPicker.Helpers
ColorRepresentationType.CIEXYZ => ColorToCIEXYZ(color),
ColorRepresentationType.VEC4 => ColorToFloat(color),
ColorRepresentationType.DecimalValue => ColorToDecimal(color),
ColorRepresentationType.HexInteger => ColorToHexInteger(color),
// Fall-back value, when "_userSettings.CopiedColorRepresentation.Value" is incorrect
_ => ColorToHex(color),
@@ -76,7 +77,7 @@ namespace ColorPicker.Helpers
/// <summary>
/// Return a hexadecimal <see cref="string"/> representation of a RGB color
/// </summary>
/// <param name="color">The see cref="Color"/> for the hexadecimal presentation</param>
/// <param name="color">The <see cref="Color"/> for the hexadecimal presentation</param>
/// <returns>A hexadecimal <see cref="string"/> representation of a RGB color</returns>
private static string ColorToHex(Color color)
{
@@ -266,5 +267,20 @@ namespace ColorPicker.Helpers
$", {y.ToString(CultureInfo.InvariantCulture)}" +
$", {z.ToString(CultureInfo.InvariantCulture)})";
}
/// <summary>
/// Return a hexadecimal integer <see cref="string"/> representation of a RGB color
/// </summary>
/// <param name="color">The <see cref="Color"/> for the hexadecimal integer presentation</param>
/// <returns>A hexadecimal integer <see cref="string"/> representation of a RGB color</returns>
private static string ColorToHexInteger(Color color)
{
const string hexFormat = "X2";
return "0xFF"
+ $"{color.R.ToString(hexFormat, CultureInfo.InvariantCulture)}"
+ $"{color.G.ToString(hexFormat, CultureInfo.InvariantCulture)}"
+ $"{color.B.ToString(hexFormat, CultureInfo.InvariantCulture)}";
}
}
}

View File

@@ -326,6 +326,12 @@ namespace ColorPicker.ViewModels
FormatName = "Decimal",
Convert = (Color color) => ColorRepresentationHelper.GetStringRepresentationFromMediaColor(color, ColorRepresentationType.DecimalValue),
});
_allColorRepresentations.Add(
new ColorFormatModel()
{
FormatName = "HEX Int",
Convert = (Color color) => ColorRepresentationHelper.GetStringRepresentationFromMediaColor(color, ColorRepresentationType.HexInteger),
});
_userSettings.VisibleColorFormats.CollectionChanged += VisibleColorFormats_CollectionChanged;

View File

@@ -26,6 +26,7 @@ namespace Microsoft.ColorPicker.UnitTests
[DataRow(ColorRepresentationType.CIEXYZ, "XYZ(0, 0, 0)")]
[DataRow(ColorRepresentationType.VEC4, "(0f, 0f, 0f, 1f)")]
[DataRow(ColorRepresentationType.DecimalValue, "0")]
[DataRow(ColorRepresentationType.HexInteger, "0xFF000000")]
public void GetStringRepresentationTest(ColorRepresentationType type, string expected)
{