From ea8f7b748fb04febb3d8d3791b5a93d1799e375a Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Fri, 10 May 2019 00:24:28 +0200 Subject: [PATCH] cleanup and document Color --- src/ImageSharp/Color/Color.Conversions.cs | 162 +++++++++++++++++++ src/ImageSharp/Color/Color.NamedColors.cs | 7 +- src/ImageSharp/Color/Color.WebSafePalette.cs | 9 +- src/ImageSharp/Color/Color.WernerPalette.cs | 7 +- src/ImageSharp/Color/Color.cs | 107 ++++++------ 5 files changed, 226 insertions(+), 66 deletions(-) create mode 100644 src/ImageSharp/Color/Color.Conversions.cs diff --git a/src/ImageSharp/Color/Color.Conversions.cs b/src/ImageSharp/Color/Color.Conversions.cs new file mode 100644 index 000000000..001aee5a4 --- /dev/null +++ b/src/ImageSharp/Color/Color.Conversions.cs @@ -0,0 +1,162 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Numerics; + +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp +{ + /// + /// Contains constructors and implicit conversion methods. + /// + public readonly partial struct Color + { + /// + /// Initializes a new instance of the struct. + /// + /// The containing the color information. + public Color(Rgba64 pixel) + { + this.data = pixel; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The containing the color information. + public Color(Rgba32 pixel) + { + this.data = new Rgba64(pixel); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The containing the color information. + public Color(Argb32 pixel) + { + this.data = new Rgba64(pixel); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The containing the color information. + public Color(Bgra32 pixel) + { + this.data = new Rgba64(pixel); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The containing the color information. + public Color(Rgb24 pixel) + { + this.data = new Rgba64(pixel); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The containing the color information. + public Color(Bgr24 pixel) + { + this.data = new Rgba64(pixel); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The containing the color information. + public Color(Vector4 vector) + { + this.data = new Rgba64(vector); + } + + /// + /// Converts an to . + /// + /// The . + /// The . + public static implicit operator Color(Rgba64 source) => new Color(source); + + /// + /// Converts an to . + /// + /// The . + /// The . + public static implicit operator Color(Rgba32 source) => new Color(source); + + /// + /// Converts an to . + /// + /// The . + /// The . + public static implicit operator Color(Bgra32 source) => new Color(source); + + /// + /// Converts an to . + /// + /// The . + /// The . + public static implicit operator Color(Argb32 source) => new Color(source); + + /// + /// Converts an to . + /// + /// The . + /// The . + public static implicit operator Color(Rgb24 source) => new Color(source); + + /// + /// Converts an to . + /// + /// The . + /// The . + public static implicit operator Color(Bgr24 source) => new Color(source); + + /// + /// Converts a to . + /// + /// The . + /// The . + public static implicit operator Rgba64(Color color) => color.data; + + /// + /// Converts a to . + /// + /// The . + /// The . + public static implicit operator Rgba32(Color color) => color.data.ToRgba32(); + + /// + /// Converts a to . + /// + /// The . + /// The . + public static implicit operator Bgra32(Color color) => color.data.ToBgra32(); + + /// + /// Converts a to . + /// + /// The . + /// The . + public static implicit operator Argb32(Color color) => color.data.ToArgb32(); + + /// + /// Converts a to . + /// + /// The . + /// The . + public static implicit operator Rgb24(Color color) => color.data.ToRgb24(); + + /// + /// Converts a to . + /// + /// The . + /// The . + public static implicit operator Bgr24(Color color) => color.data.ToBgr24(); + } +} \ No newline at end of file diff --git a/src/ImageSharp/Color/Color.NamedColors.cs b/src/ImageSharp/Color/Color.NamedColors.cs index a712f85da..8811516c1 100644 --- a/src/ImageSharp/Color/Color.NamedColors.cs +++ b/src/ImageSharp/Color/Color.NamedColors.cs @@ -1,8 +1,11 @@ -// // Copyright (c) Six Labors and contributors. -// // Licensed under the Apache License, Version 2.0. +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. namespace SixLabors.ImageSharp { + /// + /// Contains static named color values. + /// public readonly partial struct Color { /// diff --git a/src/ImageSharp/Color/Color.WebSafePalette.cs b/src/ImageSharp/Color/Color.WebSafePalette.cs index 13720ec49..506432ac2 100644 --- a/src/ImageSharp/Color/Color.WebSafePalette.cs +++ b/src/ImageSharp/Color/Color.WebSafePalette.cs @@ -1,16 +1,19 @@ -// // Copyright (c) Six Labors and contributors. -// // Licensed under the Apache License, Version 2.0. +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. using System; namespace SixLabors.ImageSharp { + /// + /// Contains the definition of . + /// 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. + /// Gets a collection of named, web safe colors as defined in the CSS Color Module Level 4. /// public static ReadOnlySpan WebSafePalette => WebSafePaletteLazy.Value; diff --git a/src/ImageSharp/Color/Color.WernerPalette.cs b/src/ImageSharp/Color/Color.WernerPalette.cs index 45823479e..37980b15f 100644 --- a/src/ImageSharp/Color/Color.WernerPalette.cs +++ b/src/ImageSharp/Color/Color.WernerPalette.cs @@ -1,10 +1,13 @@ -// // Copyright (c) Six Labors and contributors. -// // Licensed under the Apache License, Version 2.0. +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. using System; namespace SixLabors.ImageSharp { + /// + /// Contains the definition of . + /// public partial struct Color { private static readonly Lazy WernerPaletteLazy = new Lazy(CreateWernerPalette, true); diff --git a/src/ImageSharp/Color/Color.cs b/src/ImageSharp/Color/Color.cs index 021cea415..94b5dde2e 100644 --- a/src/ImageSharp/Color/Color.cs +++ b/src/ImageSharp/Color/Color.cs @@ -11,81 +11,58 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp { + /// + /// Represents a color value that is convertible to all implementations. + /// 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(); - + /// + /// Checks whether two structures are equal. + /// + /// The left hand operand. + /// The right hand operand. + /// + /// True if the parameter is equal to the parameter; + /// otherwise, false. + /// public static bool operator ==(Color left, Color right) { return left.Equals(right); } + /// + /// Checks whether two structures are equal. + /// + /// The left hand operand. + /// The right hand operand. + /// + /// True if the parameter is not equal to the parameter; + /// otherwise, false. + /// public static bool operator !=(Color left, Color right) { return !left.Equals(right); } + /// + /// Creates a from RGBA bytes. + /// + /// The red component (0-255). + /// The green component (0-255). + /// The blue component (0-255). + /// The alpha component (0-255). + /// The . public static Color FromRgba(byte r, byte g, byte b, byte a) => new Color(new Rgba32(r, g, b, a)); + /// + /// Creates a from RGB bytes. + /// + /// The red component (0-255). + /// The green component (0-255). + /// The blue component (0-255). + /// The . public static Color FromRgb(byte r, byte g, byte b) => FromRgba(r, g, b, 255); /// @@ -120,6 +97,12 @@ namespace SixLabors.ImageSharp /// public override string ToString() => this.ToHex(); + /// + /// Converts the color instance to an + /// implementation defined by . + /// + /// The pixel type to convert to. + /// The pixel value. public TPixel ToPixel() where TPixel : struct, IPixel { @@ -128,17 +111,20 @@ namespace SixLabors.ImageSharp 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); } @@ -148,6 +134,9 @@ namespace SixLabors.ImageSharp return this.data.PackedValue.GetHashCode(); } + /// + /// Bulk convert a span of to a span of a specified pixel type. + /// internal static void ToPixel( Configuration configuration, ReadOnlySpan source,