scene gap is measured in frames, not msecs.
[ardour.git] / libs / canvas / colors.cc
index be55d2b7e24de985b53a3d785c449425d1f5ff97..a747124cb2fe2212552a7aeaec77296036b46749 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"
 
@@ -30,6 +35,12 @@ using namespace ArdourCanvas;
 using std::max;
 using std::min;
 
+ArdourCanvas::Color
+change_alpha (Color c, double a)
+{
+       return ((c & ~0xff) | (lrintf (a*255.0) & 0xff));
+}
+
 void
 ArdourCanvas::color_to_hsv (Color color, double& h, double& s, double& v)
 {
@@ -201,7 +212,9 @@ 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;
@@ -234,6 +247,26 @@ 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
 {
@@ -376,6 +409,7 @@ HSV::delta (const HSV& other) const
                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;
 }
@@ -409,15 +443,15 @@ HSV::distance (const HSV& other) const
 
        double sL, sA, sB;
        double oL, oA, oB;
-       double r, g, b, a;
+       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, 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, a);
+       color_to_rgba (c, r, g, b, alpha);
        Rgb2Lab (&oL, &oA, &oB, r, g, b);
 
        // Weighting factors depending on the application (1 = default)
@@ -492,3 +526,135 @@ HSV::print (std::ostream& o) const
 
 
 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 (0)
+       , _v (0)
+       , _a (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;
+               /* no-op values for multiply */
+               _s = 1.0;
+               _v = 1.0;
+               _a = 1.0;
+               break;
+       case '+':
+               type = Add;
+               /* no-op values for add */
+               _s = 0.0;
+               _v = 0.0;
+               _a = 0.0;
+               break;
+       case '=':
+               type = Assign;
+               /* this will avoid assignment in operator() (see below) */
+               _s = -1.0;
+               _v = -1.0;
+               _a = -1.0;
+               break;
+       default:
+               throw failed_constructor ();
+       }
+
+       string::size_type pos;
+
+       while (ss) {
+               ss >> mod;
+               if ((pos = mod.find ("alpha:")) != string::npos) {
+                       _a = PBD::atof (mod.substr (pos+6));
+               } else if ((pos = mod.find ("saturate:")) != string::npos) {
+                       _s = PBD::atof (mod.substr (pos+9));
+               } else if ((pos = mod.find ("darkness:")) != string::npos) {
+                       _v = PBD::atof (mod.substr (pos+9));
+               } else {
+                       throw failed_constructor ();
+               }
+       }
+}
+
+string
+SVAModifier::to_string () const
+{
+       PBD::LocaleGuard lg ("C");
+       stringstream ss;
+
+       switch (type) {
+       case Add:
+               ss << '+';
+               break;
+       case Multiply:
+               ss << '*';
+               break;
+       case Assign:
+               ss << '=';
+               break;
+       }
+
+       if (_s >= 0.0) {
+               ss << " saturate:" << _s;
+       }
+
+       if (_v >= 0.0) {
+               ss << " darker:" << _v;
+       }
+
+       if (_a >= 0.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:
+               if (_s >= 0.0) {
+                       r.s = _s;
+               }
+               if (_v >= 0.) {
+                       r.v = _v;
+               }
+               if (_a >= 0.0) {
+                       r.a = _a;
+               }
+               break;
+       }
+
+       return r;
+}
+