Browse Source

Color stylecop [skip ci]

Former-commit-id: 2da0a80a899dd840b6f172c4def833f36c8e7928
Former-commit-id: 0bca572f378a3e9ad03054405af9e055472c66cf
Former-commit-id: ea59bff3ab338a9b648b556761a7eb8cd01c15d4
pull/1/head
James Jackson-South 9 years ago
parent
commit
2e7708e747
  1. 148
      src/ImageProcessorCore/Colors/Color.cs
  2. 3
      src/ImageProcessorCore/Colors/PackedVector/IPackedVector.cs

148
src/ImageProcessorCore/Colors/Color.cs

@ -34,6 +34,71 @@ namespace ImageProcessorCore
/// </summary>
private uint packedValue;
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
public Color(byte r, byte g, byte b, byte a = 255)
{
this.packedValue = (uint)(r << 24 | g << 16 | b << 8 | a);
}
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="hex">
/// The hexadecimal representation of the combined color components arranged
/// in rgb, rgba, rrggbb, or rrggbbaa format to match web syntax.
/// </param>
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));
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
public Color(float r, float g, float b, float a = 1)
{
this.packedValue = Pack(r, g, b, a);
}
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="vector">
/// The vector containing the components for the packed vector.
/// </param>
public Color(Vector3 vector)
{
this.packedValue = Pack(ref vector);
}
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="vector">
/// The vector containing the components for the packed vector.
/// </param>
public Color(Vector4 vector)
{
this.packedValue = Pack(ref vector);
}
/// <summary>
/// Gets or sets the red component.
/// </summary>
@ -43,13 +108,13 @@ namespace ImageProcessorCore
{
return (byte)(this.packedValue >> 24);
}
set
{
this.packedValue = this.packedValue & 0x00FFFFFF | (uint)value << 24;
}
}
/// <summary>
/// Gets or sets the green component.
/// </summary>
@ -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;
}
}
/// <summary>
/// The packed value.
/// </summary>
public uint PackedValue { get { return this.packedValue; } set { this.packedValue = value; } }
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
public Color(byte r, byte g, byte b, byte a = 255)
{
this.packedValue = (uint)(r << 24 | g << 16 | b << 8 | a);
}
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="hex">
/// The hexadecimal representation of the combined color components arranged
/// in rgb, rgba, rrggbb, or rrggbbaa format to match web syntax.
/// </param>
public Color(string hex)
/// <inheritdoc/>
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;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
public Color(float r, float g, float b, float a = 1)
{
this.packedValue = Pack(r, g, b, a);
}
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="vector">
/// The vector containing the components for the packed vector.
/// </param>
public Color(Vector3 vector)
{
this.packedValue = Pack(ref vector);
}
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="vector">
/// The vector containing the components for the packed vector.
/// </param>
public Color(Vector4 vector)
{
this.packedValue = Pack(ref vector);
set
{
this.packedValue = value;
}
}
/// <summary>
@ -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;
}

3
src/ImageProcessorCore/Colors/PackedVector/IPackedVector.cs

@ -15,6 +15,9 @@ namespace ImageProcessorCore
public interface IPackedVector<TPacked> : IPackedVector
where TPacked : struct
{
/// <summary>
/// Gets or sets the packed representation of the value.
/// </summary>
TPacked PackedValue { get; set; }
}

Loading…
Cancel
Save