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"
24 #include "canvas/colorspace.h"
27 using namespace ArdourCanvas;
33 ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v)
36 color_to_hsva (color, h, s, v, a);
40 ArdourCanvas::color_to_hsva (Color color, double& h, double& s, double& v, double& a)
47 color_to_rgba (color, r, g, b, a);
66 // r = g = b == 0 ... v is undefined, s = 0
74 h = fmod ((g - b)/delta, 6.0);
75 } else if (cmax == g) {
76 h = ((b - r)/delta) + 2;
78 h = ((r - g)/delta) + 4;
84 /* negative values are legal but confusing, because
85 they alias positive values.
91 if (delta == 0 || cmax == 0) {
99 ArdourCanvas::hsva_to_color (double h, double s, double v, double a)
101 s = min (1.0, max (0.0, s));
102 v = min (1.0, max (0.0, v));
105 return rgba_to_color (v, v, v, a);
108 h = fmod (h + 360.0, 360.0);
111 double x = c * (1.0 - fabs(fmod(h / 60.0, 2) - 1.0));
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);
127 return rgba_to_color (m, m, m, a);
131 ArdourCanvas::color_to_rgba (Color color, double& r, double& g, double& b, double& a)
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;
140 ArdourCanvas::rgba_to_color (double r, double g, double b, double a)
142 /* clamp to [0 .. 1] range */
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));
149 /* convert to [0..255] range */
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);
157 /* build-an-integer */
159 return (rc << 24) | (gc << 16) | (bc << 8) | ac;
162 // Inverse of sRGB "gamma" function.
164 inv_gam_sRGB (double c)
169 return pow(((c+0.055)/(1.055)),2.4);
173 // sRGB "gamma" function
177 if (v <= 0.0031308) {
180 v = 1.055 * pow (v, 1.0 / 2.4) - 0.055;
182 return int (v*255+.5);
186 luminance (uint32_t c)
188 // sRGB luminance(Y) values
189 const double rY = 0.212655;
190 const double gY = 0.715158;
191 const double bY = 0.072187;
195 ArdourCanvas::color_to_rgba (c, r, g, b, a);
197 return (gam_sRGB (rY*inv_gam_sRGB(r) + gY*inv_gam_sRGB(g) + bY*inv_gam_sRGB(b))) / 255.0;
201 ArdourCanvas::contrasting_text_color (uint32_t c)
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);
206 return (luminance (c) < 0.50) ? white : black;
219 HSV::HSV (double hh, double ss, double vv, double aa)
226 /* normalize negative hue values into positive range */
233 color_to_hsva (c, h, s, v, a);
237 HSV::is_gray () const
247 /* normalize negative hue values into positive range */
256 HSV::operator+ (const HSV& operand) const
259 hsv.h = h + operand.h;
260 hsv.s = s + operand.s;
261 hsv.v = v + operand.v;
262 hsv.a = a + operand.a;
268 HSV::operator- (const HSV& operand) const
271 hsv.h = h - operand.h;
272 hsv.s = s - operand.s;
273 hsv.v = s - operand.v;
274 hsv.a = a - operand.a;
280 HSV::operator=(Color c)
282 color_to_hsva (c, h, s, v, a);
288 HSV::operator=(const std::string& str)
291 c = strtol (str.c_str(), 0, 16);
292 color_to_hsva (c, h, s, v, a);
298 HSV::operator== (const HSV& other)
300 return h == other.h &&
307 HSV::shade (double factor) const
311 /* algorithm derived from a google palette website
312 and analysis of their color palettes.
314 basic rule: to make a color darker, increase its saturation
315 until it reaches 88%, but then additionally reduce value/lightness
318 invert rule to make a color lighter.
323 hsv.v += (hsv.v * (factor * 10.0));
328 hsv.v -= (hsv.v * (factor * 10.0));
339 HSV::outline () const
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);
345 /* dark color, lighter outline: white with 15% opacity */
346 return HSV (0.0, 0.0, 1.0, 0.15);
351 HSV::mix (const HSV& other, double amount) const
355 hsv.h = h + (amount * (other.h - h));
356 hsv.v = v + (amount * (other.s - s));
357 hsv.s = s + (amount * (other.v - v));
365 HSV::delta (const HSV& other) const
371 /* do not clamp - we are returning a delta */
376 HSV::distance (const HSV& other) const
378 /* Use CIE94 definition for now */
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);
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);
393 // Weighting factors depending on the application (1 = default)
395 const double whtL = 1.0;
396 const double whtC = 1.0;
397 const double whtH = 1.0;
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)));
409 if (sqrt (xDE) > (sqrt (abs (xDL)) + sqrt (abs (xDC)))) {
410 xDH = sqrt ((xDE * xDE) - (xDL * xDL) - (xDC * xDC));
415 const double xSC = 1 + (0.045 * xC1);
416 const double xSH = 1 + (0.015 * xC1);
422 return sqrt ((xDL * xDL) + (xDC * xDC) + (xDH * xDH));
426 HSV::opposite () const
429 hsv.h = fmod (h + 180.0, 360.0);
434 HSV::bw_text () const
436 return HSV (contrasting_text_color (color()));
446 HSV::selected () const
449 return HSV (Color (0xff0000));
454 HSV::print (std::ostream& o) const
457 o << '(' << s << ',' << v << ',' << a << ')';
459 o << "gray(" << v << ')';
464 std::ostream& operator<<(std::ostream& o, const ArdourCanvas::HSV& hsv) { hsv.print (o); return o; }