From 2e7708e747b8f4a93e120be9f0bb1399f308cb1b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 23 Oct 2016 22:52:48 +1100 Subject: [PATCH] Color stylecop [skip ci] Former-commit-id: 2da0a80a899dd840b6f172c4def833f36c8e7928 Former-commit-id: 0bca572f378a3e9ad03054405af9e055472c66cf Former-commit-id: ea59bff3ab338a9b648b556761a7eb8cd01c15d4 --- src/ImageProcessorCore/Colors/Color.cs | 148 ++++++++++-------- .../Colors/PackedVector/IPackedVector.cs | 3 + 2 files changed, 84 insertions(+), 67 deletions(-) diff --git a/src/ImageProcessorCore/Colors/Color.cs b/src/ImageProcessorCore/Colors/Color.cs index 51d539109..cad353a80 100644 --- a/src/ImageProcessorCore/Colors/Color.cs +++ b/src/ImageProcessorCore/Colors/Color.cs @@ -34,6 +34,71 @@ namespace ImageProcessorCore /// private uint packedValue; + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + /// The alpha component. + public Color(byte r, byte g, byte b, byte a = 255) + { + this.packedValue = (uint)(r << 24 | g << 16 | b << 8 | a); + } + + /// + /// Initializes a new instance of the struct. + /// + /// + /// The hexadecimal representation of the combined color components arranged + /// in rgb, rgba, rrggbb, or rrggbbaa format to match web syntax. + /// + public Color(string hex) + { + Guard.NotNullOrEmpty(hex, nameof(hex)); + + hex = ToRgbaHex(hex); + + if (hex == null || !uint.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out this.packedValue)) + { + throw new ArgumentException("Hexadecimal string is not in the correct format.", nameof(hex)); + } + } + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + /// The alpha component. + public Color(float r, float g, float b, float a = 1) + { + this.packedValue = Pack(r, g, b, a); + } + + /// + /// Initializes a new instance of the struct. + /// + /// + /// The vector containing the components for the packed vector. + /// + public Color(Vector3 vector) + { + this.packedValue = Pack(ref vector); + } + + /// + /// Initializes a new instance of the struct. + /// + /// + /// The vector containing the components for the packed vector. + /// + public Color(Vector4 vector) + { + this.packedValue = Pack(ref vector); + } + /// /// Gets or sets the red component. /// @@ -43,13 +108,13 @@ namespace ImageProcessorCore { return (byte)(this.packedValue >> 24); } + set { this.packedValue = this.packedValue & 0x00FFFFFF | (uint)value << 24; } } - /// /// Gets or sets the green component. /// @@ -59,6 +124,7 @@ namespace ImageProcessorCore { return (byte)(this.packedValue >> 16); } + set { this.packedValue = this.packedValue & 0xFF00FFFF | (uint)value << 16; @@ -74,6 +140,7 @@ namespace ImageProcessorCore { return (byte)(this.packedValue >> 8); } + set { this.packedValue = this.packedValue & 0xFFFF00FF | (uint)value << 8; @@ -89,80 +156,25 @@ namespace ImageProcessorCore { return (byte)this.packedValue; } + set { this.packedValue = this.packedValue & 0xFFFFFF00 | value; } } - /// - /// The packed value. - /// - public uint PackedValue { get { return this.packedValue; } set { this.packedValue = value; } } - - /// - /// Initializes a new instance of the struct. - /// - /// The red component. - /// The green component. - /// The blue component. - /// The alpha component. - public Color(byte r, byte g, byte b, byte a = 255) - { - this.packedValue = (uint)(r << 24 | g << 16 | b << 8 | a); - } - - /// - /// Initializes a new instance of the struct. - /// - /// - /// The hexadecimal representation of the combined color components arranged - /// in rgb, rgba, rrggbb, or rrggbbaa format to match web syntax. - /// - public Color(string hex) + /// + public uint PackedValue { - Guard.NotNullOrEmpty(hex, nameof(hex)); - - hex = ToRgbaHex(hex); - - if (hex == null || !uint.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out this.packedValue)) + get { - throw new ArgumentException("Hexadecimal string is not in the correct format.", nameof(hex)); + return this.packedValue; } - } - - /// - /// Initializes a new instance of the struct. - /// - /// The red component. - /// The green component. - /// The blue component. - /// The alpha component. - public Color(float r, float g, float b, float a = 1) - { - this.packedValue = Pack(r, g, b, a); - } - /// - /// Initializes a new instance of the struct. - /// - /// - /// The vector containing the components for the packed vector. - /// - public Color(Vector3 vector) - { - this.packedValue = Pack(ref vector); - } - - /// - /// Initializes a new instance of the struct. - /// - /// - /// The vector containing the components for the packed vector. - /// - public Color(Vector4 vector) - { - this.packedValue = Pack(ref vector); + set + { + this.packedValue = value; + } } /// @@ -311,11 +323,13 @@ namespace ImageProcessorCore { return hex; } - else if (hex.Length == 6) + + if (hex.Length == 6) { return hex + "FF"; } - else if (hex.Length < 3 || hex.Length > 4) + + if (hex.Length < 3 || hex.Length > 4) { return null; } diff --git a/src/ImageProcessorCore/Colors/PackedVector/IPackedVector.cs b/src/ImageProcessorCore/Colors/PackedVector/IPackedVector.cs index 2deb378f4..cb4662a01 100644 --- a/src/ImageProcessorCore/Colors/PackedVector/IPackedVector.cs +++ b/src/ImageProcessorCore/Colors/PackedVector/IPackedVector.cs @@ -15,6 +15,9 @@ namespace ImageProcessorCore public interface IPackedVector : IPackedVector where TPacked : struct { + /// + /// Gets or sets the packed representation of the value. + /// TPacked PackedValue { get; set; } }