2 Copyright (C) 2011-2013 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
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.
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.
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.
23 #include <cairomm/context.h>
24 #include "canvas/utils.h"
30 ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v)
37 color_to_rgba (color, r, g, b, a);
56 // r = g = b == 0 ... v is undefined, s = 0
63 h = fmod ((g - b)/delta, 6.0);
64 } else if (cmax == g) {
65 h = ((b - r)/delta) + 2;
67 h = ((r - g)/delta) + 4;
73 if (delta == 0 || cmax == 0) {
82 ArdourCanvas::hsv_to_color (double h, double s, double v, double a)
84 s = min (1.0, max (0.0, s));
85 v = min (1.0, max (0.0, v));
89 return rgba_to_color (v, v, v, a);
92 h = min (360.0, max (0.0, h));
95 double x = c * (1.0 - fabs(fmod(h / 60.0, 2) - 1.0));
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);
111 return rgba_to_color (m, m, m, a);
115 ArdourCanvas::color_to_rgba (Color color, double& r, double& g, double& b, double& a)
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;
124 ArdourCanvas::rgba_to_color (double r, double g, double b, double a)
126 /* clamp to [0 .. 1] range */
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));
133 /* convert to [0..255] range */
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);
141 /* build-an-integer */
143 return (rc << 24) | (gc << 16) | (bc << 8) | ac;
147 ArdourCanvas::set_source_rgba (Cairo::RefPtr<Cairo::Context> context, Color color)
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