diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2014-11-10 20:33:54 -0500 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2014-11-10 20:33:54 -0500 |
commit | fdcb7923582e43f69419023db7985823b2ac1402 (patch) | |
tree | 26529caaa303292594b1a2844bbbfb3848f6485e /libs | |
parent | 405afb757d105d4673cdaf5d652bc6b388bcb935 (diff) |
further work on new color manipulation code
Diffstat (limited to 'libs')
-rw-r--r-- | libs/canvas/canvas/colors.h | 78 | ||||
-rw-r--r-- | libs/canvas/colors.cc | 188 |
2 files changed, 260 insertions, 6 deletions
diff --git a/libs/canvas/canvas/colors.h b/libs/canvas/canvas/colors.h index 80d57c698f..17e93cf128 100644 --- a/libs/canvas/canvas/colors.h +++ b/libs/canvas/canvas/colors.h @@ -17,19 +17,85 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#ifndef __ardour_canvas_colors_h__ +#define __ardour_canvas_colors_h__ + #include <cairomm/context.h> #include "canvas/visibility.h" #include "canvas/types.h" -namespace ArdourCanvas { +namespace ArdourCanvas +{ + +struct LIBCANVAS_API HSV; +struct LIBCANVAS_API HSVA; + +extern LIBCANVAS_API Color hsv_to_color (double h, double s, double v, double a = 1.0); +extern LIBCANVAS_API Color hsv_to_color (const HSV&, double a = 1.0); +extern LIBCANVAS_API Color hsva_to_color (const HSVA&); +extern LIBCANVAS_API void color_to_hsv (Color color, double& h, double& s, double& v); + +extern LIBCANVAS_API void color_to_rgba (Color, double& r, double& g, double& b, double& a); +extern LIBCANVAS_API Color rgba_to_color (double r, double g, double b, double a); + +uint32_t LIBCANVAS_API contrasting_text_color (uint32_t c); + +struct LIBCANVAS_API HSV +{ + HSV (); + HSV (double h, double s, double v); + HSV (Color); + virtual ~HSV() {} + + double h; + double s; + double v; + + bool is_gray() const { return s == 0; } + + operator Color() const { return hsv_to_color (*this); } + + HSV operator+ (const HSV&) const; + HSV operator- (const HSV&) const; + HSV operator* (double) const; + + HSV darker (double factor = 1.3) const { return shade (factor); } + HSV lighter (double factor = 0.7) const { return shade (factor); } - extern LIBCANVAS_API void color_to_hsv (Color color, double& h, double& s, double& v); - extern LIBCANVAS_API Color hsv_to_color (double h, double s, double v, double a); + HSV shade (double factor) const; + HSV mix (const HSV& other, double amt) const; - extern LIBCANVAS_API void color_to_rgba (Color, double& r, double& g, double& b, double& a); - extern LIBCANVAS_API Color rgba_to_color (double r, double g, double b, double a); + void print (std::ostream&) const; + + protected: + virtual void clamp(); +}; + + + +struct LIBCANVAS_API HSVA : public HSV +{ + HSVA (); + HSVA (double h, double s, double v, double a); + HSVA (Color); + + double a; + + operator Color() const { return hsva_to_color (*this); } + + HSVA operator+ (const HSVA&) const; + HSVA operator- (const HSVA&) const; + + void print (std::ostream&) const; + + protected: + void clamp (); +}; - uint32_t LIBCANVAS_API contrasting_text_color (uint32_t c); } +std::ostream& operator<<(std::ostream& o, const ArdourCanvas::HSV& hsv); +std::ostream& operator<<(std::ostream& o, const ArdourCanvas::HSVA& hsva); + +#endif /* __ardour_canvas_colors_h__ */ diff --git a/libs/canvas/colors.cc b/libs/canvas/colors.cc index 3c638935ec..a3a7565fc8 100644 --- a/libs/canvas/colors.cc +++ b/libs/canvas/colors.cc @@ -77,7 +77,12 @@ ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v) } else { s = delta / cmax; } +} +ArdourCanvas::Color +ArdourCanvas::hsv_to_color (const HSV& hsv, double a) +{ + return hsv_to_color (hsv.h, hsv.s, hsv.v, a); } ArdourCanvas::Color @@ -191,3 +196,186 @@ ArdourCanvas::contrasting_text_color (uint32_t c) return (luminance (c) < 0.50) ? white : black; } + +HSV::HSV () + : h (1.0) + , s (1.0) + , v (1.0) +{ +} + +HSV::HSV (double hh, double ss, double vv) + : h (hh) + , s (ss) + , v (vv) +{ +} + +HSV::HSV (Color c) +{ + color_to_hsv (c, h, s, v); +} + +void +HSV::clamp () +{ + s = min (s, 1.0); + v = min (v, 1.0); + h = min (255.0, h); +} + +HSV +HSV::operator+ (const HSV& operand) const +{ + HSV hsv; + hsv.h = h + operand.h; + hsv.s = s + operand.s; + hsv.v = v + operand.v; + hsv.clamp(); + return hsv; +} + +HSV +HSV::operator- (const HSV& operand) const +{ + HSV hsv; + hsv.h = h - operand.h; + hsv.s = s - operand.s; + hsv.v = v - operand.v; + hsv.clamp(); + return hsv; +} + +HSV +HSV::operator* (double d) const +{ + HSV hsv; + hsv.h = h * d; + hsv.s = s * d; + hsv.v = v * d; + hsv.clamp(); + return hsv; +} + +HSV +HSV::shade (double factor) const +{ + HSV hsv (*this); + + /* algorithm derived from a google palette website + and analysis of their color palettes. + + basic rule: to make a color darker, increase its saturation + until it reaches 88%, but then additionally reduce value/lightness + by a larger amount. + + invert rule to make a color lighter. + */ + + if (factor > 1.0) { + if (s < 88) { + hsv.v *= 1.0/(factor/10.0); + } else { + hsv.s *= factor; + } + } else { + if (s < 88) { + hsv.v *= 1.0/factor; + } else { + hsv.s *= factor; + } + } + + hsv.clamp(); + + return hsv; +} + +HSV +HSV::mix (const HSV& other, double amount) const +{ + HSV hsv; + + hsv.h = h + (amount * (other.h - h)); + hsv.v = v + (amount * (other.s - s)); + hsv.s = s + (amount * (other.v - v)); + + hsv.clamp(); + + return hsv; +} + +void +HSV::print (std::ostream& o) const +{ + if (!is_gray()) { + o << "hsv " << h << '|' << s << '|' << v; + } else { + o << "hsv gray"; + } +} + +HSVA::HSVA () + : a (1.0) +{ +} + +HSVA::HSVA (double hh, double ss, double vv, double aa) + : HSV (hh, ss, vv) + , a (aa) +{ +} + +HSVA::HSVA (Color c) +{ + color_to_hsv (c, h, s, v); + a = c & 0xff; +} + +void +HSVA::clamp () +{ + HSV::clamp (); + a = min (1.0, a); +} + +HSVA +HSVA::operator+ (const HSVA& operand) const +{ + HSVA hsv; + hsv.h = h + operand.h; + hsv.s = s + operand.s; + hsv.v = v + operand.v; + hsv.a = a + operand.a; + return hsv; +} + +HSVA +HSVA::operator- (const HSVA& operand) const +{ + HSVA hsv; + hsv.h = h - operand.h; + hsv.s = s - operand.s; + hsv.a = a - operand.a; + return hsv; +} + +void +HSVA::print (std::ostream& o) const +{ + if (!is_gray()) { + o << "hsva " << h << '|' << s << '|' << v << '|' << a; + } else { + o << "hsva gray"; + } +} + + +ArdourCanvas::Color +ArdourCanvas::hsva_to_color (const HSVA& hsva) +{ + return hsv_to_color (hsva.h, hsva.s, hsva.v, hsva.a); +} + +std::ostream& operator<<(std::ostream& o, const ArdourCanvas::HSV& hsv) { hsv.print (o); return o; } +std::ostream& operator<<(std::ostream& o, const ArdourCanvas::HSVA& hsva) { hsva.print (o); return o; } |