From bb596c91ff20456ddcb3fd6d818566652a54ca40 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 4 Nov 2020 23:44:41 +0100 Subject: [PATCH] cleaned up color extensions and move some methods to colorhelper instead --- MLEM.Extended/Tiled/TiledExtensions.cs | 4 +- MLEM/Extensions/ColorExtensions.cs | 66 +++++++++++++++++++++----- MLEM/Formatting/TextFormatter.cs | 4 +- 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/MLEM.Extended/Tiled/TiledExtensions.cs b/MLEM.Extended/Tiled/TiledExtensions.cs index 38e3f23..dff4289 100644 --- a/MLEM.Extended/Tiled/TiledExtensions.cs +++ b/MLEM.Extended/Tiled/TiledExtensions.cs @@ -5,7 +5,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended; using MonoGame.Extended.Tiled; -using ColorExtensions = MLEM.Extensions.ColorExtensions; +using ColorHelper = MLEM.Extensions.ColorHelper; namespace MLEM.Extended.Tiled { /// @@ -44,7 +44,7 @@ namespace MLEM.Extended.Tiled { /// The key by which to get a property /// The color property public static Color GetColor(this TiledMapProperties properties, string key) { - return ColorExtensions.FromHex(properties.Get(key)); + return ColorHelper.FromHexString(properties.Get(key)); } /// diff --git a/MLEM/Extensions/ColorExtensions.cs b/MLEM/Extensions/ColorExtensions.cs index e8dc192..71843bc 100644 --- a/MLEM/Extensions/ColorExtensions.cs +++ b/MLEM/Extensions/ColorExtensions.cs @@ -13,9 +13,8 @@ namespace MLEM.Extensions { /// /// The color to invert /// The inverted color - public static Color Invert(this Color color) { - return new Color(Math.Abs(255 - color.R), Math.Abs(255 - color.G), Math.Abs(255 - color.B), color.A); - } + [Obsolete("Use ColorHelper.Invert instead")] + public static Color Invert(this Color color) => ColorHelper.Invert(color); /// /// Parses a hexadecimal number into a color. @@ -23,9 +22,8 @@ namespace MLEM.Extensions { /// /// The number to parse /// The resulting color - public static Color FromHex(uint value) { - return new Color((int) (value >> 16 & 0xFF), (int) (value >> 8 & 0xFF), (int) (value >> 0 & 0xFF), (int) (value >> 24 & 0xFF)); - } + [Obsolete("Use ColorHelper.FromHexRgba instead")] + public static Color FromHex(uint value) => ColorHelper.FromHexRgba((int) value); /// /// Parses a hexadecimal string into a color. @@ -33,11 +31,8 @@ namespace MLEM.Extensions { /// /// The string to parse /// The resulting color - public static Color FromHex(string value) { - if (value.StartsWith("#")) - value = value.Substring(1); - return FromHex(uint.Parse(value, NumberStyles.HexNumber)); - } + [Obsolete("Use ColorHelper.FromHexString instead")] + public static Color FromHex(string value) => ColorHelper.FromHexString(value); /// /// Copies the alpha value from another color into this color. @@ -50,4 +45,53 @@ namespace MLEM.Extensions { } } + + /// + /// A set of utility methods for dealing with objects + /// + public static class ColorHelper { + + /// + /// Returns an inverted version of the color. + /// + /// The color to invert + /// The inverted color + public static Color Invert(this Color color) { + return new Color(Math.Abs(255 - color.R), Math.Abs(255 - color.G), Math.Abs(255 - color.B), color.A); + } + + /// + /// Parses a hexadecimal number into an rgba color. + /// The number should be in the format 0xaarrggbb. + /// + /// The number to parse + /// The resulting color + public static Color FromHexRgba(int value) { + return new Color(value >> 16 & 0xFF, value >> 8 & 0xFF, value >> 0 & 0xFF, value >> 24 & 0xFF); + } + + /// + /// Parses a hexadecimal number into an rgb color with 100% alpha. + /// The number should be in the format 0xrrggbb. + /// + /// The number to parse + /// The resulting color + public static Color FromHexRgb(int value) { + return new Color(value >> 16 & 0xFF, value >> 8 & 0xFF, value >> 0 & 0xFF); + } + + /// + /// Parses a hexadecimal string into a color. + /// The string can either be formatted as RRGGBB or AARRGGBB and can optionally start with a #. + /// + /// The string to parse + /// The resulting color + public static Color FromHexString(string value) { + if (value.StartsWith("#")) + value = value.Substring(1); + var val = int.Parse(value, NumberStyles.HexNumber); + return value.Length > 6 ? FromHexRgba(val) : FromHexRgb(val); + } + + } } \ No newline at end of file diff --git a/MLEM/Formatting/TextFormatter.cs b/MLEM/Formatting/TextFormatter.cs index 5f7c456..e10a915 100644 --- a/MLEM/Formatting/TextFormatter.cs +++ b/MLEM/Formatting/TextFormatter.cs @@ -34,7 +34,7 @@ namespace MLEM.Formatting { // font codes this.Codes.Add(new Regex(""), (f, m, r) => new FontCode(m, r, fnt => fnt.Bold)); this.Codes.Add(new Regex(""), (f, m, r) => new FontCode(m, r, fnt => fnt.Italic)); - this.Codes.Add(new Regex(@""), (f, m, r) => new ShadowCode(m, r, m.Groups[1].Success ? ColorExtensions.FromHex(m.Groups[1].Value) : Color.Black, new Vector2(float.TryParse(m.Groups[2].Value, out var offset) ? offset : 2))); + this.Codes.Add(new Regex(@""), (f, m, r) => new ShadowCode(m, r, m.Groups[1].Success ? ColorHelper.FromHexString(m.Groups[1].Value) : Color.Black, new Vector2(float.TryParse(m.Groups[2].Value, out var offset) ? offset : 2))); this.Codes.Add(new Regex(""), (f, m, r) => new UnderlineCode(m, r, 1 / 16F, 0.85F)); this.Codes.Add(new Regex(""), (f, m, r) => new FontCode(m, r, null)); @@ -45,7 +45,7 @@ namespace MLEM.Formatting { this.Codes.Add(new Regex($""), (f, m, r) => new ColorCode(m, r, value)); } } - this.Codes.Add(new Regex(@""), (f, m, r) => new ColorCode(m, r, ColorExtensions.FromHex(m.Groups[1].Value))); + this.Codes.Add(new Regex(@""), (f, m, r) => new ColorCode(m, r, ColorHelper.FromHexString(m.Groups[1].Value))); this.Codes.Add(new Regex(""), (f, m, r) => new ColorCode(m, r, null)); // animation codes