1 module polyplex.core.color;
2 import polyplex.math;
3 import polyplex.utils.logging;
4 import polyplex.core.colors;
5 
6 /**
7 	A class handling color and color blending
8 */
9 public class Color {
10 public:
11 	mixin ColorList;
12 
13 	union {
14 		struct {
15 			/// Red color
16 			int R;
17 
18 			/// Green color
19 			int G;
20 
21 			/// Blue color
22 			int B;
23 
24 			/// Alpha transparency
25 			int A;
26 		}
27 
28 		/// OpenGL friendly color
29 		Vector4i GLColor;
30 	}
31 
32 	/// Constructor
33 	this(int r, int g, int b, int a) {
34 		R = Mathf.Clamp(r, 0, 255);
35 		G = Mathf.Clamp(g, 0, 255);
36 		B = Mathf.Clamp(b, 0, 255);
37 		A = Mathf.Clamp(a, 0, 255);
38 	}
39 
40 	/// Constructor
41 	this(int r, int g, int b) {
42 		this(r, g, b, 255);
43 	}
44 
45 	/// Constructor
46 	this(uint packed) {
47 		this((packed >> 24) & 0xff, (packed >> 16) & 0xff, (packed >> 8) & 0xff, packed & 0xff);
48 	}
49 
50 	/// Float variant of red color
51 	@property float Rf() { return cast(float)R/255; }
52 
53 	/// Float variant of green color
54 	@property float Gf() { return cast(float)G/255; }
55 
56 	/// Float variant of blue color
57 	@property float Bf() { return cast(float)B/255; }
58 
59 	/// Float variant of alpha transparency
60 	@property float Af() { return cast(float)A/255; }
61 
62 	/// GL friendly float color
63 	@property Vector4 GLfColor() { return Vector4(Rf(), Gf(), Bf(), Af()); }
64 	
65 	/// Gets teh 
66 	@property ubyte[] ColorBytes() { return [cast(ubyte)R, cast(ubyte)G, cast(ubyte)B, cast(ubyte)A]; }
67 
68 	/**
69 		Premultiplied alpha blending.
70 	*/
71 	Color PreMultAlphaBlend(Color other) {
72 		Color o = new Color(R, G, B, A);
73 		o.R = cast(int)(((other.Rf * other.Af) + (o.Rf * (1f - other.Af)))*255);
74 		o.G = cast(int)(((other.Gf * other.Af) + (o.Gf * (1f - other.Af)))*255);
75 		o.B = cast(int)(((other.Bf * other.Af) + (o.Bf * (1f - other.Af)))*255);
76 		o.A = 255;
77 		return o;
78 	}
79 }