using System;
using Microsoft.Xna.Framework;
namespace MonoGame.Extended
{
///
/// Provides additional methods for working with color
///
public static class ColorExtensions
{
///
/// Converts a to its hexadecimal string representation in RGBA format.
///
/// The to convert.
/// A hexadecimal string representation of the color in the format #RRGGBBAA.
public static string ToHex(this Color color)
{
string rx = $"{color.R:x2}";
string gx = $"{color.G:x2}";
string bx = $"{color.B:x2}";
string ax = $"{color.A:x2}";
return $"#{rx}{gx}{bx}{ax}";
}
[Obsolete("Use ColorHelper.FromHex instead. This will be removed in the next major SemVer release.")]
public static Color FromHex(string value)
{
return ColorHelper.FromHex(value);
}
[Obsolete("Use HslColor.ToRgb instead. This will be removed in the next major SemVer update")]
public static Color ToRgb(this HslColor c)
{
var h = c.H;
var s = c.S;
var l = c.L;
if (s == 0f)
return new Color(l, l, l);
h = h/360f;
var max = l < 0.5f ? l*(1 + s) : l + s - l*s;
var min = 2f*l - max;
return new Color(
ComponentFromHue(min, max, h + 1f/3f),
ComponentFromHue(min, max, h),
ComponentFromHue(min, max, h - 1f/3f));
}
[Obsolete("This will be removed in the next major SemVer release.")]
private static float ComponentFromHue(float m1, float m2, float h)
{
h = (h + 1f)%1f;
if (h*6f < 1)
return m1 + (m2 - m1)*6f*h;
if (h*2 < 1)
return m2;
if (h*3 < 2)
return m1 + (m2 - m1)*(2f/3f - h)*6f;
return m1;
}
[Obsolete("Use HslColor.FromRgb instead. This method will be removed in the next SemVer release")]
public static HslColor ToHsl(this Color c)
{
var r = c.R/255f;
var b = c.B/255f;
var g = c.G/255f;
var max = Math.Max(Math.Max(r, g), b);
var min = Math.Min(Math.Min(r, g), b);
var chroma = max - min;
var sum = max + min;
var l = sum*0.5f;
if (chroma == 0)
return new HslColor(0f, 0f, l);
float h;
if (r == max)
h = (60*(g - b)/chroma + 360)%360;
else
{
if (g == max)
h = 60*(b - r)/chroma + 120f;
else
h = 60*(r - g)/chroma + 240f;
}
var s = l <= 0.5f ? chroma/sum : chroma/(2f - sum);
return new HslColor(h, s, l);
}
///
/// Returns a new value based on a packed value in the ABGR format.
///
///
/// This is useful for when you have HTML hex style values such as #123456 and want to use it in hex format for
/// the parameter. Since Color's standard format is RGBA, you would have to do new Color(0xFF563212) since R
/// is the LSB. With this method, you can write it the same way it is written in HTML hex by doing
/// >ColorExtensions.FromAbgr(0x123456FF);
///
/// The packed color value in ABGR format
/// The value created
[Obsolete("Use ColorHelper.FromAbgr instead. This will be removed in the next major SemVer release.")]
public static Color FromAbgr(uint abgr)
{
uint rgba = (abgr & 0x000000FF) << 24 | // Alpha
(abgr & 0x0000FF00) << 8 | // Blue
(abgr & 0x00FF0000) >> 8 | // Green
(abgr & 0xFF000000) >> 24; // Red
Color result;
#if FNA
result = default;
result.PackedValue = rgba;
#else
result = new Color(rgba);
#endif
return result;
}
}
}