using System;
using System.Globalization;
using Microsoft.Xna.Framework;
namespace MLEM.Extensions {
///
/// A set of extensions for dealing with objects
///
public static class ColorExtensions {
///
/// 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 a color.
/// The number should be in the format 0xaarrggbb.
///
/// 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));
}
///
/// Parses a hexadecimal string into a color.
/// The string can optionally start with a #.
///
/// 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));
}
///
/// Copies the alpha value from another color into this color.
///
/// The color
/// The color to copy the alpha from
/// The first color with the second color's alpha value
public static Color CopyAlpha(this Color color, Color other) {
return color * (other.A / 255F);
}
}
}