1 module prova.graphics.primitives.color;
2 
3 ///
4 struct Color
5 {
6   ///
7   float r = 1;
8   ///
9   float g = 1;
10   ///
11   float b = 1;
12   ///
13   float a = 1;
14 
15   ///
16   this(float r, float g, float b)
17   {
18     set(r, g, b, 1);
19   }
20 
21   ///
22   this(float r, float g, float b, float a)
23   {
24     set(r, g, b, a);
25   }
26 
27   /// Sets the values of r, g, and b in a single statement
28   void set(float r, float g, float b)
29   {
30     this.r = r;
31     this.g = g;
32     this.b = b;
33     this.a = 1;
34   }
35 
36   /// Sets the values of r, g, b, and a in a single statement
37   void set(float r, float g, float b, float a)
38   {
39     this.r = r;
40     this.g = g;
41     this.b = b;
42     this.a = a;
43   }
44 
45   Color lerp(Color color, float a) const
46   {
47     color.r = (color.r + r) * a;
48     color.g = (color.g + g) * a;
49     color.b = (color.b + b) * a;
50     color.a = (color.a + a) * a;
51 
52     return color;
53   }
54 
55   invariant
56   {
57     // limit components to be [0-1]
58     assert(r <= 1 && r >= 0);
59     assert(g <= 1 && g >= 0);
60     assert(b <= 1 && b >= 0);
61     assert(a <= 1 && a >= 0);
62   }
63 }