add SVAModifier to ArdourCanvas color code
[ardour.git] / libs / canvas / colors.cc
index 3c638935ec51b091971e1c367786ca5642091495..19ab783b78187bb3074b19c03cd522a46fab33ce 100644 (file)
 */
 
 #include <algorithm>
+#include <sstream>
 #include <cmath>
 #include <stdint.h>
+#include <cfloat>
+
+#include "pbd/convert.h"
+#include "pbd/failed_constructor.h"
+#include "pbd/locale_guard.h"
 
 #include "canvas/colors.h"
+#include "canvas/colorspace.h"
 
 using namespace std;
 using namespace ArdourCanvas;
@@ -31,7 +38,14 @@ using std::min;
 void
 ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v)
 {
-       double r, g, b, a;
+       double a;
+       color_to_hsva (color, h, s, v, a);
+}
+
+void
+ArdourCanvas::color_to_hsva (Color color, double& h, double& s, double& v, double& a)
+{
+       double r, g, b;
        double cmax;
        double cmin;
        double delta;
@@ -57,7 +71,8 @@ ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v)
        if (cmax == 0) {
                // r = g = b == 0 ... v is undefined, s = 0
                s = 0.0;  
-               h = -1.0;
+               h = 0.0;
+               return;
        }
 
        if (delta != 0.0) {     
@@ -70,6 +85,13 @@ ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v)
                }
                
                h *= 60.0;
+               
+               if (h < 0.0) {
+                       /* negative values are legal but confusing, because
+                          they alias positive values.
+                       */
+                       h = 360 + h;
+               }
        }
 
        if (delta == 0 || cmax == 0) {
@@ -77,21 +99,19 @@ ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v)
        } else {
                s = delta / cmax;
        }
-
 }
 
 ArdourCanvas::Color
