From cd18e0136240cf971c9bcb7c45ac2cb7adfbd65d Mon Sep 17 00:00:00 2001 From: James South Date: Mon, 27 Apr 2015 10:50:40 +0100 Subject: [PATCH] Added web compatible hex overload. Former-commit-id: f77be6bc3a0f72c6c8837793801e122a0b2b113b Former-commit-id: aef161da11ecf0b4e691e3f820839ca768e40759 Former-commit-id: e78951c909777df6a2ce651b1a1995441776b54e --- src/ImageProcessor/Colors/Color.cs | 94 ++++++++++++++++++++++++++++-- src/ImageProcessor/ImageBase.cs | 4 +- 2 files changed, 90 insertions(+), 8 deletions(-) diff --git a/src/ImageProcessor/Colors/Color.cs b/src/ImageProcessor/Colors/Color.cs index 891d30ee3..f7335e4aa 100644 --- a/src/ImageProcessor/Colors/Color.cs +++ b/src/ImageProcessor/Colors/Color.cs @@ -128,6 +128,50 @@ namespace ImageProcessor this.Bgra = bgra; } + /// + /// Initializes a new instance of the struct. + /// + /// + /// The hexadecimal representation of the combined color components arranged + /// in r,g,b or a,r,g,b format to match web syntax. + /// + public Color(string hex) + : this() + { + hex = hex.StartsWith("#") ? hex.Substring(1) : hex; + + if (hex.Length != 8 && hex.Length != 6 && hex.Length != 3) + { + throw new ArgumentException("Hexadecimal string is not in the correct format.", "hex"); + } + + if (hex.Length == 8) + { + this.R = Convert.ToByte(hex.Substring(2, 2), 16); + this.G = Convert.ToByte(hex.Substring(4, 2), 16); + this.B = Convert.ToByte(hex.Substring(6, 2), 16); + this.A = Convert.ToByte(hex.Substring(0, 2), 16); + } + else if (hex.Length == 6) + { + this.R = Convert.ToByte(hex.Substring(0, 2), 16); + this.G = Convert.ToByte(hex.Substring(2, 2), 16); + this.B = Convert.ToByte(hex.Substring(4, 2), 16); + this.A = 255; + } + else + { + string r = char.ToString(hex[0]); + string g = char.ToString(hex[1]); + string b = char.ToString(hex[2]); + + this.R = Convert.ToByte(r + r, 16); + this.G = Convert.ToByte(g + g, 16); + this.B = Convert.ToByte(b + b, 16); + this.A = 255; + } + } + /// /// Gets a value indicating whether this is empty. /// @@ -140,6 +184,44 @@ namespace ImageProcessor } } + /// + /// Compares two objects. The result specifies whether the values + /// of the , , , and + /// properties of the two objects are equal. + /// + /// + /// The on the left side of the operand. + /// + /// + /// The on the right side of the operand. + /// + /// + /// True if the current left is equal to the parameter; otherwise, false. + /// + public static bool operator ==(Color left, Color right) + { + return left.Equals(right); + } + + /// + /// Compares two objects. The result specifies whether the values + /// of the , , , and + /// properties of the two objects are unequal. + /// + /// + /// The on the left side of the operand. + /// + /// + /// The on the right side of the operand. + /// + /// + /// True if the current left is unequal to the parameter; otherwise, false. + /// + public static bool operator !=(Color left, Color right) + { + return !left.Equals(right); + } + /// /// Indicates whether this instance and a specified object are equal. /// @@ -200,20 +282,20 @@ namespace ImageProcessor /// /// Returns the hash code for the given instance. /// - /// + /// /// The instance of to return the hash code for. /// /// /// A 32-bit signed integer that is the hash code for this instance. /// - private int GetHashCode(Color obj) + private int GetHashCode(Color color) { unchecked { - int hashCode = obj.B.GetHashCode(); - hashCode = (hashCode * 397) ^ obj.G.GetHashCode(); - hashCode = (hashCode * 397) ^ obj.R.GetHashCode(); - hashCode = (hashCode * 397) ^ obj.A.GetHashCode(); + int hashCode = color.B.GetHashCode(); + hashCode = (hashCode * 397) ^ color.G.GetHashCode(); + hashCode = (hashCode * 397) ^ color.R.GetHashCode(); + hashCode = (hashCode * 397) ^ color.A.GetHashCode(); return hashCode; } } diff --git a/src/ImageProcessor/ImageBase.cs b/src/ImageProcessor/ImageBase.cs index ae64550bb..616f879fe 100644 --- a/src/ImageProcessor/ImageBase.cs +++ b/src/ImageProcessor/ImageBase.cs @@ -4,7 +4,7 @@ // Licensed under the Apache License, Version 2.0. // // -// The base class of all image. Encapsulates all the properties and methods +// The base class of all images. Encapsulates all the properties and methods // required to manipulate images. // // -------------------------------------------------------------------------------------------------------------------- @@ -14,7 +14,7 @@ namespace ImageProcessor using System; /// - /// The base class of all image. Encapsulates all the properties and methods + /// The base class of all images. Encapsulates all the properties and methods /// required to manipulate images. /// public abstract class ImageBase : IImageBase