]> git.lyx.org Git - features.git/commitdiff
Refactor xforms' colour handling code.
authorAngus Leeming <leeming@lyx.org>
Tue, 11 Feb 2003 17:53:38 +0000 (17:53 +0000)
committerAngus Leeming <leeming@lyx.org>
Tue, 11 Feb 2003 17:53:38 +0000 (17:53 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6107 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/xforms/ChangeLog
src/frontends/xforms/Color.C
src/frontends/xforms/Color.h
src/frontends/xforms/FormPreferences.C
src/frontends/xforms/FormPreferences.h
src/frontends/xforms/lyx_gui.C
src/frontends/xforms/xformsImage.C
src/frontends/xforms/xforms_helpers.C
src/frontends/xforms/xforms_helpers.h

index 27b0de6312c7014bb1bca1c78a26ed9befad6920..08148e27f455731d096fc9e370be5de9b316311e 100644 (file)
@@ -1,3 +1,17 @@
+2003-02-11  Angus Leeming  <leeming@lyx.org>
+
+       * xforms_helpers.[Ch] (fl_getmcolor): new function; a wrapper for the
+       xforms routine that accepts unsigned ints.
+
+       * Color.[Ch] (getRGBColor): factorise code into one place.
+
+       * FormPreferences.[Ch] (X11hexname: not a class member. Move to
+       namespace anon.
+
+       * FormPreferences.C: use fl_getmcolor and getRGBColor.
+       * lyx_gui.C: use getRGBColor.
+       * xformsImage.C: use getRGBColor.
+       
 2003-02-11  Angus Leeming  <leeming@lyx.org>
 
        * bmtable.c: whitespace and indentation consistent with the other
index 57a210ba709ec97a216d53214387221815abf6f4..c691b284fb9e22ae64a772acb56d924a26df7a0c 100644 (file)
@@ -32,6 +32,29 @@ int const nohue = -1;
 
 } // namespace anon
 
+
+bool getRGBColor(LColor::color col,
+                unsigned int & r, unsigned int & g, unsigned int & b)
+{
+       string const name = lcolor.getX11Name(col);
+       Display * const display = fl_get_display();
+       Colormap const cmap = fl_state[fl_get_vclass()].colormap;
+       XColor xcol, ccol;
+
+       if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
+               r = 0;
+               g = 0;
+               b = 0;
+               return false;
+       }
+
+       r = xcol.red   / 256;
+       g = xcol.green / 256;
+       b = xcol.blue  / 256;
+       return true;
+}
+
+
 RGBColor::RGBColor(HSVColor const & hsv)
 {
        double h = hsv.h;
index 63875dc4eb335e6e1661073936da737fdc53b188..0f3ac6c5bcc7fac2e2b2a10d84e7f376d2872112 100644 (file)
 #endif
 
 #include "LString.h"
+#include "LColor.h"
+
+/** Given col, fills r, g, b in the range 0-255.
+    The function returns true if successful.
+    It returns false on failure and sets r, g, b to 0. */
+bool getRGBColor(LColor::color col,
+                unsigned int & r, unsigned int & g, unsigned int & b);
 
 struct RGBColor;
 
@@ -36,11 +43,11 @@ struct HSVColor {
 };
 
 struct RGBColor {
-       int r;
-       int g;
-       int b;
+       unsigned int r;
+       unsigned int g;
+       unsigned int b;
        RGBColor() : r(0), g(0), b(0) {}
-       RGBColor(int red, int green, int blue)
+       RGBColor(unsigned int red, unsigned int green, unsigned int blue)
                : r(red), g(green), b(blue) {}
        RGBColor(HSVColor const &);
 };
index e18972055662f838539c5d49499cdd3960d5f688..a94c8bef05f6974e3312681a5c8feae3a6779ffa 100644 (file)
@@ -89,6 +89,18 @@ pair<string,string> parseFontName(string const & name)
 }
 
 
