|
|
|
@ -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; |
|
|
|
} |
|
|
|
|