1 module evael.utils.Color;
2 
3 import derelict.nanovg.types : NVGcolor;
4 import derelict.nanovg.funcs : nvgRGBA;
5 
6 import jsonizer;
7 
8 /**
9  * Color.
10  */
11 struct Color
12 {
13 	mixin JsonizeMe;
14 
15 	/// Color's values
16 	@jsonize("rgba") private ubyte[4] m_values;
17 
18 	/// Predefined colors
19 	public static Color White = Color(255, 255, 255),
20 						Black = Color(0, 0, 0),
21 						Red = Color(255, 0, 0),
22 						Blue = Color(0, 0, 255),
23 						Green = Color(0, 255, 0),
24 						Grey = Color(223, 223, 223),
25 						LightGrey = Color(240, 240, 240),
26 						DarkGrey = Color(80, 80, 80),
27 						Orange = Color(252, 148, 0),
28 						LightOrange = Color(255, 195, 110),
29 						Transparent = Color(255, 255, 255, 0);
30 
31 	/**
32 	 * Color constructor.
33 	 * Params:
34 	 * 		r : r
35 	 *		g : g
36 	 *		b : b
37 	 *		a : a
38 	 */
39 	@nogc @safe
40 	public this(in ubyte r, in ubyte g, in ubyte b, in ubyte a = 255) pure nothrow
41 	{
42 		this.m_values = [r, g, b, a];
43 	}
44 
45 	@nogc @safe
46 	public bool opEquals()(in ref Color c) const pure nothrow
47 	{
48 		return this.m_values[] == c.m_values[];
49 	}
50 
51 	@nogc @safe
52 	@property nothrow
53 	{
54 		public ubyte r() const
55 		{
56 			return this.m_values[0];
57 		}
58 
59 		public ubyte g() const
60 		{
61 			return this.m_values[1];
62 		}
63 
64 		public ubyte b() const
65 		{
66 			return this.m_values[2];
67 		}
68 
69 		public ubyte a() const
70 		{
71 			return this.m_values[3];
72 		}
73 
74 		public void a(in ubyte value)
75 		{
76 			this.m_values[3] = value;
77 		}
78 		
79 		@trusted
80 		public NVGcolor asNvg() const
81 		{
82 			return nvgRGBA(this.m_values[0], this.m_values[1], this.m_values[2], this.m_values[3]);
83 		}
84 
85 		public float[4] asFloat() const
86 		{
87 			return [this.m_values[0] / 255.0f, this.m_values[1] / 255.0f, this.m_values[2] / 255.0f, this.m_values[3] / 255.0f];
88 		}
89 	}
90 
91 	@nogc
92 	@property
93 	public auto ptr() pure nothrow
94 	{
95 		return this.m_values.ptr;
96 	}
97 }