3c638935ec51b091971e1c367786ca5642091495
[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
83 ArdourCanvas::Color
84 ArdourCanvas::hsv_to_color (double h, double s, double v, double a)
85 {
86         s = min (1.0, max (0.0, s));
87         v = min (1.0, max (0.0, v));
88
89         if (s == 0) {
90                 // achromatic (grey)
91                 return rgba_to_color (v, v, v, a);
92         }
93
94         h = min (360.0, max (0.0, h));
95
96         double c = v * s;
97         double x = c * (1.0 - fabs(fmod(h / 60.0, 2) - 1.0));
98         double m = v - c;
99
100         if (h >= 0.0 && h < 60.0) {
101                 return rgba_to_color (c + m, x + m, m, a);
102         } else if (h >= 60.0 && h < 120.0) {
103                 return rgba_to_color (x + m, c + m, m, a);
104         } else if (h >= 120.0 && h < 180.0) {
105                 return rgba_to_color (m, c + m, x + m, a);
106         } else if (h >= 180.0 && h < 240.0) {
107                 return rgba_to_color (m, x + m, c + m, a);
108         } else if (h >= 240.0 && h < 300.0) {
109                 return rgba_to_color (x + m, m, c + m, a);
110         } else if (h >= 300.0 && h < 360.0) {
111                 return rgba_to_color (c + m, m, x + m, a);
112         } 
113         return rgba_to_color (m, m, m, a);
114 }
115
116 void
117 ArdourCanvas::color_to_rgba (Color color, double& r, double& g, double& b, double& a)
118 {
119         r = ((color >> 24) & 0xff) / 255.0;
120         g = ((color >> 16) & 0xff) / 255.0;
121         b = ((color >>  8) & 0xff) / 255.0;
122         a = ((color >>  0) & 0xff) / 255.0;
123 }
124
125 ArdourCanvas::Color
126 ArdourCanvas::rgba_to_color (double r, double g, double b, double a)
127 {
128         /* clamp to [0 .. 1] range */
129
130         r = min (1.0, max (0.0, r));
131         g = min (1.0, max (0.0, g));
132         b = min (1.0, max (0.0, b));
133         a = min (1.0, max (0.0, a));
134
135         /* convert to [0..255] range */
136
137         unsigned int rc, gc, bc, ac;
138         rc = rint (r * 255.0);
139         gc = rint (g * 255.0);
140         bc = rint (b * 255.0);
141         ac = rint (a * 255.0);
142
143         /* build-an-integer */
144
145         return (rc << 24) | (gc << 16) | (bc << 8) | ac;
146 }
147
148 // Inverse of sRGB "gamma" function.
149 static inline double 
150 inv_gam_sRGB (double c) 
151 {
152         if (c <= 0.04045) {
153                 return c/12.92;
154         } else {
155                 return pow(((c+0.055)/(1.055)),2.4);
156         }
157 }
158
159 // sRGB "gamma" function
160 static inline int 
161 gam_sRGB(double v) 
162 {
163         if (v <= 0.0031308) {
164                 v *= 12.92;
165         } else {
166                 v = 1.055 * pow (v, 1.0 / 2.4) - 0.055;
167         }
168         return int (v*255+.5);
169 }
170
171 static double 
172 luminance (uint32_t c)
173 {
174         // sRGB luminance(Y) values
175         const double rY = 0.212655;
176         const double gY = 0.715158;
177         const double bY = 0.072187;
178
179         double r, g, b, a;
180
181         ArdourCanvas::color_to_rgba (c, r, g, b, a);
182         
183         return (gam_sRGB (rY*inv_gam_sRGB(r) + gY*inv_gam_sRGB(g) + bY*inv_gam_sRGB(b))) / 255.0;
184 }    
185
186 uint32_t
187 ArdourCanvas::contrasting_text_color (uint32_t c)
188 {
189         static const uint32_t white = ArdourCanvas::rgba_to_color (1.0, 1.0, 1.0, 1.0);
190         static const uint32_t black = ArdourCanvas::rgba_to_color (0.0, 0.0, 0.0, 1.0);
191
192         return (luminance (c) < 0.50) ? white : black;
193 }