+string const X11hexname(RGBColor const & col)
+{
+       ostringstream ostr;
+
+       ostr << '#' << std::setbase(16) << setfill('0')
+            << setw(2) << col.r
+            << setw(2) << col.g
+            << setw(2) << col.b;
+
+       return STRCONV(ostr.str());
+}
+
 } // namespace anon
 
 
@@ -635,7 +647,7 @@ void FormPreferences::Colors::InputHSV()
        fl_redraw_object(dialog_->button_color);
 
        col = HSVColor(hue, 1.0, 1.0);
-       col.r = max(col.r, 0);
+       col.r = max(col.r, 0u);
        fl_mapcolor(GUI_COLOR_HUE_DIAL, col.r, col.g, col.b);
        fl_redraw_object(dialog_->dial_hue);
 
@@ -755,16 +767,13 @@ void FormPreferences::Colors::LoadBrowserLyX()
                    || lc == LColor::inherit
                    || lc == LColor::ignore) continue;
 
-               string const name = lcolor.getX11Name(lc);
-               Display * display = fl_get_display();;
-               Colormap const colormap = fl_state[fl_get_vclass()].colormap;
-               XColor xcol, ccol;
-
-               if (XLookupColor(display, colormap, name.c_str(), &xcol, &ccol)
-                   == 0) {
+               RGBColor col;
+               bool const success = getRGBColor(lc, col.r, col.g, col.b);
+               if (!success) {
                        lyxerr << "FormPreferences::Colors::LoadBrowserLyX:\n"
                               << "LColor " << lcolor.getLyXName(lc)
-                              << ": X can't find color \"" << name
+                              << ": X can't find color \""
+                              << lcolor.getX11Name(lc)
                               << "\". Set to \"black\"!" << endl;
 
                        string const arg = lcolor.getLyXName(lc) + " black";
@@ -772,16 +781,6 @@ void FormPreferences::Colors::LoadBrowserLyX()
                        continue;
                }
 
-               // X has found the color. Now find the "appropriate" X11 name
-               // for this color.
-
-               // Note that X stores the RGB values in the range 0 - 65535
-               // whilst we require them in the range 0 - 255.
-               RGBColor col;
-               col.r = xcol.red   / 256;
-               col.g = xcol.green / 256;
-               col.b = xcol.blue  / 256;
-
                // Create a valid X11 name of the form "#rrggbb" and change the
                // LColor X11name to this. Don't want to trigger a redraw,
                // as we're just changing the name not the RGB values.
@@ -878,7 +877,7 @@ void FormPreferences::Colors::SwitchColorSpace() const
                fl_set_slider_value(dialog_->slider_value, hsv.v);
 
                col = HSVColor(hsv.h, 1.0, 1.0);
-               col.r = max(col.r, 0);
+               col.r = max(col.r, 0u);
                fl_mapcolor(GUI_COLOR_HUE_DIAL, col.r, col.g, col.b);
                fl_redraw_object(dialog_->dial_hue);
 
@@ -916,18 +915,6 @@ void FormPreferences::Colors::SwitchColorSpace() const
        fl_unfreeze_form(dialog_->form);
 }
 
-string const FormPreferences::Colors::X11hexname(RGBColor const & col) const
-{
-       ostringstream ostr;
-
-       ostr << '#' << std::setbase(16) << setfill('0')
-            << setw(2) << col.r
-            << setw(2) << col.g
-            << setw(2) << col.b;
-
-       return STRCONV(ostr.str());
-}
-
 
 FormPreferences::Converters::Converters(FormPreferences & p)
        : parent_(p)
index 0f235462d285a736d16907f71d354d0571fc5f8a..809e84582cb8c0f050a4ab87cde8524246665f13 100644 (file)
@@ -129,8 +129,6 @@ private:
                void Modify();
                ///
                void SwitchColorSpace() const;
-               ///
-               string const X11hexname(RGBColor const &) const;
 
                ///
                FormPreferences & parent_;
