diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bda42f..f7e38b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Additions - Added GetRandomEntry and GetRandomWeightedEntry to SingleRandom - Added the ability to draw single corners of AutoTiling's extended auto tiles - Added ColorHelper.TryFromHexString, a non-throwing version of FromHexString +- Added ToHexStringRgba and ToHexStringRgb to ColorExtensions Improvements - Stopped the text formatter throwing if a color can't be parsed diff --git a/MLEM/Extensions/ColorExtensions.cs b/MLEM/Extensions/ColorExtensions.cs index 2ac4b9a..0ceebe0 100644 --- a/MLEM/Extensions/ColorExtensions.cs +++ b/MLEM/Extensions/ColorExtensions.cs @@ -37,6 +37,27 @@ namespace MLEM.Extensions { return new Color(color.ToVector4() * other.ToVector4()); } + /// + /// Returns the hexadecimal representation of this color as a string in the format #AARRGGBB, or optionally AARRGGBB, without the pound symbol. + /// + /// The color to convert. + /// Whether a # should prepend the string. + /// The resulting hex string. + public static string ToHexStringRgba(this Color color, bool hash = true) { + return $"{(hash ? "#" : string.Empty)}{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}"; + } + + /// + /// Returns the hexadecimal representation of this color as a string in the format #RRGGBB, or optionally RRGGBB, without the pound symbol. + /// The alpha channel is ignored. + /// + /// The color to convert. + /// Whether a # should prepend the string. + /// The resulting hex string. + public static string ToHexStringRgb(this Color color, bool hash = true) { + return $"{(hash ? "#" : string.Empty)}{color.R:X2}{color.G:X2}{color.B:X2}"; + } + } /// @@ -66,7 +87,7 @@ namespace MLEM.Extensions { /// /// Parses a hexadecimal string into a color and throws a if parsing fails. - /// The string can either be formatted as RRGGBB or AARRGGBB and can optionally start with a #. + /// The string can either be formatted as RRGGBB or AARRGGBB and can optionally start with a #. /// /// The string to parse. /// The resulting color. @@ -79,7 +100,7 @@ namespace MLEM.Extensions { /// /// Tries to parse a hexadecimal string into a color and returns whether a color was successfully parsed. - /// The string can either be formatted as RRGGBB or AARRGGBB and can optionally start with a #. + /// The string can either be formatted as RRGGBB or AARRGGBB and can optionally start with a #. /// /// The string to parse. /// The resulting color.