Implements PXEGeek's http://wiki.synfig.com/Wish_list entry: "Optionally display...
authordooglus <dooglus@1f10aa63-cdf2-0310-b900-c93c546f37ac>
Sun, 18 Mar 2007 02:38:18 +0000 (02:38 +0000)
committerdooglus <dooglus@1f10aa63-cdf2-0310-b900-c93c546f37ac>
Sun, 18 Mar 2007 02:38:18 +0000 (02:38 +0000)
git-svn-id: http://svn.voria.com/code@354 1f10aa63-cdf2-0310-b900-c93c546f37ac

synfig-core/trunk/src/synfig/color.cpp
synfig-core/trunk/src/synfig/color.h
synfig-studio/trunk/src/gtkmm/widget_coloredit.cpp
synfig-studio/trunk/src/gtkmm/widget_coloredit.h

index 48f7363..9ef2c03 100644 (file)
@@ -32,6 +32,8 @@
 #include <ETL/angle>
 #include "color.h"
 #include <cstdio>
+#include <sstream>
+#include <iostream>
 
 #endif
 
@@ -51,6 +53,57 @@ using namespace std;
 
 
 
+static ColorReal
+Color::hex2real(String s)
+{
+       std::istringstream i(s);
+       int n;
+       i.fill('0');
+       if (!(i >> hex >> n))
+               throw String("bad conversion from hex string \"") + s + String("\"");
+       return n / 255.0f;
+}
+
+static const String
+Color::real2hex(ColorReal c)
+{
+       std::ostringstream o;
+       o.width(2);
+       o.fill('0');
+       if (c<0) c = 0;
+       if (c>1) c = 1;
+       o << hex << int(c*255.0f);
+       return o.str();
+}
+
+void
+Color::set_hex(String& hex)
+{
+       value_type r, g, b;
+       try
+       {
+               if (hex.size() == 3)
+               {
+                       r = hex2real(hex.substr(0,1)+hex.substr(0,1));
+                       g = hex2real(hex.substr(1,1)+hex.substr(1,1));
+                       b = hex2real(hex.substr(2,1)+hex.substr(2,1));
+                       r_ = r; g_ = g; b_ = b;
+               }
+               else if (hex.size() == 6)
+               {
+                       r = hex2real(hex.substr(0,2));
+                       g = hex2real(hex.substr(2,2));
+                       b = hex2real(hex.substr(4,2));
+                       r_ = r; g_ = g; b_ = b;
+               }
+       }
+       catch (string s)
+       {
+               printf("caught <%s>\n", s.c_str());
+               return;
+       }
+}
+
 #if 0
 Color&
 Color::rotate_uv(const Angle& theta)const
index a6b81ac..3d501a2 100644 (file)
@@ -32,7 +32,7 @@
 #include <math.h>
 #include <cassert>
 #include "gamma.h"
-#include <string.h>
+#include <synfig/string.h>
 
 #ifdef USE_HALF_TYPE
 #include <OpenEXR/half.h>
@@ -107,6 +107,7 @@ public:
 
 private:
        value_type a_, r_, g_, b_;
+       mutable String hex_;
 
 public:
 
@@ -263,6 +264,18 @@ public:
        //! Synonym for get_a(). \see get_a()
        const value_type& get_alpha()const { return get_a(); }
 
+       //! Converts a 2 character hex string \a s (00-ff) into a ColorReal (0.0-1.0)
+       static ColorReal hex2real(String s);
+
+       //! Converts a ColorReal \a c (0.0-1.0) into a 2 character hex string (00-ff)
+       static const String real2hex(ColorReal c);
+
+       //! Returns the color as a 6 character hex sting
+       const String& get_hex()const { return hex_ = real2hex(r_)+real2hex(g_)+real2hex(b_); }
+
+       //! Sets the color's R, G, and B from a 3 or 6 character hex string
+       void set_hex(String& hex);
+
        //! Sets the RED component to \a x
        Color& set_r(const value_type& x) { r_ = x; return *this; }
 
index 1e6c7d2..b2d28a4 100644 (file)
@@ -343,6 +343,17 @@ Widget_ColorEdit::Widget_ColorEdit():
                ATTACH_SPIN_BUTTON(1,G);
                SLIDER_ROW(2,B,_("Blue"));
                ATTACH_SPIN_BUTTON(2,B);
+
+               hex_color_label = manage(new Gtk::Label(_("HTML code"), 0.0, 0.5));
+               hex_color_label->set_use_markup(false);
+               hex_color_label->set_use_underline(false);
+               hex_color_label->set_attributes(attr_list);
+               table->attach(*hex_color_label, 0, 1, 7, 8, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
+
+               hex_color = manage(new Gtk::Entry());
+               hex_color->set_width_chars(8);
+               hex_color->signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_ColorEdit::on_hex_edited));
+               table->attach(*hex_color, 0, 1, 8, 9, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
        }
        {
                Gtk::Table* table(yuv_table);
@@ -408,6 +419,15 @@ Widget_ColorEdit::on_slider_moved(ColorSlider::Type type, float amount)
 }
 
 void
+Widget_ColorEdit::on_hex_edited()
+{
+       Color color(get_value_raw());
+       String s = hex_color->get_text();
+       color.set_hex(s);
+       set_value(color);
+}
+
+void
 Widget_ColorEdit::on_value_changed()
 {
        if(hold_signals)
@@ -424,6 +444,7 @@ Widget_ColorEdit::on_value_changed()
        slider_HUE->set_color(color);
        slider_SAT->set_color(color);
        slider_A->set_color(color);
+       hex_color->set_text(color.get_hex());
        widget_color.set_value(color);
 
        activate();
@@ -488,6 +509,7 @@ Widget_ColorEdit::set_value(const synfig::Color &data)
        slider_HUE->set_color(color);
        slider_SAT->set_color(color);
        slider_A->set_color(color);
+       hex_color->set_text(color.get_hex());
        widget_color.set_value(color);
 
        hold_signals=false;
index 44379c3..63dcfa6 100644 (file)
@@ -123,6 +123,9 @@ class Widget_ColorEdit : public Gtk::Table
        ColorSlider *slider_R;
        ColorSlider *slider_G;
        ColorSlider *slider_B;
+       Gtk::Label *hex_color_label;
+       Gtk::Entry *hex_color;
+
        ColorSlider *slider_A;
        ColorSlider *slider_Y;
        ColorSlider *slider_U;
@@ -161,6 +164,7 @@ public:
        sigc::signal<void>& signal_activate() { return signal_activated_; }
 
        void on_slider_moved(ColorSlider::Type type, float amount);
+       void on_hex_edited();
 
        //Glib::SignalProxy0<void> signal_activate() { return spinbutton_A->signal_activate(); }