7be91c1c6f833df6d259772df0ca045d33b5ac47
[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 #include "canvas/colorspace.h"
25
26 using namespace std;
27 using namespace ArdourCanvas;
28
29 using std::max;
30 using std::min;
31
32 void
33 ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v)
34 {
35         double a;
36         color_to_hsva (color, h, s, v, a);
37 }
38
39 void
40 ArdourCanvas::color_to_hsva (Color color, double& h, double& s, double& v, double& a)
41 {
42         double r, g, b;
43         double cmax;
44         double cmin;
45         double delta;
46         
47         color_to_rgba (color, r, g, b, a);
48         
49         if (r > g) {
50                 cmax = max (r, b);
51         } else {
52                 cmax = max (g, b);
53         }
54
55         if (r < g) {
56                 cmin = min (r, b);
57         } else {
58                 cmin = min (g, b);
59         }
60
61         v = cmax;
62
63         delta = cmax - cmin;
64
65         if (cmax == 0) {
66                 // r = g = b == 0 ... v is undefined, s = 0
67                 s = 0.0;  
68                 h = 0.0;
69                 return;
70         }
71
72         if (delta != 0.0) {     
73                 if (cmax == r) {
74                         h = fmod ((g - b)/delta, 6.0);
75                 } else if (cmax == g) {
76                         h = ((b - r)/delta) + 2;
77                 } else {
78                         h = ((r - g)/delta) + 4;
79                 }
80                 
81                 h *= 60.0;
82                 
83                 if (h < 0.0) {
84                         /* negative values are legal but confusing, because
85                            they alias positive values.
86                         */
87                         h = 360 + h;
88                 }
89         }
90
91         if (delta == 0 || cmax == 0) {
92                 s = 0;
93         } else {
94                 s = delta / cmax;
95         }
96 }
97
98 ArdourCanvas::Color
99 ArdourCanvas::hsva_to_color (double h, double s, double v, double a)
100 {
101         s = min (1.0, max (0.0, s));
102         v = min (1.0, max (0.0, v));
103
104         if (s == 0) {
105                 return rgba_to_color (v, v, v, a);
106         }
107
108         h = fmod (h + 360.0, 360.0);
109
110         double c = v * s;
111         double x = c * (1.0 - fabs(fmod(h / 60.0, 2) - 1.0));
112         double m = v - c;
113
114         if (h >= 0.0 && h < 60.0) {
115                 return rgba_to_color (c + m, x + m, m, a);
116         } else if (h >= 60.0 && h < 120.0) {
117                 return rgba_to_color (x + m, c + m, m, a);
118         } else if (h >= 120.0 && h < 180.0) {
119                 return rgba_to_color (m, c + m, x + m, a);
120         } else if (h >= 180.0 && h < 240.0) {
121                 return rgba_to_color (m, x + m, c + m, a);
122         } else if (h >= 240.0 && h < 300.0) {
123                 return rgba_to_color (x + m, m, c + m, a);
124         } else if (h >= 300.0 && h < 360.0) {
125                 return rgba_to_color (c + m, m, x + m, a);
126         } 
127         return rgba_to_color (m, m, m, a);
128 }
129
130 void
131 ArdourCanvas::color_to_rgba (Color color, double& r, double& g, double& b, double& a)
132 {
133         r = ((color >> 24) & 0xff) / 255.0;
134         g = ((color >> 16) & 0xff) / 255.0;
135         b = ((color >>  8) & 0xff) / 255.0;
136         a = ((color >>  0) & 0xff) / 255.0;
137 }
138
139 ArdourCanvas::Color
140 ArdourCanvas::rgba_to_color (double r, double g, double b, double a)
141 {
142         /* clamp to [0 .. 1] range */
143
144         r = min (1.0, max (0.0, r));
145         g = min (1.0, max (0.0, g));
146         b = min (1.0, max (0.0, b));
147         a = min (1.0, max (0.0, a));
148
149         /* convert to [0..255] range */
150
151         unsigned int rc, gc, bc, ac;
152         rc = rint (r * 255.0);
153         gc = rint (g * 255.0);
154         bc = rint (b * 255.0);
155         ac = rint (a * 255.0);
156
157         /* build-an-integer */
158
159         return (rc << 24) | (gc << 16) | (bc << 8) | ac;
160 }
161
162 // Inverse of sRGB "gamma" function.
163 static inline double 
164 inv_gam_sRGB (double c) 
165 {
166         if (c <= 0.04045) {
167                 return c/12.92;
168         } else {
169                 return pow(((c+0.055)/(1.055)),2.4);
170         }
171 }
172
173 // sRGB "gamma" function
174 static inline int 
175 gam_sRGB(double v) 
176 {
177         if (v <= 0.0031308) {
178                 v *= 12.92;
179         } else {
180                 v = 1.055 * pow (v, 1.0 / 2.4) - 0.055;
181         }
182         return int (v*255+.5);
183 }
184
185 static double 
186 luminance (uint32_t c)
187 {
188         // sRGB luminance(Y) values
189         const double rY = 0.212655;
190         const double gY = 0.715158;
191         const double bY = 0.072187;
192
193         double r, g, b, a;
194
195         ArdourCanvas::color_to_rgba (c, r, g, b, a);
196         
197         return (gam_sRGB (rY*inv_gam_sRGB(r) + gY*inv_gam_sRGB(g) + bY*inv_gam_sRGB(b))) / 255.0;
198 }    
199
200 uint32_t
201 ArdourCanvas::contrasting_text_color (uint32_t c)
202 {
203         static const uint32_t white = ArdourCanvas::rgba_to_color (1.0, 1.0, 1.0, 1.0);
204         static const uint32_t black = ArdourCanvas::rgba_to_color (0.0, 0.0, 0.0, 1.0);
205
206         return (luminance (c) < 0.50) ? white : black;
207 }
208
209
210
211 HSV::HSV ()
212         : h (0.0)
213         , s (1.0)
214         , v (1.0)
215         , a (1.0) 
216 {
217 }
218
219 HSV::HSV (double hh, double ss, double vv, double aa)
220         : h (hh)
221         , s (ss)
222         , v (vv)
223         , a (aa) 
224 {
225         if (h < 0.0) {
226                 /* normalize negative hue values into positive range */
227                 h = 360.0 + h;
228         }
229 }
230
231 HSV::HSV (Color c)
232 {
233         color_to_hsva (c, h, s, v, a);
234 }
235
236 bool
237 HSV::is_gray () const
238 {
239         return s == 0;
240 }
241
242 void
243 HSV::clamp ()
244 {
245         h = fmod (h, 360.0);
246         if (h < 0.0) {
247                 /* normalize negative hue values into positive range */
248                 h = 360.0 + h;
249         }
250         s = min (1.0, s);
251         v = min (1.0, v);
252         a = min (1.0, a);
253 }
254
255 HSV
256 HSV::operator+ (const HSV& operand) const
257 {
258         HSV hsv;
259         hsv.h = h + operand.h;
260         hsv.s = s + operand.s;
261         hsv.v = v + operand.v;
262         hsv.a = a + operand.a;
263         hsv.clamp ();
264         return hsv;
265 }
266
267 HSV
268 HSV::operator- (const HSV& operand) const
269 {
270         HSV hsv;
271         hsv.h = h - operand.h;
272         hsv.s = s - operand.s;
273         hsv.v = s - operand.v;
274         hsv.a = a - operand.a;
275         hsv.clamp ();
276         return hsv;
277 }
278
279 HSV&
280 HSV::operator=(Color c)
281 {
282         color_to_hsva (c, h, s, v, a);
283         clamp ();
284         return *this;
285 }
286
287 HSV&
288 HSV::operator=(const std::string& str)
289 {
290         uint32_t c;
291         c = strtol (str.c_str(), 0, 16);
292         color_to_hsva (c, h, s, v, a);
293         clamp ();
294         return *this;
295 }
296
297 bool
298 HSV::operator== (const HSV& other)
299 {
300         return h == other.h &&
301                 s == other.s &&
302                 v == other.v &&
303                 a == other.a;
304 }
305
306 HSV
307 HSV::shade (double factor) const
308 {
309         HSV hsv (*this);
310         
311         /* algorithm derived from a google palette website
312            and analysis of their color palettes.
313
314            basic rule: to make a color darker, increase its saturation 
315            until it reaches 88%, but then additionally reduce value/lightness 
316            by a larger amount.
317
318            invert rule to make a color lighter.
319         */
320
321         if (factor > 1.0) {
322                 if (s < 88) {
323                         hsv.v += (hsv.v * (factor * 10.0));
324                 } 
325                 hsv.s *= factor;
326         } else {
327                 if (s < 88) {
328                         hsv.v -= (hsv.v * (factor * 10.0));
329                 } 
330                 hsv.s *= factor;
331         }
332
333         hsv.clamp();
334
335         return hsv;
336 }
337
338 HSV
339 HSV::outline () const
340 {
341         if (luminance (color()) < 0.50) {
342                 /* light color, darker outline: black with 15% opacity */
343                 return HSV (0.0, 0.0, 0.0, 0.15);
344         } else {
345                 /* dark color, lighter outline: white with 15% opacity */
346                 return HSV (0.0, 0.0, 1.0, 0.15);
347         }
348 }
349
350 HSV
351 HSV::mix (const HSV& other, double amount) const
352 {
353         HSV hsv;
354
355         hsv.h = h + (amount * (other.h - h));
356         hsv.v = v + (amount * (other.s - s));
357         hsv.s = s + (amount * (other.v - v));
358
359         hsv.clamp();
360
361         return hsv;
362 }
363
364 HSV
365 HSV::delta (const HSV& other) const
366 {
367         HSV d;
368         d.h = h - other.h;
369         d.s = s - other.s;
370         d.v = v - other.v;
371         /* do not clamp - we are returning a delta */
372         return d;
373 }
374
375 double
376 HSV::distance (const HSV& other) const
377 {
378         /* Use CIE94 definition for now */
379
380         double sL, sA, sB;
381         double oL, oA, oB;
382         double r, g, b, a;
383         Color c; 
384
385         c = hsva_to_color (h, s, v, a);
386         color_to_rgba (c, r, g, b, a);
387         Rgb2Lab (&sL, &sA, &sB, r, g, b);
388
389         c = hsva_to_color (other.h, other.s, other.v, other.a);
390         color_to_rgba (c, r, g, b, a);
391         Rgb2Lab (&oL, &oA, &oB, r, g, b);
392
393         // Weighting factors depending on the application (1 = default)
394
395         const double whtL = 1.0;
396         const double whtC = 1.0;
397         const double whtH = 1.0;  
398
399         const double xC1 = sqrt ((sA * sA) + (sB * oB));
400         const double xC2 = sqrt ((oA * oA) + (oB * oB));
401         double xDL = oL - sL;
402         double xDC = xC2 - xC1;
403         const double xDE = sqrt (((sL - oL) * (sL - oL))
404                                  + ((sA - oA) * (sA - oA))
405                                  + ((sB - oB) * (sB - oB)));
406         
407         double xDH;
408
409         if (sqrt (xDE) > (sqrt (abs (xDL)) + sqrt (abs (xDC)))) {
410                 xDH = sqrt ((xDE * xDE) - (xDL * xDL) - (xDC * xDC));
411         } else {
412                 xDH = 0;
413         }
414
415         const double xSC = 1 + (0.045 * xC1);
416         const double xSH = 1 + (0.015 * xC1);
417
418         xDL /= whtL;
419         xDC /= whtC * xSC;
420         xDH /= whtH * xSH;
421         
422         return sqrt ((xDL * xDL) + (xDC * xDC) + (xDH * xDH));
423 }
424
425 HSV
426 HSV::opposite () const
427 {
428         HSV hsv (*this);
429         hsv.h = fmod (h + 180.0, 360.0);
430         return hsv;
431 }
432
433 HSV
434 HSV::bw_text () const
435 {
436         return HSV (contrasting_text_color (color()));
437 }
438
439 HSV
440 HSV::text () const
441 {
442         return opposite ();
443 }
444
445 HSV
446 HSV::selected () const
447 {
448         /* XXX hack */
449         return HSV (Color (0xff0000));
450 }
451
452
453 void
454 HSV::print (std::ostream& o) const
455 {
456         if (!is_gray()) {
457                 o << '(' << s << ',' << v << ',' << a << ')';
458         } else {
459                 o << "gray(" << v << ')';
460         }
461 }
462
463
464 std::ostream& operator<<(std::ostream& o, const ArdourCanvas::HSV& hsv) { hsv.print (o); return o; }