diff --git a/src/ImageSharp/Color.cs b/src/ImageSharp/Color.cs deleted file mode 100644 index 6dae76c34d..0000000000 --- a/src/ImageSharp/Color.cs +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; - -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp -{ - public readonly partial struct Color - { - private readonly Rgba64 data; - - public Color(Rgba64 pixel) - { - this.data = pixel; - } - - public Color(Rgba32 pixel) - { - this.data = new Rgba64(pixel); - } - - public Color(Argb32 pixel) - { - this.data = new Rgba64(pixel); - } - - public Color(Bgra32 pixel) - { - this.data = new Rgba64(pixel); - } - - public Color(Rgb24 pixel) - { - this.data = new Rgba64(pixel); - } - - public Color(Bgr24 pixel) - { - this.data = new Rgba64(pixel); - } - - public Color(Vector4 vector) - { - this.data = new Rgba64(vector); - } - - public static implicit operator Color(Rgba64 source) => new Color(source); - - public static implicit operator Color(Rgba32 source) => new Color(source); - - public static implicit operator Color(Bgra32 source) => new Color(source); - - public static implicit operator Color(Argb32 source) => new Color(source); - - public static implicit operator Color(Rgb24 source) => new Color(source); - - public static implicit operator Color(Bgr24 source) => new Color(source); - - public static implicit operator Rgba64(Color color) => color.data; - - public static implicit operator Rgba32(Color color) => color.data.ToRgba32(); - - public static implicit operator Bgra32(Color color) => color.data.ToBgra32(); - - public static implicit operator Argb32(Color color) => color.data.ToArgb32(); - - public static implicit operator Rgb24(Color color) => color.data.ToRgb24(); - - public static implicit operator Bgr24(Color color) => color.data.ToBgr24(); - - public static Color FromRgba(byte r, byte g, byte b, byte a) => new Color(new Rgba32(r, g, b, a)); - - public TPixel ToPixel() - where TPixel : struct, IPixel - { - TPixel pixel = default; - pixel.FromRgba64(this.data); - return pixel; - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Source/ImageSharp/Color.NamedColors.cs b/src/ImageSharp/Color/Color.NamedColors.cs similarity index 99% rename from src/ImageSharp/Source/ImageSharp/Color.NamedColors.cs rename to src/ImageSharp/Color/Color.NamedColors.cs index e00329d89a..a712f85dab 100644 --- a/src/ImageSharp/Source/ImageSharp/Color.NamedColors.cs +++ b/src/ImageSharp/Color/Color.NamedColors.cs @@ -1,5 +1,6 @@ // // Copyright (c) Six Labors and contributors. // // Licensed under the Apache License, Version 2.0. + namespace SixLabors.ImageSharp { public readonly partial struct Color diff --git a/src/ImageSharp/Color/Color.WebSafePalette.cs b/src/ImageSharp/Color/Color.WebSafePalette.cs new file mode 100644 index 0000000000..13720ec493 --- /dev/null +++ b/src/ImageSharp/Color/Color.WebSafePalette.cs @@ -0,0 +1,163 @@ +// // Copyright (c) Six Labors and contributors. +// // Licensed under the Apache License, Version 2.0. + +using System; + +namespace SixLabors.ImageSharp +{ + public partial struct Color + { + private static readonly Lazy WebSafePaletteLazy = new Lazy(CreateWebSafePalette, true); + + /// + /// Gets a collection of named, web safe, colors as defined in the CSS Color Module Level 4. + /// + public static ReadOnlySpan WebSafePalette => WebSafePaletteLazy.Value; + + private static Color[] CreateWebSafePalette() => new[] + { + AliceBlue, + AntiqueWhite, + Aqua, + Aquamarine, + Azure, + Beige, + Bisque, + Black, + BlanchedAlmond, + Blue, + BlueViolet, + Brown, + BurlyWood, + CadetBlue, + Chartreuse, + Chocolate, + Coral, + CornflowerBlue, + Cornsilk, + Crimson, + Cyan, + DarkBlue, + DarkCyan, + DarkGoldenrod, + DarkGray, + DarkGreen, + DarkKhaki, + DarkMagenta, + DarkOliveGreen, + DarkOrange, + DarkOrchid, + DarkRed, + DarkSalmon, + DarkSeaGreen, + DarkSlateBlue, + DarkSlateGray, + DarkTurquoise, + DarkViolet, + DeepPink, + DeepSkyBlue, + DimGray, + DodgerBlue, + Firebrick, + FloralWhite, + ForestGreen, + Fuchsia, + Gainsboro, + GhostWhite, + Gold, + Goldenrod, + Gray, + Green, + GreenYellow, + Honeydew, + HotPink, + IndianRed, + Indigo, + Ivory, + Khaki, + Lavender, + LavenderBlush, + LawnGreen, + LemonChiffon, + LightBlue, + LightCoral, + LightCyan, + LightGoldenrodYellow, + LightGray, + LightGreen, + LightPink, + LightSalmon, + LightSeaGreen, + LightSkyBlue, + LightSlateGray, + LightSteelBlue, + LightYellow, + Lime, + LimeGreen, + Linen, + Magenta, + Maroon, + MediumAquamarine, + MediumBlue, + MediumOrchid, + MediumPurple, + MediumSeaGreen, + MediumSlateBlue, + MediumSpringGreen, + MediumTurquoise, + MediumVioletRed, + MidnightBlue, + MintCream, + MistyRose, + Moccasin, + NavajoWhite, + Navy, + OldLace, + Olive, + OliveDrab, + Orange, + OrangeRed, + Orchid, + PaleGoldenrod, + PaleGreen, + PaleTurquoise, + PaleVioletRed, + PapayaWhip, + PeachPuff, + Peru, + Pink, + Plum, + PowderBlue, + Purple, + RebeccaPurple, + Red, + RosyBrown, + RoyalBlue, + SaddleBrown, + Salmon, + SandyBrown, + SeaGreen, + SeaShell, + Sienna, + Silver, + SkyBlue, + SlateBlue, + SlateGray, + Snow, + SpringGreen, + SteelBlue, + Tan, + Teal, + Thistle, + Tomato, + Transparent, + Turquoise, + Violet, + Wheat, + White, + WhiteSmoke, + Yellow, + YellowGreen + }; + } +} \ No newline at end of file diff --git a/src/ImageSharp/Color/Color.WernerPalette.cs b/src/ImageSharp/Color/Color.WernerPalette.cs new file mode 100644 index 0000000000..45823479e1 --- /dev/null +++ b/src/ImageSharp/Color/Color.WernerPalette.cs @@ -0,0 +1,132 @@ +// // Copyright (c) Six Labors and contributors. +// // Licensed under the Apache License, Version 2.0. + +using System; + +namespace SixLabors.ImageSharp +{ + public partial struct Color + { + private static readonly Lazy WernerPaletteLazy = new Lazy(CreateWernerPalette, true); + + /// + /// Gets a collection of colors as defined in the original second edition of Werner’s Nomenclature of Colours 1821. + /// The hex codes were collected and defined by Nicholas Rougeux . + /// + public static ReadOnlySpan WernerPalette => WernerPaletteLazy.Value; + + private static Color[] CreateWernerPalette() => new[] + { + FromHex("#f1e9cd"), + FromHex("#f2e7cf"), + FromHex("#ece6d0"), + FromHex("#f2eacc"), + FromHex("#f3e9ca"), + FromHex("#f2ebcd"), + FromHex("#e6e1c9"), + FromHex("#e2ddc6"), + FromHex("#cbc8b7"), + FromHex("#bfbbb0"), + FromHex("#bebeb3"), + FromHex("#b7b5ac"), + FromHex("#bab191"), + FromHex("#9c9d9a"), + FromHex("#8a8d84"), + FromHex("#5b5c61"), + FromHex("#555152"), + FromHex("#413f44"), + FromHex("#454445"), + FromHex("#423937"), + FromHex("#433635"), + FromHex("#252024"), + FromHex("#241f20"), + FromHex("#281f3f"), + FromHex("#1c1949"), + FromHex("#4f638d"), + FromHex("#383867"), + FromHex("#5c6b8f"), + FromHex("#657abb"), + FromHex("#6f88af"), + FromHex("#7994b5"), + FromHex("#6fb5a8"), + FromHex("#719ba2"), + FromHex("#8aa1a6"), + FromHex("#d0d5d3"), + FromHex("#8590ae"), + FromHex("#3a2f52"), + FromHex("#39334a"), + FromHex("#6c6d94"), + FromHex("#584c77"), + FromHex("#533552"), + FromHex("#463759"), + FromHex("#bfbac0"), + FromHex("#77747f"), + FromHex("#4a475c"), + FromHex("#b8bfaf"), + FromHex("#b2b599"), + FromHex("#979c84"), + FromHex("#5d6161"), + FromHex("#61ac86"), + FromHex("#a4b6a7"), + FromHex("#adba98"), + FromHex("#93b778"), + FromHex("#7d8c55"), + FromHex("#33431e"), + FromHex("#7c8635"), + FromHex("#8e9849"), + FromHex("#c2c190"), + FromHex("#67765b"), + FromHex("#ab924b"), + FromHex("#c8c76f"), + FromHex("#ccc050"), + FromHex("#ebdd99"), + FromHex("#ab9649"), + FromHex("#dbc364"), + FromHex("#e6d058"), + FromHex("#ead665"), + FromHex("#d09b2c"), + FromHex("#a36629"), + FromHex("#a77d35"), + FromHex("#f0d696"), + FromHex("#d7c485"), + FromHex("#f1d28c"), + FromHex("#efcc83"), + FromHex("#f3daa7"), + FromHex("#dfa837"), + FromHex("#ebbc71"), + FromHex("#d17c3f"), + FromHex("#92462f"), + FromHex("#be7249"), + FromHex("#bb603c"), + FromHex("#c76b4a"), + FromHex("#a75536"), + FromHex("#b63e36"), + FromHex("#b5493a"), + FromHex("#cd6d57"), + FromHex("#711518"), + FromHex("#e9c49d"), + FromHex("#eedac3"), + FromHex("#eecfbf"), + FromHex("#ce536b"), + FromHex("#b74a70"), + FromHex("#b7757c"), + FromHex("#612741"), + FromHex("#7a4848"), + FromHex("#3f3033"), + FromHex("#8d746f"), + FromHex("#4d3635"), + FromHex("#6e3b31"), + FromHex("#864735"), + FromHex("#553d3a"), + FromHex("#613936"), + FromHex("#7a4b3a"), + FromHex("#946943"), + FromHex("#c39e6d"), + FromHex("#513e32"), + FromHex("#8b7859"), + FromHex("#9b856b"), + FromHex("#766051"), + FromHex("#453b32") + }; + } +} \ No newline at end of file diff --git a/src/ImageSharp/Color/Color.cs b/src/ImageSharp/Color/Color.cs new file mode 100644 index 0000000000..3e20cafbd3 --- /dev/null +++ b/src/ImageSharp/Color/Color.cs @@ -0,0 +1,185 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Buffers.Binary; +using System.Globalization; +using System.Numerics; + +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp +{ + public readonly partial struct Color : IEquatable + { + private readonly Rgba64 data; + + public Color(Rgba64 pixel) + { + this.data = pixel; + } + + public Color(Rgba32 pixel) + { + this.data = new Rgba64(pixel); + } + + public Color(Argb32 pixel) + { + this.data = new Rgba64(pixel); + } + + public Color(Bgra32 pixel) + { + this.data = new Rgba64(pixel); + } + + public Color(Rgb24 pixel) + { + this.data = new Rgba64(pixel); + } + + public Color(Bgr24 pixel) + { + this.data = new Rgba64(pixel); + } + + public Color(Vector4 vector) + { + this.data = new Rgba64(vector); + } + + public static implicit operator Color(Rgba64 source) => new Color(source); + + public static implicit operator Color(Rgba32 source) => new Color(source); + + public static implicit operator Color(Bgra32 source) => new Color(source); + + public static implicit operator Color(Argb32 source) => new Color(source); + + public static implicit operator Color(Rgb24 source) => new Color(source); + + public static implicit operator Color(Bgr24 source) => new Color(source); + + public static implicit operator Rgba64(Color color) => color.data; + + public static implicit operator Rgba32(Color color) => color.data.ToRgba32(); + + public static implicit operator Bgra32(Color color) => color.data.ToBgra32(); + + public static implicit operator Argb32(Color color) => color.data.ToArgb32(); + + public static implicit operator Rgb24(Color color) => color.data.ToRgb24(); + + public static implicit operator Bgr24(Color color) => color.data.ToBgr24(); + + public static bool operator ==(Color left, Color right) + { + return left.Equals(right); + } + + public static bool operator !=(Color left, Color right) + { + return !left.Equals(right); + } + + public static Color FromRgba(byte r, byte g, byte b, byte a) => new Color(new Rgba32(r, g, b, a)); + + /// + /// Creates a new instance from the string representing a color in hexadecimal form. + /// + /// + /// The hexadecimal representation of the combined color components arranged + /// in rgb, rgba, rrggbb, or rrggbbaa format to match web syntax. + /// + /// Returns a that represents the color defined by the provided RGBA hex string. + public static Color FromHex(string hex) + { + Guard.NotNullOrWhiteSpace(hex, nameof(hex)); + + hex = ToRgbaHex(hex); + + if (hex is null || !uint.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out uint packedValue)) + { + throw new ArgumentException("Hexadecimal string is not in the correct format.", nameof(hex)); + } + + var rgba = new Rgba32(BinaryPrimitives.ReverseEndianness(packedValue)); + return new Color(rgba); + } + + /// + /// Gets the hexadecimal representation of the color instance in rrggbbaa form. + /// + /// A hexadecimal string representation of the value. + public string ToHex() => this.data.ToRgba32().ToHex(); + + /// + public override string ToString() => this.ToHex(); + + /// + /// Converts the specified hex value to an rrggbbaa hex value. + /// + /// The hex value to convert. + /// + /// A rrggbbaa hex value. + /// + private static string ToRgbaHex(string hex) + { + if (hex[0] == '#') + { + hex = hex.Substring(1); + } + + if (hex.Length == 8) + { + return hex; + } + + if (hex.Length == 6) + { + return hex + "FF"; + } + + if (hex.Length < 3 || hex.Length > 4) + { + return null; + } + + char r = hex[0]; + char g = hex[1]; + char b = hex[2]; + char a = hex.Length == 3 ? 'F' : hex[3]; + + return new string(new[] { r, r, g, g, b, b, a, a }); + } + + public TPixel ToPixel() + where TPixel : struct, IPixel + { + TPixel pixel = default; + pixel.FromRgba64(this.data); + return pixel; + } + + public bool Equals(Color other) + { + return this.data.PackedValue == other.data.PackedValue; + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) + { + return false; + } + return obj is Color other && this.Equals(other); + } + + /// + public override int GetHashCode() + { + return this.data.PackedValue.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/ImageSharp.csproj.DotSettings b/src/ImageSharp/ImageSharp.csproj.DotSettings index e446893e94..e43e92ddce 100644 --- a/src/ImageSharp/ImageSharp.csproj.DotSettings +++ b/src/ImageSharp/ImageSharp.csproj.DotSettings @@ -1,4 +1,5 @@  + True True True True diff --git a/tests/ImageSharp.Tests/Color/ColorTests.CastFrom.cs b/tests/ImageSharp.Tests/Color/ColorTests.CastFrom.cs new file mode 100644 index 0000000000..22c6d55ad8 --- /dev/null +++ b/tests/ImageSharp.Tests/Color/ColorTests.CastFrom.cs @@ -0,0 +1,93 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.PixelFormats; + +using Xunit; + +namespace SixLabors.ImageSharp.Tests +{ + public partial class ColorTests + { + public class CastFrom + { + [Fact] + public void Rgba64() + { + Rgba64 source = new Rgba64(100, 2222, 3333, 4444); + + // Act: + Color color = source; + + // Assert: + Rgba64 data = color.ToPixel(); + Assert.Equal(source, data); + } + + [Fact] + public void Rgba32() + { + Rgba32 source = new Rgba32(1, 22, 33, 231); + + // Act: + Color color = source; + + // Assert: + Rgba32 data = color.ToPixel(); + Assert.Equal(source, data); + } + + [Fact] + public void Argb32() + { + Argb32 source = new Argb32(1, 22, 33, 231); + + // Act: + Color color = source; + + // Assert: + Argb32 data = color.ToPixel(); + Assert.Equal(source, data); + } + + [Fact] + public void Bgra32() + { + Bgra32 source = new Bgra32(1, 22, 33, 231); + + // Act: + Color color = source; + + // Assert: + Bgra32 data = color.ToPixel(); + Assert.Equal(source, data); + } + + [Fact] + public void Rgb24() + { + Rgb24 source = new Rgb24(1, 22, 231); + + // Act: + Color color = source; + + // Assert: + Rgb24 data = color.ToPixel(); + Assert.Equal(source, data); + } + + [Fact] + public void Bgr24() + { + Bgr24 source = new Bgr24(1, 22, 231); + + // Act: + Color color = source; + + // Assert: + Bgr24 data = color.ToPixel(); + Assert.Equal(source, data); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Color/ColorTests.CastTo.cs b/tests/ImageSharp.Tests/Color/ColorTests.CastTo.cs new file mode 100644 index 0000000000..d44472b28a --- /dev/null +++ b/tests/ImageSharp.Tests/Color/ColorTests.CastTo.cs @@ -0,0 +1,93 @@ +// // Copyright (c) Six Labors and contributors. +// // Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.PixelFormats; + +using Xunit; + +namespace SixLabors.ImageSharp.Tests +{ + public partial class ColorTests + { + public class CastTo + { + [Fact] + public void Rgba64() + { + Rgba64 source = new Rgba64(100, 2222, 3333, 4444); + + // Act: + Color color = new Color(source); + + // Assert: + Rgba64 data = color; + Assert.Equal(source, data); + } + + [Fact] + public void Rgba32() + { + Rgba32 source = new Rgba32(1, 22, 33, 231); + + // Act: + Color color = new Color(source); + + // Assert: + Rgba32 data = color; + Assert.Equal(source, data); + } + + [Fact] + public void Argb32() + { + Argb32 source = new Argb32(1, 22, 33, 231); + + // Act: + Color color = new Color(source); + + // Assert: + Argb32 data = color; + Assert.Equal(source, data); + } + + [Fact] + public void Bgra32() + { + Bgra32 source = new Bgra32(1, 22, 33, 231); + + // Act: + Color color = new Color(source); + + // Assert: + Bgra32 data = color; + Assert.Equal(source, data); + } + + [Fact] + public void Rgb24() + { + Rgb24 source = new Rgb24(1, 22, 231); + + // Act: + Color color = new Color(source); + + // Assert: + Rgb24 data = color; + Assert.Equal(source, data); + } + + [Fact] + public void Bgr24() + { + Bgr24 source = new Bgr24(1, 22, 231); + + // Act: + Color color = new Color(source); + + // Assert: + Bgr24 data = color; + Assert.Equal(source, data); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Color/ColorTests.ConstructFrom.cs b/tests/ImageSharp.Tests/Color/ColorTests.ConstructFrom.cs new file mode 100644 index 0000000000..9d21cd80c5 --- /dev/null +++ b/tests/ImageSharp.Tests/Color/ColorTests.ConstructFrom.cs @@ -0,0 +1,93 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.PixelFormats; + +using Xunit; + +namespace SixLabors.ImageSharp.Tests +{ + public partial class ColorTests + { + public class ConstructFrom + { + [Fact] + public void Rgba64() + { + Rgba64 source = new Rgba64(100, 2222, 3333, 4444); + + // Act: + Color color = new Color(source); + + // Assert: + Rgba64 data = color.ToPixel(); + Assert.Equal(source, data); + } + + [Fact] + public void Rgba32() + { + Rgba32 source = new Rgba32(1, 22, 33, 231); + + // Act: + Color color = new Color(source); + + // Assert: + Rgba32 data = color.ToPixel(); + Assert.Equal(source, data); + } + + [Fact] + public void Argb32() + { + Argb32 source = new Argb32(1, 22, 33, 231); + + // Act: + Color color = new Color(source); + + // Assert: + Argb32 data = color.ToPixel(); + Assert.Equal(source, data); + } + + [Fact] + public void Bgra32() + { + Bgra32 source = new Bgra32(1, 22, 33, 231); + + // Act: + Color color = new Color(source); + + // Assert: + Bgra32 data = color.ToPixel(); + Assert.Equal(source, data); + } + + [Fact] + public void Rgb24() + { + Rgb24 source = new Rgb24(1, 22, 231); + + // Act: + Color color = new Color(source); + + // Assert: + Rgb24 data = color.ToPixel(); + Assert.Equal(source, data); + } + + [Fact] + public void Bgr24() + { + Bgr24 source = new Bgr24(1, 22, 231); + + // Act: + Color color = new Color(source); + + // Assert: + Bgr24 data = color.ToPixel(); + Assert.Equal(source, data); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Color/ColorTests.cs b/tests/ImageSharp.Tests/Color/ColorTests.cs new file mode 100644 index 0000000000..729ef94e3c --- /dev/null +++ b/tests/ImageSharp.Tests/Color/ColorTests.cs @@ -0,0 +1,97 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Linq; +using System.Reflection; + +using SixLabors.ImageSharp.PixelFormats; + +using Xunit; + +namespace SixLabors.ImageSharp.Tests +{ + public partial class ColorTests + { + [Fact] + public void Equality_WhenTrue() + { + Color c1 = new Rgba64(100, 2000, 3000, 40000); + Color c2 = new Rgba64(100, 2000, 3000, 40000); + + Assert.True(c1.Equals(c2)); + Assert.True(c1 == c2); + Assert.False(c1 != c2); + Assert.True(c1.GetHashCode() == c2.GetHashCode()); + } + + [Fact] + public void Equality_WhenFalse() + { + Color c1 = new Rgba64(100, 2000, 3000, 40000); + Color c2 = new Rgba64(101, 2000, 3000, 40000); + Color c3 = new Rgba64(100, 2000, 3000, 40001); + + Assert.False(c1.Equals(c2)); + Assert.False(c2.Equals(c3)); + Assert.False(c3.Equals(c1)); + + Assert.False(c1 == c2); + Assert.True(c1 != c2); + } + + [Fact] + public void ToHex() + { + string expected = "ABCD1234"; + Color color = Color.FromHex(expected); + string actual = color.ToHex(); + + Assert.Equal(expected, actual); + } + + [Fact] + public void WebSafePalette_IsCorrect() + { + Rgba32[] actualPalette = Color.WebSafePalette.ToArray().Select(c => (Rgba32)c).ToArray(); + Assert.Equal(ReferencePalette.WebSafeColors, actualPalette); + } + + [Fact] + public void WernerPalette_IsCorrect() + { + Rgba32[] actualPalette = Color.WernerPalette.ToArray().Select(c => (Rgba32)c).ToArray(); + Assert.Equal(ReferencePalette.WernerColors, actualPalette); + } + + public class FromHex + { + [Fact] + public void ShortHex() + { + Assert.Equal(new Rgb24(255, 255, 255), (Rgb24) Color.FromHex("#fff")); + Assert.Equal(new Rgb24(255, 255, 255), (Rgb24) Color.FromHex("fff")); + Assert.Equal(new Rgba32(0, 0, 0, 255), (Rgba32) Color.FromHex("000f")); + } + + [Fact] + public void LeadingPoundIsOptional() + { + Assert.Equal(new Rgb24(0, 128, 128), (Rgb24) Color.FromHex("#008080")); + Assert.Equal(new Rgb24(0, 128, 128), (Rgb24) Color.FromHex("008080")); + } + + [Fact] + public void ThrowsOnEmpty() + { + Assert.Throws(() => Color.FromHex("")); + } + + [Fact] + public void ThrowsOnNull() + { + Assert.Throws(() => Color.FromHex(null)); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Color/ReferencePalette.cs b/tests/ImageSharp.Tests/Color/ReferencePalette.cs new file mode 100644 index 0000000000..3c6e382c58 --- /dev/null +++ b/tests/ImageSharp.Tests/Color/ReferencePalette.cs @@ -0,0 +1,277 @@ +// // Copyright (c) Six Labors and contributors. +// // Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Tests +{ + internal static class ReferencePalette + { + /// + /// Gets a collection of named, web safe, colors as defined in the CSS Color Module Level 4. + /// + public static readonly Rgba32[] WebSafeColors = + { + Rgba32.AliceBlue, + Rgba32.AntiqueWhite, + Rgba32.Aqua, + Rgba32.Aquamarine, + Rgba32.Azure, + Rgba32.Beige, + Rgba32.Bisque, + Rgba32.Black, + Rgba32.BlanchedAlmond, + Rgba32.Blue, + Rgba32.BlueViolet, + Rgba32.Brown, + Rgba32.BurlyWood, + Rgba32.CadetBlue, + Rgba32.Chartreuse, + Rgba32.Chocolate, + Rgba32.Coral, + Rgba32.CornflowerBlue, + Rgba32.Cornsilk, + Rgba32.Crimson, + Rgba32.Cyan, + Rgba32.DarkBlue, + Rgba32.DarkCyan, + Rgba32.DarkGoldenrod, + Rgba32.DarkGray, + Rgba32.DarkGreen, + Rgba32.DarkKhaki, + Rgba32.DarkMagenta, + Rgba32.DarkOliveGreen, + Rgba32.DarkOrange, + Rgba32.DarkOrchid, + Rgba32.DarkRed, + Rgba32.DarkSalmon, + Rgba32.DarkSeaGreen, + Rgba32.DarkSlateBlue, + Rgba32.DarkSlateGray, + Rgba32.DarkTurquoise, + Rgba32.DarkViolet, + Rgba32.DeepPink, + Rgba32.DeepSkyBlue, + Rgba32.DimGray, + Rgba32.DodgerBlue, + Rgba32.Firebrick, + Rgba32.FloralWhite, + Rgba32.ForestGreen, + Rgba32.Fuchsia, + Rgba32.Gainsboro, + Rgba32.GhostWhite, + Rgba32.Gold, + Rgba32.Goldenrod, + Rgba32.Gray, + Rgba32.Green, + Rgba32.GreenYellow, + Rgba32.Honeydew, + Rgba32.HotPink, + Rgba32.IndianRed, + Rgba32.Indigo, + Rgba32.Ivory, + Rgba32.Khaki, + Rgba32.Lavender, + Rgba32.LavenderBlush, + Rgba32.LawnGreen, + Rgba32.LemonChiffon, + Rgba32.LightBlue, + Rgba32.LightCoral, + Rgba32.LightCyan, + Rgba32.LightGoldenrodYellow, + Rgba32.LightGray, + Rgba32.LightGreen, + Rgba32.LightPink, + Rgba32.LightSalmon, + Rgba32.LightSeaGreen, + Rgba32.LightSkyBlue, + Rgba32.LightSlateGray, + Rgba32.LightSteelBlue, + Rgba32.LightYellow, + Rgba32.Lime, + Rgba32.LimeGreen, + Rgba32.Linen, + Rgba32.Magenta, + Rgba32.Maroon, + Rgba32.MediumAquamarine, + Rgba32.MediumBlue, + Rgba32.MediumOrchid, + Rgba32.MediumPurple, + Rgba32.MediumSeaGreen, + Rgba32.MediumSlateBlue, + Rgba32.MediumSpringGreen, + Rgba32.MediumTurquoise, + Rgba32.MediumVioletRed, + Rgba32.MidnightBlue, + Rgba32.MintCream, + Rgba32.MistyRose, + Rgba32.Moccasin, + Rgba32.NavajoWhite, + Rgba32.Navy, + Rgba32.OldLace, + Rgba32.Olive, + Rgba32.OliveDrab, + Rgba32.Orange, + Rgba32.OrangeRed, + Rgba32.Orchid, + Rgba32.PaleGoldenrod, + Rgba32.PaleGreen, + Rgba32.PaleTurquoise, + Rgba32.PaleVioletRed, + Rgba32.PapayaWhip, + Rgba32.PeachPuff, + Rgba32.Peru, + Rgba32.Pink, + Rgba32.Plum, + Rgba32.PowderBlue, + Rgba32.Purple, + Rgba32.RebeccaPurple, + Rgba32.Red, + Rgba32.RosyBrown, + Rgba32.RoyalBlue, + Rgba32.SaddleBrown, + Rgba32.Salmon, + Rgba32.SandyBrown, + Rgba32.SeaGreen, + Rgba32.SeaShell, + Rgba32.Sienna, + Rgba32.Silver, + Rgba32.SkyBlue, + Rgba32.SlateBlue, + Rgba32.SlateGray, + Rgba32.Snow, + Rgba32.SpringGreen, + Rgba32.SteelBlue, + Rgba32.Tan, + Rgba32.Teal, + Rgba32.Thistle, + Rgba32.Tomato, + Rgba32.Transparent, + Rgba32.Turquoise, + Rgba32.Violet, + Rgba32.Wheat, + Rgba32.White, + Rgba32.WhiteSmoke, + Rgba32.Yellow, + Rgba32.YellowGreen + }; + + /// + /// Gets a collection of colors as defined in the original second edition of Werner’s Nomenclature of Colours 1821. + /// The hex codes were collected and defined by Nicholas Rougeux + /// + public static readonly Rgba32[] WernerColors = + { + Rgba32.FromHex("#f1e9cd"), + Rgba32.FromHex("#f2e7cf"), + Rgba32.FromHex("#ece6d0"), + Rgba32.FromHex("#f2eacc"), + Rgba32.FromHex("#f3e9ca"), + Rgba32.FromHex("#f2ebcd"), + Rgba32.FromHex("#e6e1c9"), + Rgba32.FromHex("#e2ddc6"), + Rgba32.FromHex("#cbc8b7"), + Rgba32.FromHex("#bfbbb0"), + Rgba32.FromHex("#bebeb3"), + Rgba32.FromHex("#b7b5ac"), + Rgba32.FromHex("#bab191"), + Rgba32.FromHex("#9c9d9a"), + Rgba32.FromHex("#8a8d84"), + Rgba32.FromHex("#5b5c61"), + Rgba32.FromHex("#555152"), + Rgba32.FromHex("#413f44"), + Rgba32.FromHex("#454445"), + Rgba32.FromHex("#423937"), + Rgba32.FromHex("#433635"), + Rgba32.FromHex("#252024"), + Rgba32.FromHex("#241f20"), + Rgba32.FromHex("#281f3f"), + Rgba32.FromHex("#1c1949"), + Rgba32.FromHex("#4f638d"), + Rgba32.FromHex("#383867"), + Rgba32.FromHex("#5c6b8f"), + Rgba32.FromHex("#657abb"), + Rgba32.FromHex("#6f88af"), + Rgba32.FromHex("#7994b5"), + Rgba32.FromHex("#6fb5a8"), + Rgba32.FromHex("#719ba2"), + Rgba32.FromHex("#8aa1a6"), + Rgba32.FromHex("#d0d5d3"), + Rgba32.FromHex("#8590ae"), + Rgba32.FromHex("#3a2f52"), + Rgba32.FromHex("#39334a"), + Rgba32.FromHex("#6c6d94"), + Rgba32.FromHex("#584c77"), + Rgba32.FromHex("#533552"), + Rgba32.FromHex("#463759"), + Rgba32.FromHex("#bfbac0"), + Rgba32.FromHex("#77747f"), + Rgba32.FromHex("#4a475c"), + Rgba32.FromHex("#b8bfaf"), + Rgba32.FromHex("#b2b599"), + Rgba32.FromHex("#979c84"), + Rgba32.FromHex("#5d6161"), + Rgba32.FromHex("#61ac86"), + Rgba32.FromHex("#a4b6a7"), + Rgba32.FromHex("#adba98"), + Rgba32.FromHex("#93b778"), + Rgba32.FromHex("#7d8c55"), + Rgba32.FromHex("#33431e"), + Rgba32.FromHex("#7c8635"), + Rgba32.FromHex("#8e9849"), + Rgba32.FromHex("#c2c190"), + Rgba32.FromHex("#67765b"), + Rgba32.FromHex("#ab924b"), + Rgba32.FromHex("#c8c76f"), + Rgba32.FromHex("#ccc050"), + Rgba32.FromHex("#ebdd99"), + Rgba32.FromHex("#ab9649"), + Rgba32.FromHex("#dbc364"), + Rgba32.FromHex("#e6d058"), + Rgba32.FromHex("#ead665"), + Rgba32.FromHex("#d09b2c"), + Rgba32.FromHex("#a36629"), + Rgba32.FromHex("#a77d35"), + Rgba32.FromHex("#f0d696"), + Rgba32.FromHex("#d7c485"), + Rgba32.FromHex("#f1d28c"), + Rgba32.FromHex("#efcc83"), + Rgba32.FromHex("#f3daa7"), + Rgba32.FromHex("#dfa837"), + Rgba32.FromHex("#ebbc71"), + Rgba32.FromHex("#d17c3f"), + Rgba32.FromHex("#92462f"), + Rgba32.FromHex("#be7249"), + Rgba32.FromHex("#bb603c"), + Rgba32.FromHex("#c76b4a"), + Rgba32.FromHex("#a75536"), + Rgba32.FromHex("#b63e36"), + Rgba32.FromHex("#b5493a"), + Rgba32.FromHex("#cd6d57"), + Rgba32.FromHex("#711518"), + Rgba32.FromHex("#e9c49d"), + Rgba32.FromHex("#eedac3"), + Rgba32.FromHex("#eecfbf"), + Rgba32.FromHex("#ce536b"), + Rgba32.FromHex("#b74a70"), + Rgba32.FromHex("#b7757c"), + Rgba32.FromHex("#612741"), + Rgba32.FromHex("#7a4848"), + Rgba32.FromHex("#3f3033"), + Rgba32.FromHex("#8d746f"), + Rgba32.FromHex("#4d3635"), + Rgba32.FromHex("#6e3b31"), + Rgba32.FromHex("#864735"), + Rgba32.FromHex("#553d3a"), + Rgba32.FromHex("#613936"), + Rgba32.FromHex("#7a4b3a"), + Rgba32.FromHex("#946943"), + Rgba32.FromHex("#c39e6d"), + Rgba32.FromHex("#513e32"), + Rgba32.FromHex("#8b7859"), + Rgba32.FromHex("#9b856b"), + Rgba32.FromHex("#766051"), + Rgba32.FromHex("#453b32") + }; + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/ColorTests.cs b/tests/ImageSharp.Tests/ColorTests.cs deleted file mode 100644 index a1eaf9fae3..0000000000 --- a/tests/ImageSharp.Tests/ColorTests.cs +++ /dev/null @@ -1,255 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using SixLabors.ImageSharp.PixelFormats; - -using Xunit; - -namespace SixLabors.ImageSharp.Tests -{ - public class ColorTests - { - public class ConstructFrom - { - [Fact] - public void Rgba64() - { - Rgba64 source = new Rgba64(100, 2222, 3333, 4444); - - // Act: - Color color = new Color(source); - - // Assert: - Rgba64 data = color.ToPixel(); - Assert.Equal(source, data); - } - - [Fact] - public void Rgba32() - { - Rgba32 source = new Rgba32(1, 22, 33, 231); - - // Act: - Color color = new Color(source); - - // Assert: - Rgba32 data = color.ToPixel(); - Assert.Equal(source, data); - } - - [Fact] - public void Argb32() - { - Argb32 source = new Argb32(1, 22, 33, 231); - - // Act: - Color color = new Color(source); - - // Assert: - Argb32 data = color.ToPixel(); - Assert.Equal(source, data); - } - - [Fact] - public void Bgra32() - { - Bgra32 source = new Bgra32(1, 22, 33, 231); - - // Act: - Color color = new Color(source); - - // Assert: - Bgra32 data = color.ToPixel(); - Assert.Equal(source, data); - } - - [Fact] - public void Rgb24() - { - Rgb24 source = new Rgb24(1, 22, 231); - - // Act: - Color color = new Color(source); - - // Assert: - Rgb24 data = color.ToPixel(); - Assert.Equal(source, data); - } - - [Fact] - public void Bgr24() - { - Bgr24 source = new Bgr24(1, 22, 231); - - // Act: - Color color = new Color(source); - - // Assert: - Bgr24 data = color.ToPixel(); - Assert.Equal(source, data); - } - } - - public class Cast - { - [Fact] - public void Rgba64() - { - Rgba64 source = new Rgba64(100, 2222, 3333, 4444); - - // Act: - Color color = source; - - // Assert: - Rgba64 data = color.ToPixel(); - Assert.Equal(source, data); - } - - [Fact] - public void Rgba32() - { - Rgba32 source = new Rgba32(1, 22, 33, 231); - - // Act: - Color color = source; - - // Assert: - Rgba32 data = color.ToPixel(); - Assert.Equal(source, data); - } - - [Fact] - public void Argb32() - { - Argb32 source = new Argb32(1, 22, 33, 231); - - // Act: - Color color = source; - - // Assert: - Argb32 data = color.ToPixel(); - Assert.Equal(source, data); - } - - [Fact] - public void Bgra32() - { - Bgra32 source = new Bgra32(1, 22, 33, 231); - - // Act: - Color color = source; - - // Assert: - Bgra32 data = color.ToPixel(); - Assert.Equal(source, data); - } - - [Fact] - public void Rgb24() - { - Rgb24 source = new Rgb24(1, 22, 231); - - // Act: - Color color = source; - - // Assert: - Rgb24 data = color.ToPixel(); - Assert.Equal(source, data); - } - - [Fact] - public void Bgr24() - { - Bgr24 source = new Bgr24(1, 22, 231); - - // Act: - Color color = source; - - // Assert: - Bgr24 data = color.ToPixel(); - Assert.Equal(source, data); - } - } - - public class CastTo - { - [Fact] - public void Rgba64() - { - Rgba64 source = new Rgba64(100, 2222, 3333, 4444); - - // Act: - Color color = new Color(source); - - // Assert: - Rgba64 data = color; - Assert.Equal(source, data); - } - - [Fact] - public void Rgba32() - { - Rgba32 source = new Rgba32(1, 22, 33, 231); - - // Act: - Color color = new Color(source); - - // Assert: - Rgba32 data = color; - Assert.Equal(source, data); - } - - [Fact] - public void Argb32() - { - Argb32 source = new Argb32(1, 22, 33, 231); - - // Act: - Color color = new Color(source); - - // Assert: - Argb32 data = color; - Assert.Equal(source, data); - } - - [Fact] - public void Bgra32() - { - Bgra32 source = new Bgra32(1, 22, 33, 231); - - // Act: - Color color = new Color(source); - - // Assert: - Bgra32 data = color; - Assert.Equal(source, data); - } - - [Fact] - public void Rgb24() - { - Rgb24 source = new Rgb24(1, 22, 231); - - // Act: - Color color = new Color(source); - - // Assert: - Rgb24 data = color; - Assert.Equal(source, data); - } - - [Fact] - public void Bgr24() - { - Bgr24 source = new Bgr24(1, 22, 231); - - // Act: - Color color = new Color(source); - - // Assert: - Bgr24 data = color; - Assert.Equal(source, data); - } - } - } -} \ No newline at end of file