-ArdourCanvas::hsv_to_color (double h, double s, double v, double a)
+ArdourCanvas::hsva_to_color (double h, double s, double v, double a)
 {
        s = min (1.0, max (0.0, s));
        v = min (1.0, max (0.0, v));
 
        if (s == 0) {
-               // achromatic (grey)
                return rgba_to_color (v, v, v, a);
        }
 
-       h = min (360.0, max (0.0, h));
+       h = fmod (h + 360.0, 360.0);
 
        double c = v * s;
         double x = c * (1.0 - fabs(fmod(h / 60.0, 2) - 1.0));
@@ -186,8 +206,431 @@ luminance (uint32_t c)
 uint32_t
 ArdourCanvas::contrasting_text_color (uint32_t c)
 {
-        static const uint32_t white = ArdourCanvas::rgba_to_color (1.0, 1.0, 1.0, 1.0);
+       /* use a slightly off-white... XXX should really look this up */
+
+        static const uint32_t white = ArdourCanvas::rgba_to_color (0.98, 0.98, 0.98, 1.0);
         static const uint32_t black = ArdourCanvas::rgba_to_color (0.0, 0.0, 0.0, 1.0);
 
        return (luminance (c) < 0.50) ? white : black;
 }
+
+
+
+HSV::HSV ()
+       : h (0.0)
+       , s (1.0)
+       , v (1.0)
+       , a (1.0) 
+{
+}
+
+HSV::HSV (double hh, double ss, double vv, double aa)
+       : h (hh)
+       , s (ss)
+       , v (vv)
+       , a (aa) 
+{
+       if (h < 0.0) {
+               /* normalize negative hue values into positive range */
+               h = 360.0 + h;
+       }
+}
+
+HSV::HSV (Color c)
+{
+       color_to_hsva (c, h, s, v, a);
+}
+
+HSV::HSV (const std::string& str)
+{
+       stringstream ss (str);
+       ss >> h;
+       ss >> s;
+       ss >> v;
+       ss >> a;
+}
+
+string
+HSV::to_string () const
+{
+       stringstream ss;
+       ss << h << ' ';
+       ss << s << ' ';
+       ss << v << ' ';
+       ss << a;
+       return ss.str();
+}
+
+bool
+HSV::is_gray () const
+{
+       return s == 0;
+}
+
+void
+HSV::clamp ()
+{
+       h = fmod (h, 360.0);
+       if (h < 0.0) {
+               /* normalize negative hue values into positive range */
+               h = 360.0 + h;
+       }
+       s = min (1.0, s);
+       v = min (1.0, v);
+       a = min (1.0, a);
+}
+
+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.a = a + operand.a;
+       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 = s - operand.v;
+       hsv.a = a - operand.a;
+       hsv.clamp ();
+       return hsv;
+}
+
+HSV&
+HSV::operator=(Color c)
+{
+       color_to_hsva (c, h, s, v, a);
+       clamp ();
+       return *this;
+}
+
+HSV&
+HSV::operator=(const std::string& str)
+{
+       uint32_t c;
+       c = strtol (str.c_str(), 0, 16);
+       color_to_hsva (c, h, s, v, a);
+       clamp ();
+       return *this;
+}
+
+bool
+HSV::operator== (const HSV& other)
+{
+       return h == other.h &&
+               s == other.s &&
+               v == other.v &&
+               a == other.a;
+}
+
+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 += (hsv.v * (factor * 10.0));
+               } 
+               hsv.s *= factor;
+       } else {
+               if (s < 88) {
+                       hsv.v -= (hsv.v * (factor * 10.0));
+               } 
+               hsv.s *= factor;
+       }
+
+       hsv.clamp();
+
+       return hsv;
+}
+
+HSV
+HSV::outline () const
+{
+       if (luminance (color()) < 0.50) {
+               /* light color, darker outline: black with 15% opacity */
+               return HSV (0.0, 0.0, 0.0, 0.15);
+       } else {
+               /* dark color, lighter outline: white with 15% opacity */
+               return HSV (0.0, 0.0, 1.0, 0.15);
+       }
+}
+
+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;
+}
+
+HSV
+HSV::delta (const HSV& other) const
+{
+       HSV d;
+
+       if (is_gray() && other.is_gray()) {
+               d.h = 0.0;
+               d.s = 0.0;
+               d.v = v - other.v;
+       } else {
+               d.h = h - other.h;
+               d.s = s - other.s;
+               d.v = v - other.v;
+       }
+       d.a = a - other.a;
+       /* do not clamp - we are returning a delta */
+       return d;
+}
+
+double
+HSV::distance (const HSV& other) const
+{
+       if (is_gray() && other.is_gray()) {
+               /* human color perception of achromatics generates about 450
+                  distinct colors. By contrast, CIE94 could give a maximal
+                  perceptual distance of sqrt ((360^2) + 1 + 1) = 360. The 450 
+                  are not evenly spread (Webers Law), so lets use 360 as an
+                  approximation of the number of distinct achromatics.
+                  
+                  So, scale up the achromatic difference to give about
+                  a maximal distance between v = 1.0 and v = 0.0 of 360.
+                  
+                  A difference of about 0.0055 will generate a return value of
+                  2, which is roughly the limit of human perceptual
+                  discrimination for chromatics.
+               */
+               return fabs (360.0 * (v - other.v));
+       }
+
+       if (is_gray() != other.is_gray()) {
+               /* no comparison possible */
+               return DBL_MAX;
+       }
+
+       /* Use CIE94 definition for now */
+
+       double sL, sA, sB;
+       double oL, oA, oB;
+       double r, g, b, alpha;  // Careful, "a" is a field of this
+       Color c; 
+
+       c = hsva_to_color (h, s, v, a);
+       color_to_rgba (c, r, g, b, alpha);
+       Rgb2Lab (&sL, &sA, &sB, r, g, b);
+
+       c = hsva_to_color (other.h, other.s, other.v, other.a);
+       color_to_rgba (c, r, g, b, alpha);
+       Rgb2Lab (&oL, &oA, &oB, r, g, b);
+
+       // Weighting factors depending on the application (1 = default)
+
+       const double whtL = 1.0;
+       const double whtC = 1.0;
+       const double whtH = 1.0;  
+
+       const double xC1 = sqrt ((sA * sA) + (sB * oB));
+       const double xC2 = sqrt ((oA * oA) + (oB * oB));
+       double xDL = oL - sL;
+       double xDC = xC2 - xC1;
+       const double xDE = sqrt (((sL - oL) * (sL - oL))
+                                + ((sA - oA) * (sA - oA))
+                                + ((sB - oB) * (sB - oB)));
+       
+       double xDH;
+
+       if (sqrt (xDE) > (sqrt (abs (xDL)) + sqrt (abs (xDC)))) {
+               xDH = sqrt ((xDE * xDE) - (xDL * xDL) - (xDC * xDC));
+       } else {
+               xDH = 0;
+       }
+
+       const double xSC = 1 + (0.045 * xC1);
+       const double xSH = 1 + (0.015 * xC1);
+
+       xDL /= whtL;
+       xDC /= whtC * xSC;
+       xDH /= whtH * xSH;
+       
+       return sqrt ((xDL * xDL) + (xDC * xDC) + (xDH * xDH));
+}
+
+HSV
+HSV::opposite () const
+{
+       HSV hsv (*this);
+       hsv.h = fmod (h + 180.0, 360.0);
+       return hsv;
+}
+
+HSV
+HSV::bw_text () const
+{
+       return HSV (contrasting_text_color (color()));
+}
+
+HSV
+HSV::text () const
+{
+       return opposite ();
+}
+
+HSV
+HSV::selected () const
+{
+       /* XXX hack */
+       return HSV (Color (0xff0000));
+}
+
+
+void
+HSV::print (std::ostream& o) const
+{
+       if (!is_gray()) {
+               o << '(' << h << ',' << s << ',' << v << ',' << a << ')';
+       } else {
+               o << "gray(" << v << ')';
+       }
+}
+
+
+std::ostream& operator<<(std::ostream& o, const ArdourCanvas::HSV& hsv) { hsv.print (o); return o; }
+
+HSV
+HSV::mod (SVAModifier const & svam)
+{
+       return svam (*this);
+}
+
+SVAModifier::SVAModifier (string const &str)
+       : type (Add)
+       , s (-1.0)
+       , v (-1.0)
+       , a (-1.0)
+{
+       from_string (str);
+}
+
+void
+SVAModifier::from_string (string const & str)
+{
+       char op;
+       stringstream ss (str);
+       string mod;
+
+       ss >> op;
+
+       switch (op) {
+       case '*':
+               type = Multiply;
+               break;
+       case '+':
+               type = Add;
+               break;
+       case '=':
+               type = Assign;
+               break;
+       default:
+               throw failed_constructor ();
+       }
+
+       string::size_type pos;
+
+       while (ss) {
+               ss >> mod;
+               if ((pos = mod.find ("alpha:")) != string::npos) {
+                       a = PBD::atoi (mod.substr (pos+6));
+               } else if ((pos = mod.find ("saturate:")) != string::npos) {
+                       s = PBD::atoi (mod.substr (pos+9));
+               } else if ((pos = mod.find ("darkness:")) != string::npos) {
+                       v = PBD::atoi (mod.substr (pos+9));
+               } else {
+                       throw failed_constructor ();
+               }
+       }
+}
+
+string
+SVAModifier::to_string () const
+{
+       PBD::LocaleGuard lg ("POSIX");
+       stringstream ss;
+
+       switch (type) {
+       case Add:
+               ss << '+';
+               break;
+       case Multiply:
+               ss << '*';
+               break;
+       case Assign:
+               ss << '=';
+               break;
+       }
+
+       if (s > -1.0) {
+               ss << " saturate:" << s;
+       }
+
+       if (v > -1.0) {
+               ss << " darker:" << v;
+       }
+
+       if (a > -1.0) {
+               ss << " alpha:" << a;
+       }
+
+       return ss.str();
+}
+
+HSV
+SVAModifier::operator () (HSV& hsv)  const
+{
+       HSV r (hsv);
+       
+       switch (type) {
+       case Add:
+               r.s += s;
+               r.v += v;
+               r.a += a;
+               break;
+       case Multiply:
+               r.s *= s;
+               r.v *= v;
+               r.a *= a;
+               break;
+       case Assign:
+               r.s = s;
+               r.v = v;
+               r.a = a;
+               break;
+       }
+
+       return r;
+}
+