further work on new color manipulation code
[ardour.git] / libs / canvas / colors.cc
1 /*
2     Copyright (C) 2014 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <algorithm>
20 #include <cmath>
21 #include <stdint.h>
22
23 #include "canvas/colors.h"
24
25 using namespace std;
26 using namespace ArdourCanvas;
27
28 using std::max;
29 using std::min;
30
31 void
32 ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v)
33 {
34         double r, g, b, a;
35         double cmax;
36         double cmin;
37         double delta;
38         
39         color_to_rgba (color, r, g, b, a);
40         
41         if (r > g) {
42                 cmax = max (r, b);
43         } else {
44                 cmax = max (g, b);
45         }
46
47         if (r < g) {
48                 cmin = min (r, b);
49         } else {
50                 cmin = min (g, b);
51         }
52
53         v = cmax;
54
55         delta = cmax - cmin;
56
57         if (cmax == 0) {
58                 // r = g = b == 0 ... v is undefined, s = 0
59                 s = 0.0;  
60                 h = -1.0;
61         }
62
63         if (delta != 0.0) {     
64                 if (cmax == r) {
65                         h = fmod ((g - b)/delta, 6.0);
66                 } else if (cmax == g) {
67                         h = ((b - r)/delta) + 2;
68                 } else {
69                         h = ((r - g)/delta) + 4;
70                 }
71                 
72                 h *= 60.0;
73         }
74
75         if (delta == 0 || cmax == 0) {
76                 s = 0;
77         } else {
78                 s = delta / cmax;
79         }
80 }
81
82 ArdourCanvas::Color
83 ArdourCanvas::hsv_to_color (const HSV& hsv, double a)
84 {
85         return hsv_to_color (hsv.h, hsv.s, hsv.v, a);
86 }
87
88 ArdourCanvas::Color
89 ArdourCanvas::hsv_to_color (double h, double s, double v, double a)
90 {
91         s = min (1.0, max (0.0, s));
92         v = min (1.0, max (0.0, v));
93
94         if (s == 0) {
95                 // achromatic (grey)
96                 return rgba_to_color (v, v, v, a);
97         }
98
99         h = min (360.0, max (0.0, h));
100
101         double c = v * s;
102         double x = c * (1.0 - fabs(fmod(h / 60.0, 2) - 1.0));
103         double m = v - c;
104
105         if (h >= 0.0 && h < 60.0) {
106                 return rgba_to_color (c + m, x + m, m, a);
107         } else if (h >= 60.0 && h < 120.0) {
108                 return rgba_to_color (x + m, c + m, m, a);
109         } else if (h >= 120.0 && h < 180.0) {
110                 return rgba_to_color (m, c + m, x + m, a);
111         } else if (h >= 180.0 && h < 240.0) {
112                 return rgba_to_color (m, x + m, c + m, a);
113         } else if (h >= 240.0 && h < 300.0) {
114                 return rgba_to_color (x + m, m, c + m, a);
115         } else if (h >= 300.0 && h < 360.0) {
116                 return rgba_to_color (c + m, m, x + m, a);
117         } 
118         return rgba_to_color (m, m, m, a);
119 }
120
121 void
122 ArdourCanvas::color_to_rgba (Color color, double& r, double& g, double& b, double& a)
123 {
124         r = ((color >> 24) & 0xff) / 255.0;
125         g = ((color >> 16) & 0xff) / 255.0;
126         b = ((color >>  8) & 0xff) / 255.0;
127         a = ((color >>  0) & 0xff) / 255.0;
128 }
129
130 ArdourCanvas::Color
131 ArdourCanvas::rgba_to_color (double r, double g, double b, double a)
132 {
133         /* clamp to [0 .. 1] range */
134
135         r = min (1.0, max (0.0, r));
136         g = min (1.0, max (0.0, g));
137         b = min (1.0, max (0.0, b));
138         a = min (1.0, max (0.0, a));
139
140         /* convert to [0..255] range */
141
142         unsigned int rc, gc, bc, ac;
143         rc = rint (r * 255.0);
144         gc = rint (g * 255.0);
145         bc = rint (b * 255.0);
146         ac = rint (a * 255.0);
147
148         /* build-an-integer */
149
150         return (rc << 24) | (gc << 16) | (bc << 8) | ac;
151 }
152
153 // Inverse of sRGB "gamma" function.
154 static inline double 
155 inv_gam_sRGB (double c) 
156 {
157         if (c <= 0.04045) {
158                 return c/12.92;
159         } else {
160                 return pow(((c+0.055)/(1.055)),2.4);
161         }
162 }
163
164 // sRGB "gamma" function
165 static inline int 
166 gam_sRGB(double v) 
167 {
168         if (v <= 0.0031308) {
169                 v *= 12.92;
170         } else {
171                 v = 1.055 * pow (v, 1.0 / 2.4) - 0.055;
172         }
173         return int (v*255+.5);
174 }
175
176 static double 
177 luminance (uint32_t c)
178 {
179         // sRGB luminance(Y) values
180         const double rY = 0.212655;
181         const double gY = 0.715158;
182         const double bY = 0.072187;
183
184         double r, g, b, a;
185
186         ArdourCanvas::color_to_rgba (c, r, g, b, a);
187         
188         return (gam_sRGB (rY*inv_gam_sRGB(r) + gY*inv_gam_sRGB(g) + bY*inv_gam_sRGB(b))) / 255.0;
189 }    
190
191 uint32_t
192 ArdourCanvas::contrasting_text_color (uint32_t c)
193 {
194         static const uint32_t white = ArdourCanvas::rgba_to_color (1.0, 1.0, 1.0, 1.0);
195         static const uint32_t black = ArdourCanvas::rgba_to_color (0.0, 0.0, 0.0, 1.0);
196
197         return (luminance (c) < 0.50) ? white : black;
198 }
199
200 HSV::HSV ()
201         : h (1.0)
202         , s (1.0)
203         , v (1.0)
204 {
205 }
206
207 HSV::HSV (double hh, double ss, double vv)
208         : h (hh)
209         , s (ss)
210         , v (vv)
211 {
212 }
213
214 HSV::HSV (Color c)
215 {
216         color_to_hsv (c, h, s, v);
217 }
218
219 void
220 HSV::clamp ()
221 {
222         s = min (s, 1.0);
223         v = min (v, 1.0);
224         h = min (255.0, h);
225 }
226
227 HSV
228 HSV::operator+ (const HSV& operand) const
229 {
230         HSV hsv;
231         hsv.h = h + operand.h;
232         hsv.s = s + operand.s;
233         hsv.v = v + operand.v;
234         hsv.clamp();
235         return hsv;
236 }
237
238 HSV
239 HSV::operator- (const HSV& operand) const
240 {
241         HSV hsv;
242         hsv.h = h - operand.h;
243         hsv.s = s - operand.s;
244         hsv.v = v - operand.v;
245         hsv.clamp();
246         return hsv;
247 }
248
249 HSV
250 HSV::operator* (double d) const
251 {
252         HSV hsv;
253         hsv.h = h * d;
254         hsv.s = s * d;
255         hsv.v = v * d;
256         hsv.clamp();
257         return hsv;
258 }
259
260 HSV
261 HSV::shade (double factor) const
262 {
263         HSV hsv (*this);
264         
265         /* algorithm derived from a google palette website
266            and analysis of their color palettes.
267
268            basic rule: to make a color darker, increase its saturation 
269            until it reaches 88%, but then additionally reduce value/lightness 
270            by a larger amount.
271
272            invert rule to make a color lighter.
273         */
274
275         if (factor > 1.0) {
276                 if (s < 88) {
277                         hsv.v *= 1.0/(factor/10.0);
278                 } else {
279                         hsv.s *= factor;
280                 }
281         } else {
282                 if (s < 88) {
283                         hsv.v *= 1.0/factor;
284                 } else {
285                         hsv.s *= factor;
286                 }
287         }
288
289         hsv.clamp();
290
291         return hsv;
292 }
293
294 HSV
295 HSV::mix (const HSV& other, double amount) const
296 {
297         HSV hsv;
298
299         hsv.h = h + (amount * (other.h - h));
300         hsv.v = v + (amount * (other.s - s));
301         hsv.s = s + (amount * (other.v - v));
302
303         hsv.clamp();
304
305         return hsv;
306 }
307
308 void
309 HSV::print (std::ostream& o) const
310 {
311         if (!is_gray()) {
312                 o << "hsv " << h << '|' << s << '|' << v;
313         } else {
314                 o << "hsv gray";
315         }
316 }
317
318 HSVA::HSVA ()
319         : a (1.0) 
320 {
321 }
322
323 HSVA::HSVA (double hh, double ss, double vv, double aa)
324         : HSV (hh, ss, vv)
325         , a (aa) 
326 {
327 }
328
329 HSVA::HSVA (Color c)
330 {
331         color_to_hsv (c, h, s, v);
332         a = c & 0xff;
333 }
334
335 void
336 HSVA::clamp ()
337 {
338         HSV::clamp ();
339         a = min (1.0, a);
340 }
341
342 HSVA
343 HSVA::operator+ (const HSVA& operand) const
344 {
345         HSVA hsv;
346         hsv.h = h + operand.h;
347         hsv.s = s + operand.s;
348         hsv.v = v + operand.v;
349         hsv.a = a + operand.a;
350         return hsv;
351 }
352
353 HSVA
354 HSVA::operator- (const HSVA& operand) const
355 {
356         HSVA hsv;
357         hsv.h = h - operand.h;
358         hsv.s = s - operand.s;
359         hsv.a = a - operand.a;
360         return hsv;
361 }
362
363 void
364 HSVA::print (std::ostream& o) const
365 {
366         if (!is_gray()) {
367                 o << "hsva " << h << '|' << s << '|' << v << '|' << a;
368         } else {
369                 o << "hsva gray";
370         }
371 }
372
373
374 ArdourCanvas::Color
375 ArdourCanvas::hsva_to_color (const HSVA& hsva)
376 {
377         return hsv_to_color (hsva.h, hsva.s, hsva.v, hsva.a);
378 }
379
380 std::ostream& operator<<(std::ostream& o, const ArdourCanvas::HSV& hsv) { hsv.print (o); return o; }
381 std::ostream& operator<<(std::ostream& o, const ArdourCanvas::HSVA& hsva) { hsva.print (o); return o; }