index 38c92d7aa00e745c76d2dcb4eef7a2652a0f0396..491534866f18e5369fe14ef8aafc7bbd73a998d1 100644 (file)
@@ -325,26 +325,20 @@ FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
 
 string const lyx_gui::hexname(LColor::color col)
 {
-       string const name = lcolor.getX11Name(col);
-       Display * const display = fl_get_display();
-       Colormap const cmap = fl_state[fl_get_vclass()].colormap;
-       XColor xcol, ccol;
-
-       if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
-                       lyxerr << "X can't find color \""
-                              << lcolor.getLyXName(col)
-                              << '"' << endl;
-                       return string();
-       }
+       unsigned int r, g, b;
+       bool const success = getRGBColor(col, r, g, b);
+       if (!success) {
+               lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
+                      << '"' << endl;
+               return string();
+        }
 
-       ostringstream os;
+        ostringstream os;
 
-       // Note that X stores the RGB values in the range 0 - 65535
-       // whilst we require them in the range 0 - 255.
        os << setbase(16) << setfill('0')
-          << setw(2) << (xcol.red   / 256)
-          << setw(2) << (xcol.green / 256)
-          << setw(2) << (xcol.blue  / 256);
+          << setw(2) << r
+          << setw(2) << g
+          << setw(2) << b;
 
        return STRCONV(os.str());
 }
index 23ae842235424d4192df88801bb2400a5ef23249..4b33a6a805161bac3b1214b23065d525f1a87228 100644 (file)
@@ -16,7 +16,7 @@
 
 #include "xformsImage.h"
 #include "graphics/GraphicsParams.h"
-#include "LColor.h"
+#include "Color.h"
 #include "converter.h"              // formats
 #include "debug.h"
 #include "support/LAssert.h"
@@ -454,24 +454,14 @@ void init_graphics()
 }
 
 
-unsigned int packedcolor(LColor::color c)
+unsigned int packedcolor(LColor::color col)
 {
-       string const x11color = lcolor.getX11Name(c);
-
-       Display * display = fl_get_display();
-       Colormap cmap     = fl_state[fl_get_vclass()].colormap;
-       XColor xcol;
-       XColor ccol;
-       if (XLookupColor(display, cmap, x11color.c_str(), &xcol, &ccol) == 0)
-               // Unable to parse x11color.
+       unsigned int r, g, b;
+       bool const success = getRGBColor(col, r, g, b);
+       if (!success)
+               // Set to black on failure
                return FL_PACK(255,255,255);
 
-       // Note that X stores the RGB values in the range 0 - 65535
-       // whilst we require them in the range 0 - 255.
-       unsigned int const r = xcol.red   / 256;
-       unsigned int const g = xcol.green / 256;
-       unsigned int const b = xcol.blue  / 256;
-
        return FL_PACK(r, g, b);
 }
 
index edd06a4187edc9ba40db3fe0a532614c78153a0f..63048165986462eac8bb91ed77f387218f3ea28c 100644 (file)
@@ -42,6 +42,19 @@ bool isActive(FL_OBJECT * ob)
 }
 
 
+// A wrapper for the xforms routine, but this one accepts uint args
+unsigned long fl_getmcolor(int i,
+                          unsigned int * r, unsigned int * g, unsigned int * b)
+{
+       int r2, g2, b2;
+       unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
+       *r = r2;
+       *g = g2;
+       *b = b2;
+       return ret_val;
+}
+
+
 // Set an FL_OBJECT to activated or deactivated
 void setEnabled(FL_OBJECT * ob, bool enable)
 {
index 3e741be2cff48716745321dac8de73e029de931d..ba3b37ddfa004a592aa302aa70f7a28568d41cc8 100644 (file)
 
 class LyXLength;
 
+// A wrapper for the xforms routine, but this one accepts uint args
+unsigned long fl_getmcolor(int i, unsigned int * r, unsigned int * g,
+                          unsigned int * b);
+
 // what we always need for lengths
 string const choice_Length_All =
     "cm|mm|in|text%%|col%%|page%%|line%%|ex|em|pt|sp|bp|dd|pc|cc|mu";