1 module daque.graphics.color;
2 
3 /++
4 	Struct representing an RGBA color
5 +/
6 struct Color
7 {
8 public:
9 	/++
10 		RGBA components of the color
11 	+/
12 	ubyte[4] component;
13 
14 	/++
15 		Gets a reference to the Red component of the color
16 	+/
17 	ref ubyte r()
18 	{
19 		return component[0];
20 	}
21 
22 	/++
23 		Gets a reference to the Green component of the color 
24 	+/
25 	ref ubyte g()
26 	{
27 		return component[1];
28 	}
29 
30 	/++
31 		Gets a reference to the Blue component of the color
32 	+/
33 	ref ubyte b()
34 	{
35 		return component[2];
36 	}
37 
38 	/++
39 		Gets a reference to the Alpha component of the color
40 	+/
41 	ref ubyte a()
42 	{
43 		return component[3];
44 	}
45 
46 	/++
47 		Constructs a new Color from the given red (r), green (g), blue (b) and alpha (a) components
48 	+/
49 	this(ubyte r, ubyte g, ubyte b, ubyte a)
50 	{
51 		component[] = [r, g, b, a];
52 	}
53 
54 	/++
55 		Constructs a color from the encoded 32 bit unsigned integer color
56 	+/
57 	this(uint color)
58 	{
59 		for (uint i; i < component.length; i++)
60 		{
61 			component[i] = color % 0x100;
62 			color >>= 8;
63 		}
64 	}
65 
66 	/++
67 		Encodes the color into a 32 bit unsigned integer
68 	+/
69 	uint toInt()
70 	{
71 		uint color = 0u;
72 
73 		for (int i = cast(int) component.length - 1; i >= 0; i--)
74 		{
75 			color += component[i];
76 			if (i - 1 >= 0)
77 				color <<= 8;
78 		}
79 
80 		return color;
81 	}
82 }