2 Copyright (C) 2014 Paul Davis
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.
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.
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.
23 #include "canvas/colors.h"
26 using namespace ArdourCanvas;
32 ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v)
39 color_to_rgba (color, r, g, b, a);
58 // r = g = b == 0 ... v is undefined, s = 0
65 h = fmod ((g - b)/delta, 6.0);
66 } else if (cmax == g) {
67 h = ((b - r)/delta) + 2;
69 h = ((r - g)/delta) + 4;
75 if (delta == 0 || cmax == 0) {
83 ArdourCanvas::hsv_to_color (const HSV& hsv, double a)
85 return hsv_to_color (hsv.h, hsv.s, hsv.v, a);
89 ArdourCanvas::hsv_to_color (double h, double s, double v, double a)
91 s = min (1.0, max (0.0, s));
92 v = min (1.0, max (0.0, v));
96 return rgba_to_color (v, v, v, a);
99 h = min (360.0, max (0.0, h));
102 double x = c * (1.0 - fabs(fmod(h / 60.0, 2) - 1.0));
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);
118 return rgba_to_color (m, m, m, a);
122 ArdourCanvas::color_to_rgba (Color color, double& r, double& g, double& b, double& a)
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;
131 ArdourCanvas::rgba_to_color (double r, double g, double b, double a)
133 /* clamp to [0 .. 1] range */
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));
140 /* convert to [0..255] range */
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);
148 /* build-an-integer */
150 return (rc << 24) | (gc << 16) | (bc << 8) | ac;
153 // Inverse of sRGB "gamma" function.
155 inv_gam_sRGB (double c)
160 return pow(((c+0.055)/(1.055)),2.4);
164 // sRGB "gamma" function
168 if (v <= 0.0031308) {
171 v = 1.055 * pow (v, 1.0 / 2.4) - 0.055;
173 return int (v*255+.5);
177 luminance (uint32_t c)
179 // sRGB luminance(Y) values
180 const double rY = 0.212655;
181 const double gY = 0.715158;
182 const double bY = 0.072187;
186 ArdourCanvas::color_to_rgba (c, r, g, b, a);
188 return (gam_sRGB (rY*inv_gam_sRGB(r) + gY*inv_gam_sRGB(g) + bY*inv_gam_sRGB(b))) / 255.0;
192 ArdourCanvas::contrasting_text_color (uint32_t c)
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);
197 return (luminance (c) < 0.50) ? white : black;
207 HSV::HSV (double hh, double ss, double vv)
216 color_to_hsv (c, h, s, v);
228 HSV::operator+ (const HSV& operand) const
231 hsv.h = h + operand.h;
232 hsv.s = s + operand.s;
233 hsv.v = v + operand.v;
239 HSV::operator- (const HSV& operand) const
242 hsv.h = h - operand.h;
243 hsv.s = s - operand.s;
244 hsv.v = v - operand.v;
250 HSV::operator* (double d) const
261 HSV::shade (double factor) const
265 /* algorithm derived from a google palette website
266 and analysis of their color palettes.
268 basic rule: to make a color darker, increase its saturation
269 until it reaches 88%, but then additionally reduce value/lightness
272 invert rule to make a color lighter.
277 hsv.v *= 1.0/(factor/10.0);
295 HSV::mix (const HSV& other, double amount) const
299 hsv.h = h + (amount * (other.h - h));
300 hsv.v = v + (amount * (other.s - s));
301 hsv.s = s + (amount * (other.v - v));
309 HSV::print (std::ostream& o) const
312 o << "hsv " << h << '|' << s << '|' << v;
323 HSVA::HSVA (double hh, double ss, double vv, double aa)
331 color_to_hsv (c, h, s, v);
343 HSVA::operator+ (const HSVA& operand) const
346 hsv.h = h + operand.h;
347 hsv.s = s + operand.s;
348 hsv.v = v + operand.v;
349 hsv.a = a + operand.a;
354 HSVA::operator- (const HSVA& operand) const
357 hsv.h = h - operand.h;
358 hsv.s = s - operand.s;
359 hsv.a = a - operand.a;
364 HSVA::print (std::ostream& o) const
367 o << "hsva " << h << '|' << s << '|' << v << '|' << a;
375 ArdourCanvas::hsva_to_color (const HSVA& hsva)
377 return hsv_to_color (hsva.h, hsva.s, hsva.v, hsva.a);
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; }