This commit is contained in:
Matthew Leibowitz 2021-04-20 17:30:26 +02:00
Родитель ee70e3e8cd
Коммит c02e389d15
5 изменённых файлов: 651 добавлений и 616 удалений

Просмотреть файл

@ -22,28 +22,6 @@ namespace Microsoft.Maui.Graphics.Native
return color;
}
public static int ToArgb(this Color target)
{
var a = (int) (target.Alpha * 255f);
var r = (int) (target.Red * 255f);
var g = (int) (target.Green * 255f);
var b = (int) (target.Blue * 255f);
int argb = a << 24 | r << 16 | g << 8 | b;
return argb;
}
public static int ToArgb(this Color target, float alpha)
{
var a = (int) (target.Alpha * 255f * alpha);
var r = (int) (target.Red * 255f);
var g = (int) (target.Green * 255f);
var b = (int) (target.Blue * 255f);
int argb = a << 24 | r << 16 | g << 8 | b;
return argb;
}
public static global::Android.Graphics.Color AsColor(this Color target)
{
var r = (int) (target.Red * 255f);

Просмотреть файл

@ -278,7 +278,7 @@ namespace Microsoft.Maui.Graphics.Native
for (int i = 0; i < vStops.Length; i++)
{
colors[i] = vStops[i].Color.ToArgb(CurrentState.Alpha);
colors[i] = vStops[i].Color.MultiplyAlpha(CurrentState.Alpha).ToInt();
stops[i] = vStops[i].Offset;
}
@ -303,7 +303,7 @@ namespace Microsoft.Maui.Graphics.Native
for (int i = 0; i < vStops.Length; i++)
{
colors[i] = vStops[i].Color.ToArgb(CurrentState.Alpha);
colors[i] = vStops[i].Color.MultiplyAlpha(CurrentState.Alpha).ToInt();
stops[i] = vStops[i].Offset;
}

Просмотреть файл

@ -1,430 +1,461 @@
using System;
using System.Diagnostics;
using System.Globalization;
namespace Microsoft.Maui.Graphics
{
[DebuggerDisplay("Red={Red}, Green={Green}, Blue={Blue}, Alpha={Alpha}")]
public class Color
{
public readonly float Red;
public readonly float Green;
public readonly float Blue;
public readonly float Alpha = 1;
public Color(float gray)
{
Red = Green = Blue = gray.Clamp(0,1);
}
public Color(float red, float green, float blue)
{
Red = red.Clamp(0, 1);
Green = green.Clamp(0, 1);
Blue = blue.Clamp(0, 1);
}
public Color(float red, float green, float blue, float alpha)
{
Red = red.Clamp(0,1);
Green = green.Clamp(0,1);
Blue = blue.Clamp(0,1);
Alpha = alpha.Clamp(0,1);
}
public Color(string colorAsHex)
{
//Remove # if present
if (colorAsHex.IndexOf('#') != -1)
colorAsHex = colorAsHex.Replace("#", "");
int red = 0;
int green = 0;
int blue = 0;
int alpha = 255;
if (colorAsHex.Length == 6)
{
//#RRGGBB
red = int.Parse(colorAsHex.Substring(0, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse(colorAsHex.Substring(2, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse(colorAsHex.Substring(4, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 3)
{
//#RGB
red = int.Parse($"{colorAsHex[0]}{colorAsHex[0]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse($"{colorAsHex[1]}{colorAsHex[1]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse($"{colorAsHex[2]}{colorAsHex[2]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 4)
{
//#RGBA
red = int.Parse($"{colorAsHex[0]}{colorAsHex[0]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse($"{colorAsHex[1]}{colorAsHex[1]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse($"{colorAsHex[2]}{colorAsHex[2]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
alpha = int.Parse($"{colorAsHex[3]}{colorAsHex[3]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 8)
{
//#RRGGBBAA
red = int.Parse(colorAsHex.Substring(0, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse(colorAsHex.Substring(2, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse(colorAsHex.Substring(4, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
alpha = int.Parse(colorAsHex.Substring(6, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
Red = red / 255f;
Green = green / 255f;
Blue = blue / 255f;
Alpha = alpha / 255f;
}
public override string ToString()
{
return $"[Color: Red={Red}, Green={Green}, Blue={Blue}, Alpha={Alpha}]";
}
public override int GetHashCode()
{
unchecked
{
int hashcode = Red.GetHashCode();
hashcode = (hashcode * 397) ^ Green.GetHashCode();
hashcode = (hashcode * 397) ^ Blue.GetHashCode();
hashcode = (hashcode * 397) ^ Alpha.GetHashCode();
return hashcode;
}
}
public override bool Equals(object obj)
{
if (obj is Color other)
return Red == other.Red && Green == other.Green && Blue == other.Blue && Alpha == other.Alpha;
return base.Equals(obj);
}
public string ToHex(bool includeAlpha = false)
{
if (includeAlpha || Alpha < 1)
return "#" + ToHex(Red) + ToHex(Green) + ToHex(Blue) + ToHex(Alpha);
return "#" + ToHex(Red) + ToHex(Green) + ToHex(Blue);
}
public Paint AsPaint()
{
return new Paint()
{
PaintType = PaintType.Solid,
StartColor = this
};
}
public Color WithAlpha(float alpha)
{
if (Math.Abs(alpha - Alpha) < Geometry.Epsilon)
return this;
return new Color(Red,Green,Blue, alpha);
}
public Color MultiplyAlpha(float multiplyBy)
{
return new Color(Red,Green,Blue, Alpha * multiplyBy);
}
public static string ToHex(float r, float g, float b)
{
return "#" + ToHex(r) + ToHex(g) + ToHex(b);
}
private static string ToHex(float value)
{
var intValue = (int)(255f * value);
var stringValue = intValue.ToString("X");
if (stringValue.Length == 1)
return "0" + stringValue;
return stringValue;
}
public float GetLuminosity()
{
float v = Math.Max(Red, Green);
v = Math.Max(v, Blue);
float m = Math.Min(Red, Green);
m = Math.Min(m, Blue);
var l = (m + v) / 2.0f;
if (l <= 0.0)
return 0;
return l;
}
public Color AddLuminosity(float delta)
{
ConvertToHsl(Red,Green,Blue, out var h, out var s, out var l);
l+= delta;
l = l.Clamp(0, 1);
return FromHsla(h,s,l,Alpha);
}
public Color WithLuminosity(float luminosity)
{
ConvertToHsl(Red, Green, Blue, out var h, out var s, out var l);
return FromHsla(h, s, luminosity, Alpha);
}
public float GetSaturation()
{
ConvertToHsl(Red, Green, Blue, out var h, out var s, out var l);
return s;
}
public Color WithSaturation(float saturation)
{
ConvertToHsl(Red, Green, Blue, out var h, out var s, out var l);
return FromHsla(h, saturation, l, Alpha);
}
public float GetHue()
{
ConvertToHsl(Red, Green, Blue, out var h, out var s, out var l);
return h;
}
public Color WithHue(float hue)
{
ConvertToHsl(Red, Green, Blue, out var h, out var s, out var l);
return FromHsla(hue, s, l, Alpha);
}
public static Color FromHex(string hex)
{
return new Color(hex);
}
public static Color FromHsva(float h, float s, float v, float a)
{
h = h.Clamp(0, 1);
s = s.Clamp(0, 1);
v = v.Clamp(0, 1);
var range = (int)(Math.Floor(h * 6)) % 6;
var f = h * 6 - Math.Floor(h * 6);
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch (range)
{
case 0:
return FromRgba(v, t, p, a);
case 1:
return FromRgba(q, v, p, a);
case 2:
return FromRgba(p, v, t, a);
case 3:
return FromRgba(p, q, v, a);
case 4:
return FromRgba(t, p, v, a);
}
return FromRgba(v, p, q, a);
}
public static Color FromUint(uint argb)
{
return FromRgba((byte)((argb & 0x00ff0000) >> 0x10), (byte)((argb & 0x0000ff00) >> 0x8), (byte)(argb & 0x000000ff), (byte)((argb & 0xff000000) >> 0x18));
}
public static Color FromRgb(byte red, byte green, byte blue)
{
return Color.FromRgba(red, green, blue, 255);
}
public static Color FromRgba(byte red, byte green, byte blue, byte alpha)
{
return new Color(red / 255f, green / 255f, blue / 255f, alpha / 255f);
}
public static Color FromRgb(float red, float green, float blue)
{
return Color.FromRgba(red, green, blue, 1);
}
public static Color FromRgb(double red, double green, double blue)
{
return Color.FromRgba(red, green, blue, 1);
}
public static Color FromRgba(float r, float g, float b, float a)
{
return new Color(r, g, b, a);
}
public static Color FromRgba(double r, double g, double b, double a)
{
return new Color((float)r, (float)g, (float)b, (float)a);
}
public static Color FromArgb(string colorAsHex)
{
//Remove # if present
if (colorAsHex.IndexOf('#') != -1)
colorAsHex = colorAsHex.Replace("#", "");
int red = 0;
int green = 0;
int blue = 0;
int alpha = 255;
if (colorAsHex.Length == 6)
{
//#RRGGBB
red = int.Parse(colorAsHex.Substring(0, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse(colorAsHex.Substring(2, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse(colorAsHex.Substring(4, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 3)
{
//#RGB
red = int.Parse($"{colorAsHex[0]}{colorAsHex[0]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse($"{colorAsHex[1]}{colorAsHex[1]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse($"{colorAsHex[2]}{colorAsHex[2]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 4)
{
//#ARGB
alpha = int.Parse($"{colorAsHex[3]}{colorAsHex[0]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
red = int.Parse($"{colorAsHex[0]}{colorAsHex[1]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse($"{colorAsHex[1]}{colorAsHex[2]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse($"{colorAsHex[2]}{colorAsHex[3]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 8)
{
//#AARRGGBB
alpha = int.Parse(colorAsHex.Substring(0, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
red = int.Parse(colorAsHex.Substring(2, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse(colorAsHex.Substring(4, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse(colorAsHex.Substring(6, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
return FromRgba(red, green, blue, alpha);
}
public static Color FromHsla(float h, float s, float l, float a = 1)
{
float red, green, blue;
ConvertToRgb(h, s, l, out red, out green, out blue);
return new Color(red, green, blue, a);
}
public static Color FromHsla(double h, double s, double l, double a = 1)
{
float red, green, blue;
ConvertToRgb((float)h, (float)s, (float)l, out red, out green, out blue);
return new Color(red, green, blue, (float)a);
}
public static Color FromHsv(float h, float s, float v)
{
return FromHsva(h, s, v, 1f);
}
public static Color FromHsva(int h, int s, int v, int a)
{
return FromHsva(h / 360f, s / 100f, v / 100f, a / 100f);
}
public static Color FromHsv(int h, int s, int v)
{
return FromHsva(h / 360f, s / 100f, v / 100f, 1f);
}
private static void ConvertToRgb(float hue, float saturation, float luminosity, out float r, out float g, out float b)
{
if (luminosity == 0)
{
r = g = b = 0;
return;
}
if (saturation == 0)
{
r = g = b = luminosity;
return;
}
float temp2 = luminosity <= 0.5f ? luminosity * (1.0f + saturation) : luminosity + saturation - luminosity * saturation;
float temp1 = 2.0f * luminosity - temp2;
var t3 = new[] { hue + 1.0f / 3.0f, hue, hue - 1.0f / 3.0f };
var clr = new float[] { 0, 0, 0 };
for (var i = 0; i < 3; i++)
{
if (t3[i] < 0)
t3[i] += 1.0f;
if (t3[i] > 1)
t3[i] -= 1.0f;
if (6.0 * t3[i] < 1.0)
clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0f;
else if (2.0 * t3[i] < 1.0)
clr[i] = temp2;
else if (3.0 * t3[i] < 2.0)
clr[i] = temp1 + (temp2 - temp1) * (2.0f / 3.0f - t3[i]) * 6.0f;
else
clr[i] = temp1;
}
r = clr[0];
g = clr[1];
b = clr[2];
}
private static void ConvertToHsl(float r, float g, float b, out float h, out float s, out float l)
{
float v = Math.Max(r, g);
v = Math.Max(v, b);
float m = Math.Min(r, g);
m = Math.Min(m, b);
l = (m + v) / 2.0f;
if (l <= 0.0)
{
h = s = l = 0;
return;
}
float vm = v - m;
s = vm;
if (s > 0.0)
{
s /= l <= 0.5f ? v + m : 2.0f - v - m;
}
else
{
h = 0;
s = 0;
return;
}
float r2 = (v - r) / vm;
float g2 = (v - g) / vm;
float b2 = (v - b) / vm;
if (r == v)
{
h = g == m ? 5.0f + b2 : 1.0f - g2;
}
else if (g == v)
{
h = b == m ? 1.0f + r2 : 3.0f - b2;
}
else
{
h = r == m ? 3.0f + g2 : 5.0f - r2;
}
h /= 6.0f;
}
}
using System.Diagnostics;
using System.Globalization;
namespace Microsoft.Maui.Graphics
{
[DebuggerDisplay("Red={Red}, Green={Green}, Blue={Blue}, Alpha={Alpha}")]
public class Color
{
public readonly float Red;
public readonly float Green;
public readonly float Blue;
public readonly float Alpha = 1;
public Color(float gray)
{
Red = Green = Blue = gray.Clamp(0, 1);
}
public Color(float red, float green, float blue)
{
Red = red.Clamp(0, 1);
Green = green.Clamp(0, 1);
Blue = blue.Clamp(0, 1);
}
public Color(float red, float green, float blue, float alpha)
{
Red = red.Clamp(0, 1);
Green = green.Clamp(0, 1);
Blue = blue.Clamp(0, 1);
Alpha = alpha.Clamp(0, 1);
}
public override string ToString()
{
return $"[Color: Red={Red}, Green={Green}, Blue={Blue}, Alpha={Alpha}]";
}
public override int GetHashCode()
{
unchecked
{
int hashcode = Red.GetHashCode();
hashcode = (hashcode * 397) ^ Green.GetHashCode();
hashcode = (hashcode * 397) ^ Blue.GetHashCode();
hashcode = (hashcode * 397) ^ Alpha.GetHashCode();
return hashcode;
}
}
public override bool Equals(object obj)
{
if (obj is Color other)
return Red == other.Red && Green == other.Green && Blue == other.Blue && Alpha == other.Alpha;
return base.Equals(obj);
}
public string ToHex(bool includeAlpha = false)
{
if (includeAlpha || Alpha < 1)
return "#" + ToHex(Alpha) + ToHex(Red) + ToHex(Green) + ToHex(Blue);
return "#" + ToHex(Red) + ToHex(Green) + ToHex(Blue);
}
public static Color FromHex(string colorAsHex) => FromArgb(colorAsHex);
public Paint AsPaint()
{
return new Paint()
{
PaintType = PaintType.Solid,
StartColor = this
};
}
public Color WithAlpha(float alpha)
{
if (Math.Abs(alpha - Alpha) < Geometry.Epsilon)
return this;
return new Color(Red, Green, Blue, alpha);
}
public Color MultiplyAlpha(float multiplyBy)
{
return new Color(Red, Green, Blue, Alpha * multiplyBy);
}
private static string ToHex(float value)
{
var intValue = (int)(255f * value);
var stringValue = intValue.ToString("X");
if (stringValue.Length == 1)
return "0" + stringValue;
return stringValue;
}
public int ToInt()
{
ToRgba(out var r, out var g, out var b, out var a);
int argb = a << 24 | r << 16 | g << 8 | b;
return argb;
}
public uint ToUint() =>(uint) ToInt();
public void ToRgb(out byte r, out byte g, out byte b) =>
ToRgba(out r, out g, out b, out _);
public void ToRgba(out byte r, out byte g, out byte b, out byte a)
{
a = (byte)(Alpha * 255f);
r = (byte)(Red * 255f);
g = (byte)(Green * 255f);
b = (byte)(Blue * 255f);
}
public float GetLuminosity()
{
float v = Math.Max(Red, Green);
v = Math.Max(v, Blue);
float m = Math.Min(Red, Green);
m = Math.Min(m, Blue);
var l = (m + v) / 2.0f;
if (l <= 0.0)
return 0;
return l;
}
public Color AddLuminosity(float delta)
{
ToHsl(out var h, out var s, out var l);
l += delta;
l = l.Clamp(0, 1);
return FromHsla(h, s, l, Alpha);
}
public Color WithLuminosity(float luminosity)
{
ToHsl(out var h, out var s, out var l);
return FromHsla(h, s, luminosity, Alpha);
}
public float GetSaturation()
{
ToHsl(out var h, out var s, out var l);
return s;
}
public Color WithSaturation(float saturation)
{
ToHsl(out var h, out var s, out var l);
return FromHsla(h, saturation, l, Alpha);
}
public float GetHue()
{
ToHsl(out var h, out var s, out var l);
return h;
}
public Color WithHue(float hue)
{
ToHsl(out var h, out var s, out var l);
return FromHsla(hue, s, l, Alpha);
}
public Color GetComplementary()
{
ToHsl(out var h, out var s, out var l);
// Add 180 (degrees) to get to the other side of the circle.
h += 0.5f;
// Ensure still within the bounds of a circle.
h %= 1.0f;
return Color.FromHsla(h, s, l);
}
public static Color FromHsva(float h, float s, float v, float a)
{
h = h.Clamp(0, 1);
s = s.Clamp(0, 1);
v = v.Clamp(0, 1);
var range = (int)(Math.Floor(h * 6)) % 6;
var f = h * 6 - Math.Floor(h * 6);
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch (range)
{
case 0:
return FromRgba(v, t, p, a);
case 1:
return FromRgba(q, v, p, a);
case 2:
return FromRgba(p, v, t, a);
case 3:
return FromRgba(p, q, v, a);
case 4:
return FromRgba(t, p, v, a);
}
return FromRgba(v, p, q, a);
}
public static Color FromUint(uint argb)
{
return FromRgba((byte)((argb & 0x00ff0000) >> 0x10), (byte)((argb & 0x0000ff00) >> 0x8), (byte)(argb & 0x000000ff), (byte)((argb & 0xff000000) >> 0x18));
}
public static Color FromInt(int argb)
{
return FromRgba((byte)((argb & 0x00ff0000) >> 0x10), (byte)((argb & 0x0000ff00) >> 0x8), (byte)(argb & 0x000000ff), (byte)((argb & 0xff000000) >> 0x18));
}
public static Color FromRgb(byte red, byte green, byte blue)
{
return Color.FromRgba(red, green, blue, 255);
}
public static Color FromRgba(byte red, byte green, byte blue, byte alpha)
{
return new Color(red / 255f, green / 255f, blue / 255f, alpha / 255f);
}
public static Color FromRgb(float red, float green, float blue)
{
return Color.FromRgba(red, green, blue, 1);
}
public static Color FromRgb(double red, double green, double blue)
{
return Color.FromRgba(red, green, blue, 1);
}
public static Color FromRgba(float r, float g, float b, float a)
{
return new Color(r, g, b, a);
}
public static Color FromRgba(double r, double g, double b, double a)
{
return new Color((float)r, (float)g, (float)b, (float)a);
}
public static Color FromRgba(string colorAsHex)
{
//Remove # if present
if (colorAsHex.IndexOf('#') != -1)
colorAsHex = colorAsHex.Replace("#", "");
int red = 0;
int green = 0;
int blue = 0;
int alpha = 255;
if (colorAsHex.Length == 6)
{
//#RRGGBB
red = int.Parse(colorAsHex.Substring(0, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse(colorAsHex.Substring(2, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse(colorAsHex.Substring(4, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 3)
{
//#RGB
red = int.Parse($"{colorAsHex[0]}{colorAsHex[0]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse($"{colorAsHex[1]}{colorAsHex[1]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse($"{colorAsHex[2]}{colorAsHex[2]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 4)
{
//#RGBA
red = int.Parse($"{colorAsHex[0]}{colorAsHex[0]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse($"{colorAsHex[1]}{colorAsHex[1]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse($"{colorAsHex[2]}{colorAsHex[2]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
alpha = int.Parse($"{colorAsHex[3]}{colorAsHex[3]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 8)
{
//#RRGGBBAA
red = int.Parse(colorAsHex.Substring(0, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse(colorAsHex.Substring(2, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse(colorAsHex.Substring(4, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
alpha = int.Parse(colorAsHex.Substring(6, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
return FromRgba(red / 255f, green / 255f, blue / 255f, alpha / 255f);
}
public static Color FromArgb(string colorAsHex)
{
//Remove # if present
if (colorAsHex.IndexOf('#') != -1)
colorAsHex = colorAsHex.Replace("#", "");
int red = 0;
int green = 0;
int blue = 0;
int alpha = 255;
if (colorAsHex.Length == 6)
{
//#RRGGBB
red = int.Parse(colorAsHex.Substring(0, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse(colorAsHex.Substring(2, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse(colorAsHex.Substring(4, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 3)
{
//#RGB
red = int.Parse($"{colorAsHex[0]}{colorAsHex[0]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse($"{colorAsHex[1]}{colorAsHex[1]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse($"{colorAsHex[2]}{colorAsHex[2]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 4)
{
//#ARGB
alpha = int.Parse($"{colorAsHex[0]}{colorAsHex[0]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
red = int.Parse($"{colorAsHex[1]}{colorAsHex[1]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse($"{colorAsHex[2]}{colorAsHex[2]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse($"{colorAsHex[3]}{colorAsHex[3]}", NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else if (colorAsHex.Length == 8)
{
//#AARRGGBB
alpha = int.Parse(colorAsHex.Substring(0, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
red = int.Parse(colorAsHex.Substring(2, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
green = int.Parse(colorAsHex.Substring(4, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
blue = int.Parse(colorAsHex.Substring(6, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
return FromRgba(red / 255f, green / 255f, blue / 255f, alpha / 255f);
}
public static Color FromHsla(float h, float s, float l, float a = 1)
{
float red, green, blue;
ConvertToRgb(h, s, l, out red, out green, out blue);
return new Color(red, green, blue, a);
}
public static Color FromHsla(double h, double s, double l, double a = 1)
{
float red, green, blue;
ConvertToRgb((float)h, (float)s, (float)l, out red, out green, out blue);
return new Color(red, green, blue, (float)a);
}
public static Color FromHsv(float h, float s, float v)
{
return FromHsva(h, s, v, 1f);
}
public static Color FromHsva(int h, int s, int v, int a)
{
return FromHsva(h / 360f, s / 100f, v / 100f, a / 100f);
}
public static Color FromHsv(int h, int s, int v)
{
return FromHsva(h / 360f, s / 100f, v / 100f, 1f);
}
private static void ConvertToRgb(float hue, float saturation, float luminosity, out float r, out float g, out float b)
{
if (luminosity == 0)
{
r = g = b = 0;
return;
}
if (saturation == 0)
{
r = g = b = luminosity;
return;
}
float temp2 = luminosity <= 0.5f ? luminosity * (1.0f + saturation) : luminosity + saturation - luminosity * saturation;
float temp1 = 2.0f * luminosity - temp2;
var t3 = new[] { hue + 1.0f / 3.0f, hue, hue - 1.0f / 3.0f };
var clr = new float[] { 0, 0, 0 };
for (var i = 0; i < 3; i++)
{
if (t3[i] < 0)
t3[i] += 1.0f;
if (t3[i] > 1)
t3[i] -= 1.0f;
if (6.0 * t3[i] < 1.0)
clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0f;
else if (2.0 * t3[i] < 1.0)
clr[i] = temp2;
else if (3.0 * t3[i] < 2.0)
clr[i] = temp1 + (temp2 - temp1) * (2.0f / 3.0f - t3[i]) * 6.0f;
else
clr[i] = temp1;
}
r = clr[0];
g = clr[1];
b = clr[2];
}
public void ToHsl(out float h, out float s, out float l)
{
var r = Red;
var g = Green;
var b = Blue;
float v = Math.Max(r, g);
v = Math.Max(v, b);
float m = Math.Min(r, g);
m = Math.Min(m, b);
l = (m + v) / 2.0f;
if (l <= 0.0)
{
h = s = l = 0;
return;
}
float vm = v - m;
s = vm;
if (s > 0.0)
{
s /= l <= 0.5f ? v + m : 2.0f - v - m;
}
else
{
h = 0;
s = 0;
return;
}
float r2 = (v - r) / vm;
float g2 = (v - g) / vm;
float b2 = (v - b) / vm;
if (r == v)
{
h = g == m ? 5.0f + b2 : 1.0f - g2;
}
else if (g == v)
{
h = b == m ? 1.0f + r2 : 3.0f - b2;
}
else
{
h = r == m ? 3.0f + g2 : 5.0f - r2;
}
h /= 6.0f;
}
}
}

Просмотреть файл

@ -2,147 +2,147 @@ namespace Microsoft.Maui.Graphics
{
public static class Colors
{
public static readonly Color Black = new Color("#000000");
public static readonly Color Navy = new Color("#000080");
public static readonly Color DarkBlue = new Color("#00008B");
public static readonly Color MediumBlue = new Color("#0000CD");
public static readonly Color Blue = new Color("#0000FF");
public static readonly Color DarkGreen = new Color("#006400");
public static readonly Color Green = new Color("#008000");
public static readonly Color Teal = new Color("#008080");
public static readonly Color DarkCyan = new Color("#008B8B");
public static readonly Color DeepSkyBlue = new Color("#00BFFF");
public static readonly Color DarkTurquoise = new Color("#00CED1");
public static readonly Color MediumSpringGreen = new Color("#00FA9A");
public static readonly Color Lime = new Color("#00FF00");
public static readonly Color SpringGreen = new Color("#00FF7F");
public static readonly Color Aqua = new Color("#00FFFF");
public static readonly Color Cyan = new Color("#00FFFF");
public static readonly Color MidnightBlue = new Color("#191970");
public static readonly Color DodgerBlue = new Color("#1E90FF");
public static readonly Color LightSeaGreen = new Color("#20B2AA");
public static readonly Color ForestGreen = new Color("#228B22");
public static readonly Color SeaGreen = new Color("#2E8B57");
public static readonly Color DarkSlateGrey = new Color("#2F4F4F");
public static readonly Color LimeGreen = new Color("#32CD32");
public static readonly Color MediumSeaGreen = new Color("#3CB371");
public static readonly Color Turquoise = new Color("#40E0D0");
public static readonly Color RoyalBlue = new Color("#4169E1");
public static readonly Color SteelBlue = new Color("#4682B4");
public static readonly Color DarkSlateBlue = new Color("#483D8B");
public static readonly Color MediumTurquoise = new Color("#48D1CC");
public static readonly Color Indigo = new Color("#4B0082");
public static readonly Color DarkOliveGreen = new Color("#556B2F");
public static readonly Color CadetBlue = new Color("#5F9EA0");
public static readonly Color CornflowerBlue = new Color("#6495ED");
public static readonly Color MediumAquaMarine = new Color("#66CDAA");
public static readonly Color DimGrey = new Color("#696969");
public static readonly Color SlateBlue = new Color("#6A5ACD");
public static readonly Color OliveDrab = new Color("#6B8E23");
public static readonly Color SlateGrey = new Color("#708090");
public static readonly Color LightSlateGrey = new Color("#778899");
public static readonly Color MediumSlateBlue = new Color("#7B68EE");
public static readonly Color LawnGreen = new Color("#7CFC00");
public static readonly Color Chartreuse = new Color("#7FFF00");
public static readonly Color Aquamarine = new Color("#7FFFD4");
public static readonly Color Maroon = new Color("#800000");
public static readonly Color Purple = new Color("#800080");
public static readonly Color Olive = new Color("#808000");
public static readonly Color Grey = new Color("#808080");
public static readonly Color SkyBlue = new Color("#87CEEB");
public static readonly Color LightSkyBlue = new Color("#87CEFA");
public static readonly Color BlueViolet = new Color("#8A2BE2");
public static readonly Color DarkRed = new Color("#8B0000");
public static readonly Color DarkMagenta = new Color("#8B008B");
public static readonly Color SaddleBrown = new Color("#8B4513");
public static readonly Color DarkSeaGreen = new Color("#8FBC8F");
public static readonly Color LightGreen = new Color("#90EE90");
public static readonly Color MediumPurple = new Color("#9370D8");
public static readonly Color DarkViolet = new Color("#9400D3");
public static readonly Color PaleGreen = new Color("#98FB98");
public static readonly Color DarkOrchid = new Color("#9932CC");
public static readonly Color YellowGreen = new Color("#9ACD32");
public static readonly Color Sienna = new Color("#A0522D");
public static readonly Color Brown = new Color("#A52A2A");
public static readonly Color DarkGrey = new Color("#A9A9A9");
public static readonly Color LightBlue = new Color("#ADD8E6");
public static readonly Color GreenYellow = new Color("#ADFF2F");
public static readonly Color PaleTurquoise = new Color("#AFEEEE");
public static readonly Color LightSteelBlue = new Color("#B0C4DE");
public static readonly Color PowderBlue = new Color("#B0E0E6");
public static readonly Color FireBrick = new Color("#B22222");
public static readonly Color DarkGoldenRod = new Color("#B8860B");
public static readonly Color MediumOrchid = new Color("#BA55D3");
public static readonly Color RosyBrown = new Color("#BC8F8F");
public static readonly Color DarkKhaki = new Color("#BDB76B");
public static readonly Color Silver = new Color("#C0C0C0");
public static readonly Color MediumVioletRed = new Color("#C71585");
public static readonly Color IndianRed = new Color("#CD5C5C");
public static readonly Color Peru = new Color("#CD853F");
public static readonly Color Chocolate = new Color("#D2691E");
public static readonly Color Tan = new Color("#D2B48C");
public static readonly Color LightGrey = new Color("#D3D3D3");
public static readonly Color PaleVioletRed = new Color("#D87093");
public static readonly Color Thistle = new Color("#D8BFD8");
public static readonly Color Orchid = new Color("#DA70D6");
public static readonly Color GoldenRod = new Color("#DAA520");
public static readonly Color Crimson = new Color("#DC143C");
public static readonly Color Gainsboro = new Color("#DCDCDC");
public static readonly Color Plum = new Color("#DDA0DD");
public static readonly Color BurlyWood = new Color("#DEB887");
public static readonly Color LightCyan = new Color("#E0FFFF");
public static readonly Color Lavender = new Color("#E6E6FA");
public static readonly Color DarkSalmon = new Color("#E9967A");
public static readonly Color Violet = new Color("#EE82EE");
public static readonly Color PaleGoldenRod = new Color("#EEE8AA");
public static readonly Color LightCoral = new Color("#F08080");
public static readonly Color Khaki = new Color("#F0E68C");
public static readonly Color AliceBlue = new Color("#F0F8FF");
public static readonly Color HoneyDew = new Color("#F0FFF0");
public static readonly Color Azure = new Color("#F0FFFF");
public static readonly Color SandyBrown = new Color("#F4A460");
public static readonly Color Wheat = new Color("#F5DEB3");
public static readonly Color Beige = new Color("#F5F5DC");
public static readonly Color WhiteSmoke = new Color("#F5F5F5");
public static readonly Color MintCream = new Color("#F5FFFA");
public static readonly Color GhostWhite = new Color("#F8F8FF");
public static readonly Color Salmon = new Color("#FA8072");
public static readonly Color AntiqueWhite = new Color("#FAEBD7");
public static readonly Color Linen = new Color("#FAF0E6");
public static readonly Color LightGoldenRodYellow = new Color("#FAFAD2");
public static readonly Color OldLace = new Color("#FDF5E6");
public static readonly Color Red = new Color("#FF0000");
public static readonly Color Fuchsia = new Color("#FF00FF");
public static readonly Color Magenta = new Color("#FF00FF");
public static readonly Color DeepPink = new Color("#FF1493");
public static readonly Color OrangeRed = new Color("#FF4500");
public static readonly Color Tomato = new Color("#FF6347");
public static readonly Color HotPink = new Color("#FF69B4");
public static readonly Color Coral = new Color("#FF7F50");
public static readonly Color DarkOrange = new Color("#FF8C00");
public static readonly Color LightSalmon = new Color("#FFA07A");
public static readonly Color Orange = new Color("#FFA500");
public static readonly Color LightPink = new Color("#FFB6C1");
public static readonly Color Pink = new Color("#FFC0CB");
public static readonly Color Gold = new Color("#FFD700");
public static readonly Color PeachPuff = new Color("#FFDAB9");
public static readonly Color NavajoWhite = new Color("#FFDEAD");
public static readonly Color Moccasin = new Color("#FFE4B5");
public static readonly Color Bisque = new Color("#FFE4C4");
public static readonly Color MistyRose = new Color("#FFE4E1");
public static readonly Color BlanchedAlmond = new Color("#FFEBCD");
public static readonly Color PapayaWhip = new Color("#FFEFD5");
public static readonly Color LavenderBlush = new Color("#FFF0F5");
public static readonly Color SeaShell = new Color("#FFF5EE");
public static readonly Color Cornsilk = new Color("#FFF8DC");
public static readonly Color LemonChiffon = new Color("#FFFACD");
public static readonly Color FloralWhite = new Color("#FFFAF0");
public static readonly Color Snow = new Color("#FFFAFA");
public static readonly Color Yellow = new Color("#FFFF00");
public static readonly Color LightYellow = new Color("#FFFFE0");
public static readonly Color Ivory = new Color("#FFFFF0");
public static readonly Color White = new Color("#FFFFFF");
public static readonly Color Transparent = new Color("#00000000");
public static readonly Color Black = Color.FromHex("#000000");
public static readonly Color Navy = Color.FromHex("#000080");
public static readonly Color DarkBlue = Color.FromHex("#00008B");
public static readonly Color MediumBlue = Color.FromHex("#0000CD");
public static readonly Color Blue = Color.FromHex("#0000FF");
public static readonly Color DarkGreen = Color.FromHex("#006400");
public static readonly Color Green = Color.FromHex("#008000");
public static readonly Color Teal = Color.FromHex("#008080");
public static readonly Color DarkCyan = Color.FromHex("#008B8B");
public static readonly Color DeepSkyBlue = Color.FromHex("#00BFFF");
public static readonly Color DarkTurquoise = Color.FromHex("#00CED1");
public static readonly Color MediumSpringGreen = Color.FromHex("#00FA9A");
public static readonly Color Lime = Color.FromHex("#00FF00");
public static readonly Color SpringGreen = Color.FromHex("#00FF7F");
public static readonly Color Aqua = Color.FromHex("#00FFFF");
public static readonly Color Cyan = Color.FromHex("#00FFFF");
public static readonly Color MidnightBlue = Color.FromHex("#191970");
public static readonly Color DodgerBlue = Color.FromHex("#1E90FF");
public static readonly Color LightSeaGreen = Color.FromHex("#20B2AA");
public static readonly Color ForestGreen = Color.FromHex("#228B22");
public static readonly Color SeaGreen = Color.FromHex("#2E8B57");
public static readonly Color DarkSlateGrey = Color.FromHex("#2F4F4F");
public static readonly Color LimeGreen = Color.FromHex("#32CD32");
public static readonly Color MediumSeaGreen = Color.FromHex("#3CB371");
public static readonly Color Turquoise = Color.FromHex("#40E0D0");
public static readonly Color RoyalBlue = Color.FromHex("#4169E1");
public static readonly Color SteelBlue = Color.FromHex("#4682B4");
public static readonly Color DarkSlateBlue = Color.FromHex("#483D8B");
public static readonly Color MediumTurquoise = Color.FromHex("#48D1CC");
public static readonly Color Indigo = Color.FromHex("#4B0082");
public static readonly Color DarkOliveGreen = Color.FromHex("#556B2F");
public static readonly Color CadetBlue = Color.FromHex("#5F9EA0");
public static readonly Color CornflowerBlue = Color.FromHex("#6495ED");
public static readonly Color MediumAquaMarine = Color.FromHex("#66CDAA");
public static readonly Color DimGrey = Color.FromHex("#696969");
public static readonly Color SlateBlue = Color.FromHex("#6A5ACD");
public static readonly Color OliveDrab = Color.FromHex("#6B8E23");
public static readonly Color SlateGrey = Color.FromHex("#708090");
public static readonly Color LightSlateGrey = Color.FromHex("#778899");
public static readonly Color MediumSlateBlue = Color.FromHex("#7B68EE");
public static readonly Color LawnGreen = Color.FromHex("#7CFC00");
public static readonly Color Chartreuse = Color.FromHex("#7FFF00");
public static readonly Color Aquamarine = Color.FromHex("#7FFFD4");
public static readonly Color Maroon = Color.FromHex("#800000");
public static readonly Color Purple = Color.FromHex("#800080");
public static readonly Color Olive = Color.FromHex("#808000");
public static readonly Color Grey = Color.FromHex("#808080");
public static readonly Color SkyBlue = Color.FromHex("#87CEEB");
public static readonly Color LightSkyBlue = Color.FromHex("#87CEFA");
public static readonly Color BlueViolet = Color.FromHex("#8A2BE2");
public static readonly Color DarkRed = Color.FromHex("#8B0000");
public static readonly Color DarkMagenta = Color.FromHex("#8B008B");
public static readonly Color SaddleBrown = Color.FromHex("#8B4513");
public static readonly Color DarkSeaGreen = Color.FromHex("#8FBC8F");
public static readonly Color LightGreen = Color.FromHex("#90EE90");
public static readonly Color MediumPurple = Color.FromHex("#9370D8");
public static readonly Color DarkViolet = Color.FromHex("#9400D3");
public static readonly Color PaleGreen = Color.FromHex("#98FB98");
public static readonly Color DarkOrchid = Color.FromHex("#9932CC");
public static readonly Color YellowGreen = Color.FromHex("#9ACD32");
public static readonly Color Sienna = Color.FromHex("#A0522D");
public static readonly Color Brown = Color.FromHex("#A52A2A");
public static readonly Color DarkGrey = Color.FromHex("#A9A9A9");
public static readonly Color LightBlue = Color.FromHex("#ADD8E6");
public static readonly Color GreenYellow = Color.FromHex("#ADFF2F");
public static readonly Color PaleTurquoise = Color.FromHex("#AFEEEE");
public static readonly Color LightSteelBlue = Color.FromHex("#B0C4DE");
public static readonly Color PowderBlue = Color.FromHex("#B0E0E6");
public static readonly Color FireBrick = Color.FromHex("#B22222");
public static readonly Color DarkGoldenRod = Color.FromHex("#B8860B");
public static readonly Color MediumOrchid = Color.FromHex("#BA55D3");
public static readonly Color RosyBrown = Color.FromHex("#BC8F8F");
public static readonly Color DarkKhaki = Color.FromHex("#BDB76B");
public static readonly Color Silver = Color.FromHex("#C0C0C0");
public static readonly Color MediumVioletRed = Color.FromHex("#C71585");
public static readonly Color IndianRed = Color.FromHex("#CD5C5C");
public static readonly Color Peru = Color.FromHex("#CD853F");
public static readonly Color Chocolate = Color.FromHex("#D2691E");
public static readonly Color Tan = Color.FromHex("#D2B48C");
public static readonly Color LightGrey = Color.FromHex("#D3D3D3");
public static readonly Color PaleVioletRed = Color.FromHex("#D87093");
public static readonly Color Thistle = Color.FromHex("#D8BFD8");
public static readonly Color Orchid = Color.FromHex("#DA70D6");
public static readonly Color GoldenRod = Color.FromHex("#DAA520");
public static readonly Color Crimson = Color.FromHex("#DC143C");
public static readonly Color Gainsboro = Color.FromHex("#DCDCDC");
public static readonly Color Plum = Color.FromHex("#DDA0DD");
public static readonly Color BurlyWood = Color.FromHex("#DEB887");
public static readonly Color LightCyan = Color.FromHex("#E0FFFF");
public static readonly Color Lavender = Color.FromHex("#E6E6FA");
public static readonly Color DarkSalmon = Color.FromHex("#E9967A");
public static readonly Color Violet = Color.FromHex("#EE82EE");
public static readonly Color PaleGoldenRod = Color.FromHex("#EEE8AA");
public static readonly Color LightCoral = Color.FromHex("#F08080");
public static readonly Color Khaki = Color.FromHex("#F0E68C");
public static readonly Color AliceBlue = Color.FromHex("#F0F8FF");
public static readonly Color HoneyDew = Color.FromHex("#F0FFF0");
public static readonly Color Azure = Color.FromHex("#F0FFFF");
public static readonly Color SandyBrown = Color.FromHex("#F4A460");
public static readonly Color Wheat = Color.FromHex("#F5DEB3");
public static readonly Color Beige = Color.FromHex("#F5F5DC");
public static readonly Color WhiteSmoke = Color.FromHex("#F5F5F5");
public static readonly Color MintCream = Color.FromHex("#F5FFFA");
public static readonly Color GhostWhite = Color.FromHex("#F8F8FF");
public static readonly Color Salmon = Color.FromHex("#FA8072");
public static readonly Color AntiqueWhite = Color.FromHex("#FAEBD7");
public static readonly Color Linen = Color.FromHex("#FAF0E6");
public static readonly Color LightGoldenRodYellow = Color.FromHex("#FAFAD2");
public static readonly Color OldLace = Color.FromHex("#FDF5E6");
public static readonly Color Red = Color.FromHex("#FF0000");
public static readonly Color Fuchsia = Color.FromHex("#FF00FF");
public static readonly Color Magenta = Color.FromHex("#FF00FF");
public static readonly Color DeepPink = Color.FromHex("#FF1493");
public static readonly Color OrangeRed = Color.FromHex("#FF4500");
public static readonly Color Tomato = Color.FromHex("#FF6347");
public static readonly Color HotPink = Color.FromHex("#FF69B4");
public static readonly Color Coral = Color.FromHex("#FF7F50");
public static readonly Color DarkOrange = Color.FromHex("#FF8C00");
public static readonly Color LightSalmon = Color.FromHex("#FFA07A");
public static readonly Color Orange = Color.FromHex("#FFA500");
public static readonly Color LightPink = Color.FromHex("#FFB6C1");
public static readonly Color Pink = Color.FromHex("#FFC0CB");
public static readonly Color Gold = Color.FromHex("#FFD700");
public static readonly Color PeachPuff = Color.FromHex("#FFDAB9");
public static readonly Color NavajoWhite = Color.FromHex("#FFDEAD");
public static readonly Color Moccasin = Color.FromHex("#FFE4B5");
public static readonly Color Bisque = Color.FromHex("#FFE4C4");
public static readonly Color MistyRose = Color.FromHex("#FFE4E1");
public static readonly Color BlanchedAlmond = Color.FromHex("#FFEBCD");
public static readonly Color PapayaWhip = Color.FromHex("#FFEFD5");
public static readonly Color LavenderBlush = Color.FromHex("#FFF0F5");
public static readonly Color SeaShell = Color.FromHex("#FFF5EE");
public static readonly Color Cornsilk = Color.FromHex("#FFF8DC");
public static readonly Color LemonChiffon = Color.FromHex("#FFFACD");
public static readonly Color FloralWhite = Color.FromHex("#FFFAF0");
public static readonly Color Snow = Color.FromHex("#FFFAFA");
public static readonly Color Yellow = Color.FromHex("#FFFF00");
public static readonly Color LightYellow = Color.FromHex("#FFFFE0");
public static readonly Color Ivory = Color.FromHex("#FFFFF0");
public static readonly Color White = Color.FromHex("#FFFFFF");
public static readonly Color Transparent = Color.FromHex("#00000000");
public static readonly Color DarkGoldenrod = Color.FromRgb(184, 134, 11);
public static readonly Color DarkGray = Color.FromRgb(169, 169, 169);
public static readonly Color DarkSlateGray = Color.FromRgb(47, 79, 79);

Просмотреть файл

@ -202,11 +202,11 @@ namespace Xamarin.Forms.Core.UnitTests
public void TestFromHex()
{
var color = Color.FromRgb(138, 43, 226);
Assert.Equal(color, new Color("8a2be2"));
Assert.Equal(color, Color.FromHex("8a2be2"));
Assert.Equal(Color.FromRgba(138, 43, 226, 128), new Color("#8a2be280"));
Assert.Equal(Color.FromHex("#aabbcc"), new Color("#abc"));
Assert.Equal(Color.FromHex("#aabbccdd"), new Color("#abcd"));
Assert.Equal(Color.FromRgba(138, 43, 226, 128), Color.FromHex("#808a2be2"));
Assert.Equal(Color.FromHex("#aabbcc"), Color.FromHex("#abc"));
Assert.Equal(Color.FromHex("#aabbccdd"), Color.FromHex("#abcd"));
}
[Fact]
@ -219,7 +219,7 @@ namespace Xamarin.Forms.Core.UnitTests
var colorHsl = Color.FromHsla(240, 1, 1);
Assert.Equal(Color.FromHex(colorHsl.ToHex()), colorHsl);
var colorHsla = Color.FromHsla(240, 1, 1, .1f);
var hexFromHsla = new Color(colorHsla.ToHex());
var hexFromHsla = Color.FromHex(colorHsla.ToHex());
Assert.Equal(hexFromHsla.Alpha, colorHsla.Alpha,2);
Assert.Equal(hexFromHsla.Red, colorHsla.Red,3);
Assert.Equal(hexFromHsla.Green, colorHsla.Green,3);
@ -295,27 +295,53 @@ namespace Xamarin.Forms.Core.UnitTests
Assert.Equal(Colors.White, Color.FromRgb(255, 255, 255));
}
[Fact]
public void TestFromUint()
{
var expectedColor = new Color(1, 0.65f, 0, 1);
[Fact]
public void TestFromUint()
{
var expectedColor = new Color(1, 0.65f, 0, 1);
// Convert the expected color to a uint (argb)
var blue = (int)(expectedColor.Blue * 255);
var red = (int)(expectedColor.Red * 255);
var green = (int)(expectedColor.Green * 255);
var alpha = (int)(expectedColor.Alpha * 255);
// Convert the expected color to a uint (argb)
var blue = (int)(expectedColor.Blue * 255);
var red = (int)(expectedColor.Red * 255);
var green = (int)(expectedColor.Green * 255);
var alpha = (int)(expectedColor.Alpha * 255);
uint argb = (uint)(blue | (green << 8) | (red << 16) | (alpha << 24));
uint argb = (uint)(blue | (green << 8) | (red << 16) | (alpha << 24));
// Create a new color from the uint
var fromUint = Color.FromUint(argb);
// Create a new color from the uint
var fromUint = Color.FromUint(argb);
// Verify the components
Assert.Equal(expectedColor.Alpha, fromUint.Alpha, 2);
Assert.Equal(expectedColor.Red, fromUint.Red, 2);
Assert.Equal(expectedColor.Green, fromUint.Green, 2);
Assert.Equal(expectedColor.Blue, fromUint.Blue, 2);
}
// Verify the components
Assert.Equal(expectedColor.Alpha, fromUint.Alpha, 2);
Assert.Equal(expectedColor.Red, fromUint.Red, 2);
Assert.Equal(expectedColor.Green, fromUint.Green, 2);
Assert.Equal(expectedColor.Blue, fromUint.Blue, 2);
}
[Fact]
public void ToUInt()
{
var color = Color.FromRgba(255, 122, 15, 255);
var i = color.ToUint();
Assert.Equal(4294933007U, i);
}
[Theory]
[InlineData("#FF0000", "#00FFFF")] // Red & Cyan
[InlineData("#00FF00", "#FF00FF")] // Green & Fuchsia
[InlineData("#0000FF", "#FFFF00")] // Blue & Yellow
[InlineData("#0AF56C", "#F50A93")] // Lime green & bright purple (but with no limit values)
public void GetComplementary(string original, string expected)
{
var orig = Color.FromHex(original);
var expectedComplement = Color.FromHex(expected);
var comp = orig.GetComplementary();
Assert.Equal(expectedComplement.Alpha, comp.Alpha, 3);
Assert.Equal(expectedComplement.Red, comp.Red, 3);
Assert.Equal(expectedComplement.Green, comp.Green, 3);
Assert.Equal(expectedComplement.Blue, comp.Blue, 3);
}
}
}