Merge branch 'cairocanvas'
[ardour.git] / libs / canvas / utils.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <algorithm>
21 #include <cmath>
22 #include <stdint.h>
23 #include <cairomm/context.h>
24 #include "canvas/utils.h"
25
26 using std::max;
27 using std::min;
28
29 void
30 ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v)
31 {
32         double r, g, b, a;
33         double cmax;
34         double cmin;
35         double delta;
36         
37         color_to_rgba (color, r, g, b, a);
38         
39         if (r > g) {
40                 cmax = max (r, b);
41         } else {
42                 cmax = max (g, b);
43         }
44
45         if (r < g) {
46                 cmin = min (r, b);
47         } else {
48                 cmin = min (g, b);
49         }
50
51         v = cmax;
52
53         delta = cmax - cmin;
54
55         if (cmax == 0) {
56                 // r = g = b == 0 ... v is undefined, s = 0
57                 s = 0.0;  
58                 h = -1.0;
59         }
60
61         if (delta != 0.0) {     
62                 if (cmax == r) {
63                         h = fmod ((g - b)/delta, 6.0);
64                 } else if (cmax == g) {
65                         h = ((b - r)/delta) + 2;
66                 } else {
67                         h = ((r - g)/delta) + 4;
68                 }
69                 
70                 h *= 60.0;
71         }
72
73         if (delta == 0 || cmax == 0) {
74                 s = 0;
75         } else {
76                 s = delta / cmax;
77         }
78
79 }
80
81 ArdourCanvas::Color
82 ArdourCanvas::hsv_to_color (double h, double s, double v, double a)
83 {
84         s = min (1.0, max (0.0, s));
85         v = min (1.0, max (0.0, v));
86
87         if (s == 0) {
88                 // achromatic (grey)
89                 return rgba_to_color (v, v, v, a);
90         }
91
92         h = min (360.0, max (0.0, h));
93
94         double c = v * s;
95         double x = c * (1.0 - fabs(fmod(h / 60.0, 2) - 1.0));
96         double m = v - c;
97
98         if (h >= 0.0 && h < 60.0) {
99                 return rgba_to_color (c + m, x + m, m, a);
100         } else if (h >= 60.0 && h < 120.0) {
101                 return rgba_to_color (x + m, c + m, m, a);
102         } else if (h >= 120.0 && h < 180.0) {
103                 return rgba_to_color (m, c + m, x + m, a);
104         } else if (h >= 180.0 && h < 240.0) {
105                 return rgba_to_color (m, x + m, c + m, a);
106         } else if (h >= 240.0 && h < 300.0) {
107                 return rgba_to_color (x + m, m, c + m, a);
108         } else if (h >= 300.0 && h < 360.0) {
109                 return rgba_to_color (c + m, m, x + m, a);
110         } 
111         return rgba_to_color (m, m, m, a);
112 }
113
114 void
115 ArdourCanvas::color_to_rgba (Color color, double& r, double& g, double& b, double& a)
116 {
117         r = ((color >> 24) & 0xff) / 255.0;
118         g = ((color >> 16) & 0xff) / 255.0;
119         b = ((color >>  8) & 0xff) / 255.0;
120         a = ((color >>  0) & 0xff) / 255.0;
121 }
122
123 ArdourCanvas::Color
124 ArdourCanvas::rgba_to_color (double r, double g, double b, double a)
125 {
126         /* clamp to [0 .. 1] range */
127
128         r = min (1.0, max (0.0, r));
129         g = min (1.0, max (0.0, g));
130         b = min (1.0, max (0.0, b));
131         a = min (1.0, max (0.0, a));
132
133         /* convert to [0..255] range */
134
135         unsigned int rc, gc, bc, ac;
136         rc = rint (r * 255.0);
137         gc = rint (g * 255.0);
138         bc = rint (b * 255.0);
139         ac = rint (a * 255.0);
140
141         /* build-an-integer */
142
143         return (rc << 24) | (gc << 16) | (bc << 8) | ac;
144 }
145
146 void
147 ArdourCanvas::set_source_rgba (Cairo::RefPtr<Cairo::Context> context, Color color)
148 {
149         context->set_source_rgba (
150                 ((color >> 24) & 0xff) / 255.0,
151                 ((color >> 16) & 0xff) / 255.0,
152                 ((color >>  8) & 0xff) / 255.0,
153                 ((color >>  0) & 0xff) / 255.0
154                 );
155 }
156
157 void
158 ArdourCanvas::set_source_rgb_a (Cairo::RefPtr<Cairo::Context> context, Color color, float alpha)
159 {
160         context->set_source_rgba (
161                 ((color >> 24) & 0xff) / 255.0,
162                 ((color >> 16) & 0xff) / 255.0,
163                 ((color >>  8) & 0xff) / 255.0,
164                 alpha
165                 );
166 }
167
168 void
169 ArdourCanvas::set_source_rgba (cairo_t *cr, Color color)
170 {
171         cairo_set_source_rgba ( cr,
172                 ((color >> 24) & 0xff) / 255.0,
173                 ((color >> 16) & 0xff) / 255.0,
174                 ((color >>  8) & 0xff) / 255.0,
175                 ((color >>  0) & 0xff) / 255.0
176                 );
177 }
178
179 void
180 ArdourCanvas::set_source_rgb_a (cairo_t *cr, Color color, float alpha)
181 {
182         cairo_set_source_rgba ( cr,
183                 ((color >> 24) & 0xff) / 255.0,
184                 ((color >> 16) & 0xff) / 255.0,
185                 ((color >>  8) & 0xff) / 255.0,
186                 alpha
187                 );
188 }
189
190 ArdourCanvas::Distance
191 ArdourCanvas::distance_to_segment_squared (Duple const & p, Duple const & p1, Duple const & p2, double& t, Duple& at)
192 {
193         static const double kMinSegmentLenSquared = 0.00000001;  // adjust to suit.  If you use float, you'll probably want something like 0.000001f
194         static const double kEpsilon = 1.0E-14;  // adjust to suit.  If you use floats, you'll probably want something like 1E-7f
195         double dx = p2.x - p1.x;
196         double dy = p2.y - p1.y;
197         double dp1x = p.x - p1.x;
198         double dp1y = p.y - p1.y;
199         const double segLenSquared = (dx * dx) + (dy * dy);
200
201         if (segLenSquared >= -kMinSegmentLenSquared && segLenSquared <= kMinSegmentLenSquared) {
202                 // segment is a point.
203                 at = p1;
204                 t = 0.0;
205                 return ((dp1x * dp1x) + (dp1y * dp1y));
206         } 
207
208
209         // Project a line from p to the segment [p1,p2].  By considering the line
210         // extending the segment, parameterized as p1 + (t * (p2 - p1)),
211         // we find projection of point p onto the line. 
212         // It falls where t = [(p - p1) . (p2 - p1)] / |p2 - p1|^2
213                 
214         t = ((dp1x * dx) + (dp1y * dy)) / segLenSquared;
215
216         if (t < kEpsilon) {
217                 // intersects at or to the "left" of first segment vertex (p1.x, p1.y).  If t is approximately 0.0, then
218                 // intersection is at p1.  If t is less than that, then there is no intersection (i.e. p is not within
219                 // the 'bounds' of the segment)
220                 if (t > -kEpsilon) {
221                         // intersects at 1st segment vertex
222                         t = 0.0;
223                 }
224                 // set our 'intersection' point to p1.
225                 at = p1;
226                 // Note: If you wanted the ACTUAL intersection point of where the projected lines would intersect if
227                 // we were doing PointLineDistanceSquared, then qx would be (p1.x + (t * dx)) and qy would be (p1.y + (t * dy)).
228
229         } else if (t > (1.0 - kEpsilon)) {
230                 // intersects at or to the "right" of second segment vertex (p2.x, p2.y).  If t is approximately 1.0, then
231                 // intersection is at p2.  If t is greater than that, then there is no intersection (i.e. p is not within
232                 // the 'bounds' of the segment)
233                 if (t < (1.0 + kEpsilon)) {
234                         // intersects at 2nd segment vertex
235                         t = 1.0;
236                 }
237                 // set our 'intersection' point to p2.
238                 at = p2;
239                 // Note: If you wanted the ACTUAL intersection point of where the projected lines would intersect if
240                 // we were doing PointLineDistanceSquared, then qx would be (p1.x + (t * dx)) and qy would be (p1.y + (t * dy)).
241         } else {
242                 // The projection of the point to the point on the segment that is perpendicular succeeded and the point
243                 // is 'within' the bounds of the segment.  Set the intersection point as that projected point.
244                 at = Duple (p1.x + (t * dx), p1.y + (t * dy));
245         }
246
247         // return the squared distance from p to the intersection point.  Note that we return the squared distance
248         // as an optimization because many times you just need to compare relative distances and the squared values
249         // works fine for that.  If you want the ACTUAL distance, just take the square root of this value.
250         double dpqx = p.x - at.x;
251         double dpqy = p.y - at.y;
252
253         return ((dpqx * dpqx) + (dpqy * dpqy));
254 }
255
256 uint32_t
257 ArdourCanvas::contrasting_text_color (uint32_t c)
258 {
259         double r, g, b, a;
260         ArdourCanvas::color_to_rgba (c, r, g, b, a);
261
262         const double black_r = 0.0;
263         const double black_g = 0.0;
264         const double black_b = 0.0;
265
266         const double white_r = 1.0;
267         const double white_g = 1.0;
268         const double white_b = 1.0;
269
270         /* Use W3C contrast guideline calculation */
271
272         double white_contrast = (max (r, white_r) - min (r, white_r)) +
273                 (max (g, white_g) - min (g, white_g)) + 
274                 (max (b, white_b) - min (b, white_b));
275
276         double black_contrast = (max (r, black_r) - min (r, black_r)) +
277                 (max (g, black_g) - min (g, black_g)) + 
278                 (max (b, black_b) - min (b, black_b));
279
280         if (white_contrast > black_contrast) {          
281                 /* use white */
282                 return ArdourCanvas::rgba_to_color (1.0, 1.0, 1.0, 1.0);
283         } else {
284                 /* use black */
285                 return ArdourCanvas::rgba_to_color (0.0, 0.0, 0.0, 1.0);
286         }
287 }