1 module qrcode.renderer.color.rgb;
2 
3 import qrcode.renderer.color.colorinterface;
4 import qrcode.renderer.color.gray;
5 import qrcode.renderer.color.cmyk;
6 /**
7 * RGB color.
8 */
9 class Rgb : ColorInterface
10 {
11 	/**
12 	* Red value.
13 	*
14 	* @var integer
15 	*/
16     protected int red;
17     /**
18 	* Green value.
19 	*
20 	* @var integer
21 	*/
22     protected int green;
23     /**
24 	* Blue value.
25 	*
26 	* @var integer
27 	*/
28     protected int blue;
29 
30 	this(int r, int g, int b)
31 	{
32 		if (r < 0 || r > 255) {
33             throw new Exception("Red must be between 0 and 255");
34         }
35         if (g < 0 || g > 255) {
36             throw new Exception("Green must be between 0 and 255");
37         }
38         if (b < 0 || b > 255) {
39             throw new Exception("Blue must be between 0 and 255");
40         }
41         this.red   = r;
42         this.green = g;
43         this.blue  = b;
44 	}
45 
46 	/**
47 	* Returns the red value.
48 	*
49 	* @return integer
50 	*/
51     public int getRed()
52     {
53         return this.red;
54     }
55     /**
56 	* Returns the green value.
57 	*
58 	* @return integer
59 	*/
60     public int getGreen()
61     {
62         return this.green;
63     }
64     /**
65 	* Returns the blue value.
66 	*
67 	* @return integer
68 	*/
69     public int getBlue()
70     {
71         return this.blue;
72     }
73     /**
74 	* Returns a hexadecimal string representation of the RGB value.
75 	*
76 	* @return string
77 	*/
78     public override string toString()
79     {
80 		import std.format;
81         return format("%02x%02x%02x", this.red, this.green, this.blue);
82     }
83     /**
84 	* toRgb(): defined by ColorInterface.
85 	*
86 	* @see    ColorInterface::toRgb()
87 	* @return Rgb
88 	*/
89     public Rgb toRgb()
90     {
91         return this;
92     }
93     /**
94 	* toCmyk(): defined by ColorInterface.
95 	*
96 	* @see    ColorInterface::toCmyk()
97 	* @return Cmyk
98 	*/
99     public Cmyk toCmyk()
100     {
101 		import std.algorithm;
102 
103         auto c = 1 - (this.red / 255);
104         auto m = 1 - (this.green / 255);
105         auto y = 1 - (this.blue / 255);
106         auto k = min(c, m, y);
107         return new Cmyk(
108 						100 * (c - k) / (1 - k),
109 						100 * (m - k) / (1 - k),
110 						100 * (y - k) / (1 - k),
111 						100 * k
112 						);
113     }
114     /**
115 	* toGray(): defined by ColorInterface.
116 	*
117 	* @see    ColorInterface::toGray()
118 	* @return Gray
119 	*/
120     public Gray toGray()
121     {
122 		import std.conv;
123         return new Gray(to!int((this.red * 0.21 + this.green * 0.71 + this.blue * 0.07) / 2.55));
124     }
125 }