1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-23 17:13:38 +02:00

cleaned up color extensions and move some methods to colorhelper instead

This commit is contained in:
Ell 2020-11-04 23:44:41 +01:00
parent 75d8a556eb
commit bb596c91ff
3 changed files with 59 additions and 15 deletions

View file

@ -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 {
/// <summary>
@ -44,7 +44,7 @@ namespace MLEM.Extended.Tiled {
/// <param name="key">The key by which to get a property</param>
/// <returns>The color property</returns>
public static Color GetColor(this TiledMapProperties properties, string key) {
return ColorExtensions.FromHex(properties.Get(key));
return ColorHelper.FromHexString(properties.Get(key));
}
/// <summary>

View file

@ -13,9 +13,8 @@ namespace MLEM.Extensions {
/// </summary>
/// <param name="color">The color to invert</param>
/// <returns>The inverted color</returns>
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);
/// <summary>
/// Parses a hexadecimal number into a color.
@ -23,9 +22,8 @@ namespace MLEM.Extensions {
/// </summary>
/// <param name="value">The number to parse</param>
/// <returns>The resulting color</returns>
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);
/// <summary>
/// Parses a hexadecimal string into a color.
@ -33,11 +31,8 @@ namespace MLEM.Extensions {
/// </summary>
/// <param name="value">The string to parse</param>
/// <returns>The resulting color</returns>
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);
/// <summary>
/// Copies the alpha value from another color into this color.
@ -50,4 +45,53 @@ namespace MLEM.Extensions {
}
}
/// <summary>
/// A set of utility methods for dealing with <see cref="Color"/> objects
/// </summary>
public static class ColorHelper {
/// <summary>
/// Returns an inverted version of the color.
/// </summary>
/// <param name="color">The color to invert</param>
/// <returns>The inverted color</returns>
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);
}
/// <summary>
/// Parses a hexadecimal number into an rgba color.
/// The number should be in the format <c>0xaarrggbb</c>.
/// </summary>
/// <param name="value">The number to parse</param>
/// <returns>The resulting color</returns>
public static Color FromHexRgba(int value) {
return new Color(value >> 16 & 0xFF, value >> 8 & 0xFF, value >> 0 & 0xFF, value >> 24 & 0xFF);
}
/// <summary>
/// Parses a hexadecimal number into an rgb color with 100% alpha.
/// The number should be in the format <c>0xrrggbb</c>.
/// </summary>
/// <param name="value">The number to parse</param>
/// <returns>The resulting color</returns>
public static Color FromHexRgb(int value) {
return new Color(value >> 16 & 0xFF, value >> 8 & 0xFF, value >> 0 & 0xFF);
}
/// <summary>
/// Parses a hexadecimal string into a color.
/// The string can either be formatted as RRGGBB or AARRGGBB and can optionally start with a <c>#</c>.
/// </summary>
/// <param name="value">The string to parse</param>
/// <returns>The resulting color</returns>
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);
}
}
}

View file

@ -34,7 +34,7 @@ namespace MLEM.Formatting {
// font codes
this.Codes.Add(new Regex("<b>"), (f, m, r) => new FontCode(m, r, fnt => fnt.Bold));
this.Codes.Add(new Regex("<i>"), (f, m, r) => new FontCode(m, r, fnt => fnt.Italic));
this.Codes.Add(new Regex(@"<s(?: #([0-9\w]{6,8}) (([+-.0-9]*)))?>"), (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(@"<s(?: #([0-9\w]{6,8}) (([+-.0-9]*)))?>"), (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("<u>"), (f, m, r) => new UnderlineCode(m, r, 1 / 16F, 0.85F));
this.Codes.Add(new Regex("</(b|i|s|u|l)>"), (f, m, r) => new FontCode(m, r, null));
@ -45,7 +45,7 @@ namespace MLEM.Formatting {
this.Codes.Add(new Regex($"<c {c.Name}>"), (f, m, r) => new ColorCode(m, r, value));
}
}
this.Codes.Add(new Regex(@"<c #([0-9\w]{6,8})>"), (f, m, r) => new ColorCode(m, r, ColorExtensions.FromHex(m.Groups[1].Value)));
this.Codes.Add(new Regex(@"<c #([0-9\w]{6,8})>"), (f, m, r) => new ColorCode(m, r, ColorHelper.FromHexString(m.Groups[1].Value)));
this.Codes.Add(new Regex("</c>"), (f, m, r) => new ColorCode(m, r, null));
// animation codes