.
1 /* Copyright (C) 1999 The Free Software Foundation
2 *
3 * Authors: Simon Budig <Simon.Budig@unix-ag.org> (original code)
4 * Federico Mena-Quintero <federico@gimp.org> (cleanup for GTK+)
5 * Jonathan Blandford <jrb@redhat.com> (cleanup for GTK+)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 namespace BirdFont {
24
25 public class Color {
26 public double r;
27 public double g;
28 public double b;
29 public double a;
30
31 public Color (double r, double g, double b, double a) {
32 this.r = r;
33 this.g = g;
34 this.b = b;
35 this.a = a;
36 }
37
38 public Color.hsba (double h, double s, double v, double a) {
39 double hue, saturation, value;
40 double f, p, q, t;
41
42 this.a = a;
43
44 if (s == 0.0) {
45 r = v;
46 g = v;
47 b = v;
48 } else {
49 hue = h * 6.0;
50 saturation = s;
51 value = v;
52
53 if (hue == 6.0) {
54 hue = 0.0;
55 }
56
57 f = hue - (int) hue;
58 p = value * (1.0 - saturation);
59 q = value * (1.0 - saturation * f);
60 t = value * (1.0 - saturation * (1.0 - f));
61
62 switch ((int) hue) {
63 case 0:
64 r = value;
65 g = t;
66 b = p;
67 break;
68
69 case 1:
70 r = q;
71 g = value;
72 b = p;
73 break;
74
75 case 2:
76 r = p;
77 g = value;
78 b = t;
79 break;
80
81 case 3:
82 r = p;
83 g = q;
84 b = value;
85 break;
86
87 case 4:
88 r = t;
89 g = p;
90 b = value;
91 break;
92
93 case 5:
94 r = value;
95 g = p;
96 b = q;
97 break;
98
99 default:
100 assert_not_reached ();
101 }
102 }
103 }
104
105 public void to_hsva (out double h, out double s, out double v, out double a) {
106 double red, green, blue;
107 double min, max;
108 double delta;
109
110 a = this.a;
111
112 red = r;
113 green = g;
114 blue = b;
115
116 h = 0.0;
117
118 if (red > green) {
119 if (red > blue)
120 max = red;
121 else
122 max = blue;
123
124 if (green < blue)
125 min = green;
126 else
127 min = blue;
128 } else {
129 if (green > blue)
130 max = green;
131 else
132 max = blue;
133
134 if (red < blue)
135 min = red;
136 else
137 min = blue;
138 }
139
140 v = max;
141
142 if (max != 0.0)
143 s = (max - min) / max;
144 else
145 s = 0.0;
146
147 if (s == 0.0)
148 h = 0.0;
149 else {
150 delta = max - min;
151
152 if (red == max)
153 h = (green - blue) / delta;
154 else if (green == max)
155 h = 2 + (blue - red) / delta;
156 else if (blue == max)
157 h = 4 + (red - green) / delta;
158
159 h /= 6.0;
160
161 if (h < 0.0)
162 h += 1.0;
163 else if (h > 1.0)
164 h -= 1.0;
165 }
166 }
167
168 public static Color black () {
169 return new Color (0, 0, 0, 1);
170 }
171
172 public static Color red () {
173 return new Color (1, 0, 0, 1);
174 }
175
176 public static Color green () {
177 return new Color (0, 1, 0, 1);
178 }
179
180 public static Color blue () {
181 return new Color (0, 0, 1, 1);
182 }
183
184 public static Color yellow () {
185 return new Color (222.0 / 255, 203.0 / 255, 43 / 255.0, 1);
186 }
187
188 public static Color brown () {
189 return new Color (160.0 / 255, 90.0 / 255, 44.0 / 255, 1);
190 }
191
192 public static Color pink () {
193 return new Color (247.0 / 255, 27.0 / 255, 113 / 255.0, 1);
194 }
195
196 public static Color white () {
197 return new Color (1, 1, 1, 1);
198 }
199
200 public static Color grey () {
201 return new Color (0.5, 0.5, 0.5, 1);
202 }
203
204 public static Color magenta () {
205 return new Color (103.0 / 255, 33.0 / 255, 120.0 / 255, 1);
206 }
207
208 public string to_string () {
209 return @"r: $r, g: $g, b: $b, a: $a";
210 }
211
212 public Color copy () {
213 return new Color (r, g, b, a);
214 }
215
216 public string to_rgb_hex () {
217 string s = "#";
218 s += Font.to_hex_code ((unichar) Math.rint (r * 254));
219 s += Font.to_hex_code ((unichar) Math.rint (g * 254));
220 s += Font.to_hex_code ((unichar) Math.rint (b * 254));
221 return s;
222 }
223 }
224
225 }
226