2019-08-06 14:20:11 +02:00
|
|
|
using System;
|
2020-04-30 21:15:28 +02:00
|
|
|
using System.Globalization;
|
2019-08-06 14:20:11 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
|
|
|
namespace MLEM.Extensions {
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// A set of extensions for dealing with <see cref="Color"/> objects
|
|
|
|
/// </summary>
|
2019-08-06 14:20:11 +02:00
|
|
|
public static class ColorExtensions {
|
|
|
|
|
2020-05-20 23:59:40 +02:00
|
|
|
/// <summary>
|
2020-05-21 17:21:34 +02:00
|
|
|
/// Copies the alpha value from another color into this color.
|
2020-05-20 23:59:40 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="color">The color</param>
|
|
|
|
/// <param name="other">The color to copy the alpha from</param>
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <returns>The first color with the second color's alpha value</returns>
|
2019-08-09 22:04:26 +02:00
|
|
|
public static Color CopyAlpha(this Color color, Color other) {
|
|
|
|
return color * (other.A / 255F);
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:34:32 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns an inverted version of this 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);
|
|
|
|
}
|
|
|
|
|
2019-08-06 14:20:11 +02:00
|
|
|
}
|
2020-11-04 23:44:41 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A set of utility methods for dealing with <see cref="Color"/> objects
|
|
|
|
/// </summary>
|
|
|
|
public static class ColorHelper {
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-08-06 14:20:11 +02:00
|
|
|
}
|