140 lines
4.1 KiB
C#
140 lines
4.1 KiB
C#
using Unosquare.Swan;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace Unosquare.RaspberryIO.Camera {
|
|
/// <summary>
|
|
/// A simple RGB color class to represent colors in RGB and YUV colorspaces.
|
|
/// </summary>
|
|
public class CameraColor {
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="CameraColor"/> class.
|
|
/// </summary>
|
|
/// <param name="r">The red.</param>
|
|
/// <param name="g">The green.</param>
|
|
/// <param name="b">The blue.</param>
|
|
public CameraColor(Int32 r, Int32 g, Int32 b)
|
|
: this(r, g, b, String.Empty) {
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="CameraColor"/> class.
|
|
/// </summary>
|
|
/// <param name="r">The red.</param>
|
|
/// <param name="g">The green.</param>
|
|
/// <param name="b">The blue.</param>
|
|
/// <param name="name">The well-known color name.</param>
|
|
public CameraColor(Int32 r, Int32 g, Int32 b, String name) {
|
|
this.RGB = new[] { Convert.ToByte(r.Clamp(0, 255)), Convert.ToByte(g.Clamp(0, 255)), Convert.ToByte(b.Clamp(0, 255)) };
|
|
|
|
Single y = this.R * .299000f + this.G * .587000f + this.B * .114000f;
|
|
Single u = this.R * -.168736f + this.G * -.331264f + this.B * .500000f + 128f;
|
|
Single v = this.R * .500000f + this.G * -.418688f + this.B * -.081312f + 128f;
|
|
|
|
this.YUV = new Byte[] { (Byte)y.Clamp(0, 255), (Byte)u.Clamp(0, 255), (Byte)v.Clamp(0, 255) };
|
|
this.Name = name;
|
|
}
|
|
|
|
#region Static Definitions
|
|
|
|
/// <summary>
|
|
/// Gets the predefined white color.
|
|
/// </summary>
|
|
public static CameraColor White => new CameraColor(255, 255, 255, nameof(White));
|
|
|
|
/// <summary>
|
|
/// Gets the predefined red color.
|
|
/// </summary>
|
|
public static CameraColor Red => new CameraColor(255, 0, 0, nameof(Red));
|
|
|
|
/// <summary>
|
|
/// Gets the predefined green color.
|
|
/// </summary>
|
|
public static CameraColor Green => new CameraColor(0, 255, 0, nameof(Green));
|
|
|
|
/// <summary>
|
|
/// Gets the predefined blue color.
|
|
/// </summary>
|
|
public static CameraColor Blue => new CameraColor(0, 0, 255, nameof(Blue));
|
|
|
|
/// <summary>
|
|
/// Gets the predefined black color.
|
|
/// </summary>
|
|
public static CameraColor Black => new CameraColor(0, 0, 0, nameof(Black));
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Gets the well-known color name.
|
|
/// </summary>
|
|
public String Name {
|
|
get;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the red byte.
|
|
/// </summary>
|
|
public Byte R => this.RGB[0];
|
|
|
|
/// <summary>
|
|
/// Gets the green byte.
|
|
/// </summary>
|
|
public Byte G => this.RGB[1];
|
|
|
|
/// <summary>
|
|
/// Gets the blue byte.
|
|
/// </summary>
|
|
public Byte B => this.RGB[2];
|
|
|
|
/// <summary>
|
|
/// Gets the RGB byte array (3 bytes).
|
|
/// </summary>
|
|
public Byte[] RGB {
|
|
get;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the YUV byte array (3 bytes).
|
|
/// </summary>
|
|
public Byte[] YUV {
|
|
get;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a hexadecimal representation of the RGB byte array.
|
|
/// Preceded by 0x and all in lowercase
|
|
/// </summary>
|
|
/// <param name="reverse">if set to <c>true</c> [reverse].</param>
|
|
/// <returns>A string</returns>
|
|
public String ToRgbHex(Boolean reverse) {
|
|
Byte[] data = this.RGB.ToArray();
|
|
if(reverse) {
|
|
Array.Reverse(data);
|
|
}
|
|
|
|
return ToHex(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a hexadecimal representation of the YUV byte array.
|
|
/// Preceded by 0x and all in lowercase
|
|
/// </summary>
|
|
/// <param name="reverse">if set to <c>true</c> [reverse].</param>
|
|
/// <returns>A string</returns>
|
|
public String ToYuvHex(Boolean reverse) {
|
|
Byte[] data = this.YUV.ToArray();
|
|
if(reverse) {
|
|
Array.Reverse(data);
|
|
}
|
|
|
|
return ToHex(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a hexadecimal representation of the data byte array
|
|
/// </summary>
|
|
/// <param name="data">The data.</param>
|
|
/// <returns>A string</returns>
|
|
private static String ToHex(Byte[] data) => $"0x{BitConverter.ToString(data).Replace("-", String.Empty).ToLowerInvariant()}";
|
|
}
|
|
} |