From 26d5907fbc45fa12bef12602027f6b10c8e76fde Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Tue, 11 Feb 2003 17:53:38 +0000 Subject: [PATCH] Refactor xforms' colour handling code. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6107 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/xforms/ChangeLog | 14 +++++++ src/frontends/xforms/Color.C | 23 ++++++++++++ src/frontends/xforms/Color.h | 15 ++++++-- src/frontends/xforms/FormPreferences.C | 51 ++++++++++---------------- src/frontends/xforms/FormPreferences.h | 2 - src/frontends/xforms/lyx_gui.C | 28 ++++++-------- src/frontends/xforms/xformsImage.C | 22 +++-------- src/frontends/xforms/xforms_helpers.C | 13 +++++++ src/frontends/xforms/xforms_helpers.h | 4 ++ 9 files changed, 101 insertions(+), 71 deletions(-) diff --git a/src/frontends/xforms/ChangeLog b/src/frontends/xforms/ChangeLog index 27b0de6312..08148e27f4 100644 --- a/src/frontends/xforms/ChangeLog +++ b/src/frontends/xforms/ChangeLog @@ -1,3 +1,17 @@ +2003-02-11 Angus Leeming + + * 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 * bmtable.c: whitespace and indentation consistent with the other diff --git a/src/frontends/xforms/Color.C b/src/frontends/xforms/Color.C index 57a210ba70..c691b284fb 100644 --- a/src/frontends/xforms/Color.C +++ b/src/frontends/xforms/Color.C @@ -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; diff --git a/src/frontends/xforms/Color.h b/src/frontends/xforms/Color.h index 63875dc4eb..0f3ac6c5bc 100644 --- a/src/frontends/xforms/Color.h +++ b/src/frontends/xforms/Color.h @@ -22,6 +22,13 @@ #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 &); }; diff --git a/src/frontends/xforms/FormPreferences.C b/src/frontends/xforms/FormPreferences.C index e189720556..a94c8bef05 100644 --- a/src/frontends/xforms/FormPreferences.C +++ b/src/frontends/xforms/FormPreferences.C @@ -89,6 +89,18 @@ pair 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) diff --git a/src/frontends/xforms/FormPreferences.h b/src/frontends/xforms/FormPreferences.h index 0f235462d2..809e84582c 100644 --- a/src/frontends/xforms/FormPreferences.h +++ b/src/frontends/xforms/FormPreferences.h @@ -129,8 +129,6 @@ private: void Modify(); /// void SwitchColorSpace() const; - /// - string const X11hexname(RGBColor const &) const; /// FormPreferences & parent_; diff --git a/src/frontends/xforms/lyx_gui.C b/src/frontends/xforms/lyx_gui.C index 38c92d7aa0..491534866f 100644 --- a/src/frontends/xforms/lyx_gui.C +++ b/src/frontends/xforms/lyx_gui.C @@ -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; + 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(); + } - if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) { - lyxerr << "X can't find color \"" - << 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()); } diff --git a/src/frontends/xforms/xformsImage.C b/src/frontends/xforms/xformsImage.C index 23ae842235..4b33a6a805 100644 --- a/src/frontends/xforms/xformsImage.C +++ b/src/frontends/xforms/xformsImage.C @@ -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); } diff --git a/src/frontends/xforms/xforms_helpers.C b/src/frontends/xforms/xforms_helpers.C index edd06a4187..6304816598 100644 --- a/src/frontends/xforms/xforms_helpers.C +++ b/src/frontends/xforms/xforms_helpers.C @@ -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) { diff --git a/src/frontends/xforms/xforms_helpers.h b/src/frontends/xforms/xforms_helpers.h index 3e741be2cf..ba3b37ddfa 100644 --- a/src/frontends/xforms/xforms_helpers.h +++ b/src/frontends/xforms/xforms_helpers.h @@ -26,6 +26,10 @@ 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";