Am Mittwoch, 22. März 2006 17:18 schrieb Georg Baum:
> Yes. It would be a pity to ditch this code together with the xforms 
frontend
> if it is useful somewhere else.
> I think I am going to apply the patch this evening (will send Changelog
> then, too).

OK, here comes the patch + log. It is going in right now. The difference 
to the last one are only two comments and one mechanical change in 
bufferparams.C I had forgotten.


Georg

Log:

        * src/frontends/xforms/Color.[Ch]: move to src

        * src/Color.[Ch] (getRGBColor): move to src/frontends/*/lyx_gui.C

        * src/BranchList.h
        (Branch::color_): change type to lyx::RGBColor

        * src/BranchList.[Ch]
        (Branch): new constrcutor, set color_ to LColor::background
        (getColor, setColor): adapt to type change of color_

        * src/bufferparams.C
        (BufferParams::writeFile): adapt to type change of branch color

        * src/frontends/lyx_gui.h
        * src/frontends/gtk/lyx_gui.C
        * src/frontends/qt2/lyx_gui.C
        * src/frontends/qt4/lyx_gui.C
        * src/frontends/xforms/lyx_gui.C
        (getRGBColor): move from src/Color.[Ch] here

        * src/frontends/gtk/lyx_gui.C
        * src/frontends/xforms/lyx_gui.C
        (hexname): use getRGBColor

        * src/frontends/gtk/GDocument.C
        (update): adapt to type change of branch color
        (apply): add comment about color chooser

        * src/frontends/qt2/QDocumentDialog.C
        (updateBranchView): adapt to type change of branch color
        (toggleBranchColor): ditto

        * src/frontends/qt2/lcolorcache.[Ch]
        * src/frontends/qt4/lcolorcache.[Ch]
        (rgb2qcolor): new utility function

        * src/frontends/qt4/QBranches.C
        (QBranches::update): adapt to type change of branch color
        (QBranches::on_colorPB_clicked): ditto

        * src/frontends/xforms/FormDocument.C
        (get_current_color): adapt to type change of branch color
        (FormDocument::branch_update):

        * src/frontends/xforms/FormPreferences.C
        (FormPreferences::Colors::LoadBrowse): adapt to RGBColor changes

        * src/frontends/xforms/FormPreferences.h: remove unneeded RGBColor
        forward declaration 

        * src/frontends/xforms/XWorkArea.C
        (XWorkArea::XWorkArea): adapt to RGBColor changes

        * src/frontends/xforms/Makefile.am: remove Color.[Ch]

        * src/frontends/xforms/FormColorpicker.[Ch]: adapt to RGBColor changes

        * src/frontends/xforms/xformsImage.C: adapt to RGBColor changes

        * src/frontends/controllers/ControlDocument.C
        (ControlDocument::dispatchParams): adapt to type change of branch color

        * src/Makefile.am: add Color.[Ch]
Index: src/Color.h
===================================================================
--- src/Color.h	(Revision 13453)
+++ src/Color.h	(Arbeitskopie)
@@ -18,16 +18,7 @@
 
 #include <string>
 
-class LColor_color;
-
 namespace lyx {
-namespace frontend {
-
-/** 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;
 /// returns a string of form #rrggbb, given an RGBColor struct
@@ -78,7 +69,6 @@ bool operator!=(RGBColor const & c1, RGB
 	return !(c1 == c2);
 }
 
-} // namespace frontend
 } // namespace lyx
 
 #endif
Index: src/BranchList.C
===================================================================
--- src/BranchList.C	(Revision 13453)
+++ src/BranchList.C	(Arbeitskopie)
@@ -11,11 +11,19 @@
 #include <config.h>
 
 #include "BranchList.h"
+#include "LColor.h"
+#include "frontends/lyx_gui.h"
 #include <algorithm>
 
 using std::string;
 
 
+Branch::Branch()
+{
+	lyx_gui::getRGBColor(LColor::background, color_);
+}
+
+
 string const & Branch::getBranch() const
 {
 	return branch_;
@@ -43,18 +51,28 @@ bool Branch::setSelected(bool b)
 }
 
 
-string const & Branch::getColor() const
+lyx::RGBColor const & Branch::getColor() const
 {
 	return color_;
 }
 
 
-void Branch::setColor(string const & c)
+void Branch::setColor(lyx::RGBColor const & c)
 {
 	color_ = c;
 }
 
 
+void Branch::setColor(string const & c)
+{
+	if (c.size() == 7 && c[0] == '#')
+		color_ = lyx::RGBColor(c);
+	else
+		// no color set or invalid color - use normal background
+		lyx_gui::getRGBColor(LColor::background, color_);
+}
+
+
 Branch * BranchList::find(std::string const & name)
 {
 	List::iterator it =
@@ -91,7 +109,6 @@ bool BranchList::add(string const & s)
 			Branch br;
 			br.setBranch(name);
 			br.setSelected(false);
-			br.setColor("none");
 			list.push_back(br);
 		}
 		if (j == string::npos)
Index: src/BranchList.h
===================================================================
--- src/BranchList.h	(Revision 13453)
+++ src/BranchList.h	(Arbeitskopie)
@@ -30,6 +30,8 @@
 #ifndef BRANCHES_H
 #define BRANCHES_H
 
+#include "Color.h"
+
 #include <string>
 #include <list>
 
@@ -37,6 +39,8 @@
 class Branch {
 public:
 	///
+	Branch();
+	///
 	std::string const & getBranch() const;
 	///
 	void setBranch(std::string const &);
@@ -47,8 +51,15 @@ public:
 	 */
 	bool setSelected(bool);
 	///
-	std::string const & getColor() const;
+	lyx::RGBColor const & getColor() const;
 	///
+	void setColor(lyx::RGBColor const &);
+	/**
+	 * Set color from a string "#rrggbb".
+	 * Use LColor:background if the string is no valid color.
+	 * This ensures compatibility with LyX 1.4.0 that had the symbolic
+	 * color "none" that was displayed as LColor:background.
+	 */
 	void setColor(std::string const &);
 
 
@@ -58,7 +69,7 @@ private:
 	///
 	bool selected_;
 	///
-	std::string color_;
+	lyx::RGBColor color_;
 };
 
 
Index: src/bufferparams.C
===================================================================
--- src/bufferparams.C	(Revision 13453)
+++ src/bufferparams.C	(Arbeitskopie)
@@ -601,7 +601,7 @@ void BufferParams::writeFile(ostream & o
 	for (; it != end; ++it) {
 		os << "\\branch " << it->getBranch()
 		   << "\n\\selected " << it->getSelected()
-		   << "\n\\color " << it->getColor()
+		   << "\n\\color " << lyx::X11hexname(it->getColor())
 		   << "\n\\end_branch"
 		   << "\n";
 	}
Index: src/frontends/gtk/lyx_gui.C
===================================================================
--- src/frontends/gtk/lyx_gui.C	(Revision 13453)
+++ src/frontends/gtk/lyx_gui.C	(Arbeitskopie)
@@ -26,6 +26,7 @@
 #include "funcrequest.h"
 #include "gettext.h"
 
+#include "Color.h"
 #include "LColor.h"
 #include "LyXAction.h"
 #include "lyx_main.h"
@@ -175,23 +176,44 @@ FuncStatus lyx_gui::getStatus(FuncReques
 }
 
 
-string const lyx_gui::hexname(LColor_color col)
+bool lyx_gui::getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
 {
 	Gdk::Color gdkColor;
 	Gdk::Color * gclr = colorCache.getColor(col);
 	if (!gclr) {
 		gclr = &gdkColor;
-		gclr->parse(lcolor.getX11Name(col));
+		if(!gclr->parse(lcolor.getX11Name(col))) {
+			rgbcol.r = 0;
+			rgbcol.g = 0;
+			rgbcol.b = 0;
+			return false;
+		}
 	}
 
-	std::ostringstream os;
-
 	// Note that X stores the RGB values in the range 0 - 65535
 	// whilst we require them in the range 0 - 255.
+	rgbcol.r = gclr->get_red() / 256;
+	rgbcol.g = gclr->get_green() / 256;
+	rgbcol.b = gclr->get_blue() / 256;
+	return true;
+}
+
+
+string const lyx_gui::hexname(LColor_color col)
+{
+	lyx::RGBColor rgbcol;
+	if (!getRGBColor(col, rgbcol)) {
+		lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
+		       << '"' << std::endl;
+		return string();
+	}
+
+	std::ostringstream os;
+
 	os << std::setbase(16) << std::setfill('0')
-	   << std::setw(2) << (gclr->get_red() / 256)
-	   << std::setw(2) << (gclr->get_green() / 256)
-	   << std::setw(2) << (gclr->get_blue() / 256);
+	   << std::setw(2) << rgbcol.r
+	   << std::setw(2) << rgbcol.g
+	   << std::setw(2) << rgbcol.b;
 
 	return os.str();
 }
Index: src/frontends/gtk/GDocument.C
===================================================================
--- src/frontends/gtk/GDocument.C	(Revision 13453)
+++ src/frontends/gtk/GDocument.C	(Arbeitskopie)
@@ -529,7 +529,7 @@ void GDocument::update()
 		(*row)[branchColName_] = (*it).getBranch();
 		std::cerr << "update: loading '" << (*it).getBranch() << "'\n";
 		(*row)[branchColActivated_] = (*it).getSelected();
-		(*row)[branchColColor_] = (*it).getColor();
+		(*row)[branchColColor_] = X11hexname((*it).getColor());
 	}
 	// *** End "Branches" Page ***
 
@@ -720,6 +720,10 @@ void GDocument::apply()
 			Branch * newbranch = branchlist.find(name);
 			newbranch->setSelected((*row)[branchColActivated_]);
 			Glib::ustring const color = (*row)[branchColColor_];
+			// FIXME: The color should be editable via a color
+			// chooser, not a text field (see qt/xforms frontends)
+			// setColor will silently ignore an invalid color and
+			// use the normal background color for now.
 			newbranch->setColor(color);
 		}
 	}
Index: src/frontends/qt2/lyx_gui.C
===================================================================
--- src/frontends/qt2/lyx_gui.C	(Revision 13453)
+++ src/frontends/qt2/lyx_gui.C	(Arbeitskopie)
@@ -16,6 +16,7 @@
 // FIXME: move this stuff out again
 #include "bufferlist.h"
 #include "BufferView.h"
+#include "Color.h"
 #include "funcrequest.h"
 #include "LColor.h"
 #include "lyx_main.h"
@@ -300,6 +301,22 @@ FuncStatus getStatus(FuncRequest const &
 }
 
 
+bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
+{
+	QColor const & qcol = lcolorcache.get(col);
+	if (!qcol.isValid()) {
+		rgbcol.r = 0;
+		rgbcol.g = 0;
+		rgbcol.b = 0;
+		return false;
+	}
+	rgbcol.r = qcol.red();
+	rgbcol.g = qcol.green();
+	rgbcol.b = qcol.blue();
+	return true;
+}
+
+
 string const hexname(LColor_color col)
 {
 	return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
Index: src/frontends/qt2/QDocumentDialog.C
===================================================================
--- src/frontends/qt2/QDocumentDialog.C	(Revision 13453)
+++ src/frontends/qt2/QDocumentDialog.C	(Arbeitskopie)
@@ -14,6 +14,7 @@
 #include "QDocumentDialog.h"
 
 #include "floatplacement.h"
+#include "lcolorcache.h"
 #include "lengthcombo.h"
 #include "validators.h"
 #include "panelstack.h"
@@ -421,10 +422,7 @@ void QDocumentDialog::updateBranchView()
 		QString const sel = it->getSelected() ? qt_("Yes") : qt_("No");
 		QListViewItem * newItem =
 			new QListViewItem(branchesModule->branchesLV, bname, sel);
-		string const x11hexname = it->getColor();
-		QColor itemcolor;
-		if (x11hexname[0] == '#')
-			itemcolor.setNamedColor(toqstr(x11hexname));
+		QColor const itemcolor = rgb2qcolor(it->getColor());
 		if (itemcolor.isValid()) {
 			QPixmap coloritem(30, 10);
 			coloritem.fill(itemcolor);
@@ -503,16 +501,13 @@ void QDocumentDialog::toggleBranchColor(
 	if (selItem != 0)
 		sel_branch = selItem->text(0);
 	if (sel_branch) {
-		QColor initial;
 		string current_branch = fromqstr(sel_branch);
 		Branch * branch =
 			form_->branchlist_.find(current_branch);
 		if (!branch)
 			return;
 
-		string x11hexname = branch->getColor();
-		if (x11hexname[0] == '#')
-			initial.setNamedColor(toqstr(x11hexname));
+		QColor const initial = rgb2qcolor(branch->getColor());
 		QColor ncol(QColorDialog::getColor(initial, qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget()));
 		if (ncol.isValid()){
 			// add the color to the branchlist
Index: src/frontends/qt2/lcolorcache.C
===================================================================
--- src/frontends/qt2/lcolorcache.C	(Revision 13453)
+++ src/frontends/qt2/lcolorcache.C	(Arbeitskopie)
@@ -12,6 +12,7 @@
 
 #include "lcolorcache.h"
 
+#include "Color.h"
 #include "LColor.h"
 
 LColorCache lcolorcache;
@@ -38,3 +39,9 @@ void LColorCache::clear()
 {
 	colormap.clear();
 }
+
+
+QColor const rgb2qcolor(lyx::RGBColor const & rgb)
+{
+	return QColor(rgb.r, rgb.g, rgb.b);
+}
Index: src/frontends/qt2/lcolorcache.h
===================================================================
--- src/frontends/qt2/lcolorcache.h	(Revision 13453)
+++ src/frontends/qt2/lcolorcache.h	(Arbeitskopie)
@@ -19,6 +19,11 @@
 #include <qcolor.h>
 
 
+namespace lyx {
+struct RGBColor;
+}
+
+
 // FIXME: use a fixed-size array not a map ?
 
 /**
@@ -43,4 +48,6 @@ private:
 /// singleton instance
 extern LColorCache lcolorcache;
 
+///
+QColor const rgb2qcolor(lyx::RGBColor const &);
 #endif // LCOLORCACHE_H
Index: src/frontends/qt4/QBranches.C
===================================================================
--- src/frontends/qt4/QBranches.C	(Revision 13453)
+++ src/frontends/qt4/QBranches.C	(Arbeitskopie)
@@ -13,6 +13,7 @@
 
 #include "QBranches.h"
 
+#include "lcolorcache.h"
 #include "validators.h"
 #include "qt_helpers.h"
 
@@ -68,10 +69,7 @@ void QBranches::update()
 		QString const sel = it->getSelected() ? qt_("Yes") : qt_("No");
 		Q3ListViewItem * newItem =
 			new Q3ListViewItem(branchesLV, bname, sel);
-		string const x11hexname = it->getColor();
-		QColor itemcolor;
-		if (x11hexname[0] == '#')
-			itemcolor.setNamedColor(toqstr(x11hexname));
+		QColor const itemcolor = rgb2qcolor(it->getColor());
 		if (itemcolor.isValid()) {
 			QPixmap coloritem(30, 10);
 			coloritem.fill(itemcolor);
@@ -154,16 +152,13 @@ void QBranches::on_colorPB_clicked()
 	if (selItem != 0)
 		sel_branch = selItem->text(0);
 	if (!sel_branch.isEmpty()) {
-		QColor initial("lightskyblue");
 		string current_branch = fromqstr(sel_branch);
 		Branch * branch =
 			branchlist_.find(current_branch);
 		if (!branch)
 			return;
 
-		string x11hexname = branch->getColor();
-		if (x11hexname[0] == '#')
-			initial.setNamedColor(toqstr(x11hexname));
+		QColor const initial = rgb2qcolor(branch->getColor());
 		QColor ncol(QColorDialog::getColor(initial, qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget()));
 		if (ncol.isValid()){
 			// add the color to the branchlist
Index: src/frontends/qt4/lyx_gui.C
===================================================================
--- src/frontends/qt4/lyx_gui.C	(Revision 13453)
+++ src/frontends/qt4/lyx_gui.C	(Arbeitskopie)
@@ -16,6 +16,7 @@
 // FIXME: move this stuff out again
 #include "bufferlist.h"
 #include "BufferView.h"
+#include "Color.h"
 #include "funcrequest.h"
 #include "LColor.h"
 #include "lyx_main.h"
@@ -294,6 +295,22 @@ FuncStatus getStatus(FuncRequest const &
 }
 
 
+bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
+{
+	QColor const & qcol = lcolorcache.get(col);
+	if (!qcol.isValid()) {
+		rgbcol.r = 0;
+		rgbcol.g = 0;
+		rgbcol.b = 0;
+		return false;
+	}
+	rgbcol.r = qcol.red();
+	rgbcol.g = qcol.green();
+	rgbcol.b = qcol.blue();
+	return true;
+}
+
+
 string const hexname(LColor_color col)
 {
 	return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
Index: src/frontends/qt4/lcolorcache.C
===================================================================
--- src/frontends/qt4/lcolorcache.C	(Revision 13453)
+++ src/frontends/qt4/lcolorcache.C	(Arbeitskopie)
@@ -10,6 +10,7 @@
 
 #include <config.h>
 
+#include "Color.h"
 #include "lcolorcache.h"
 
 #include "LColor.h"
@@ -51,3 +52,9 @@ void LColorCache::clear()
 {
 	colormap.clear();
 }
+
+
+QColor const rgb2qcolor(lyx::RGBColor const & rgb)
+{
+	return QColor(rgb.r, rgb.g, rgb.b);
+}
Index: src/frontends/qt4/lcolorcache.h
===================================================================
--- src/frontends/qt4/lcolorcache.h	(Revision 13453)
+++ src/frontends/qt4/lcolorcache.h	(Arbeitskopie)
@@ -19,6 +19,11 @@
 #include <QColor>
 
 
+namespace lyx {
+struct RGBColor;
+}
+
+
 // FIXME: use a fixed-size array not a map ?
 
 /**
@@ -43,4 +48,6 @@ private:
 /// singleton instance
 extern LColorCache lcolorcache;
 
+///
+QColor const rgb2qcolor(lyx::RGBColor const &);
 #endif // LCOLORCACHE_H
Index: src/frontends/xforms/FormDocument.C
===================================================================
--- src/frontends/xforms/FormDocument.C	(Revision 13453)
+++ src/frontends/xforms/FormDocument.C	(Arbeitskopie)
@@ -678,21 +678,13 @@ RGBColor get_current_color(FL_OBJECT * b
 {
 	BOOST_ASSERT(browser && browser->objclass == FL_BROWSER);
 
-	RGBColor color;
-
 	int const i = fl_get_browser(browser);
 	string const branch_name = fl_get_browser_line(browser, i);
 	Branch const * branch = branchlist.find(branch_name);
 	if (!branch)
-		return color;
+		return RGBColor();
 
-	string const x11hexname = branch->getColor();
-	if (x11hexname[0] == '#') {
-		color = RGBColor(x11hexname);
-	} else{
-		fl_getmcolor(FL_COL1, &color.r, &color.g, &color.b);
-	}
-	return color;
+	return branch->getColor();
 }
 
 } // namespace anon
@@ -1298,20 +1290,16 @@ void FormDocument::branch_update(BufferP
 
 	// display proper colour...
 	RGBColor rgb;
-	string x11hexname;
 	if (current_branch == "none")
-		x11hexname = "none";
+		fl_getmcolor(FL_COL1, &rgb.r, &rgb.g, &rgb.b);
 	else {
 		Branch * branch = branchlist_.find(current_branch);
 		if (branch)
-			x11hexname = branch->getColor();
+			rgb = branch->getColor();
+		else
+			fl_getmcolor(FL_COL1, &rgb.r, &rgb.g, &rgb.b);
 	}
 
-	if (x11hexname[0] == '#') {
-		rgb = RGBColor(x11hexname);
-	} else {
-		fl_getmcolor(FL_COL1, &rgb.r, &rgb.g, &rgb.b);
-	}
 	fl_mapcolor(GUI_COLOR_CHOICE, rgb.r, rgb.g, rgb.b);
 	fl_redraw_object(branch_->button_color);
 
Index: src/frontends/xforms/lyx_gui.C
===================================================================
--- src/frontends/xforms/lyx_gui.C	(Revision 13453)
+++ src/frontends/xforms/lyx_gui.C	(Arbeitskopie)
@@ -51,7 +51,6 @@ using lyx::support::AddName;
 using lyx::support::package;
 
 using lyx::frontend::fontloader;
-using lyx::frontend::getRGBColor;
 using lyx::frontend::lyxColorHandler;
 using lyx::frontend::LyXColorHandler;
 using lyx::frontend::XformsColor;
@@ -348,11 +347,34 @@ FuncStatus getStatus(FuncRequest const &
 	return FuncStatus();
 }
 
+
+bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
+{
+	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) {
+		rgbcol.r = 0;
+		rgbcol.g = 0;
+		rgbcol.b = 0;
+		return false;
+	}
+
+	// Note that X stores the RGB values in the range 0 - 65535
+	// whilst we require them in the range 0 - 255.
+	rgbcol.r = xcol.red   / 256;
+	rgbcol.g = xcol.green / 256;
+	rgbcol.b = xcol.blue  / 256;
+	return true;
+}
+
+
 string const hexname(LColor_color col)
 {
-	unsigned int r, g, b;
-	bool const success = getRGBColor(col, r, g, b);
-	if (!success) {
+	lyx::RGBColor rgbcol;
+	if (!getRGBColor(col, rgbcol)) {
 		lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
 		       << '"' << endl;
 		return string();
@@ -361,9 +383,9 @@ string const hexname(LColor_color col)
 	ostringstream os;
 
 	os << setbase(16) << setfill('0')
-	   << setw(2) << r
-	   << setw(2) << g
-	   << setw(2) << b;
+	   << setw(2) << rgbcol.r
+	   << setw(2) << rgbcol.g
+	   << setw(2) << rgbcol.b;
 
 	return os.str();
 }
Index: src/frontends/xforms/Color.h
===================================================================
--- src/frontends/xforms/Color.h	(Revision 13453)
+++ src/frontends/xforms/Color.h	(Arbeitskopie)
@@ -1,84 +0,0 @@
-// -*- C++ -*-
-/**
- * \file Color.h
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Angus Leeming
- *
- * Full author contact details are available in file CREDITS.
- */
-
-/* structs RGBColor and HSVColor to enable simple conversion between
- * color spaces.
- */
-
-#ifndef COLOR_H
-#define COLOR_H
-
-#include <string>
-
-class LColor_color;
-
-namespace lyx {
-namespace frontend {
-
-/** 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;
-/// returns a string of form #rrggbb, given an RGBColor struct
-std::string const X11hexname(RGBColor const & col);
-
-struct HSVColor {
-	double h;
-	double s;
-	double v;
-	HSVColor() : h(0.0), s(0.0), v(0.0) {}
-	HSVColor(double hue, double sat, double val)
-		: h(hue), s(sat), v(val) {}
-	HSVColor(RGBColor const &);
-};
-
-struct RGBColor {
-	unsigned int r;
-	unsigned int g;
-	unsigned int b;
-	RGBColor() : r(0), g(0), b(0) {}
-	RGBColor(unsigned int red, unsigned int green, unsigned int blue)
-		: r(red), g(green), b(blue) {}
-	RGBColor(HSVColor const &);
-	/// \param x11hexname is of the form "#ffa071"
-	RGBColor(std::string const & x11hexname);
-};
-
-struct NamedColor : public RGBColor {
-	std::string lyxname;
-	std::string guiname;
-	NamedColor() : RGBColor() {}
-	NamedColor(std::string const & lyx, std::string const & gui,
-		   RGBColor const & c)
-		: RGBColor(c), lyxname(lyx), guiname(gui) {}
-	RGBColor const & color() const { return *this; }
-};
-
-inline
-bool operator==(RGBColor const & c1, RGBColor const & c2)
-{
-	return (c1.r == c2.r && c1.g == c2.g && c1.b == c2.b);
-}
-
-
-inline
-bool operator!=(RGBColor const & c1, RGBColor const & c2)
-{
-	return !(c1 == c2);
-}
-
-} // namespace frontend
-} // namespace lyx
-
-#endif
Index: src/frontends/xforms/FormPreferences.C
===================================================================
--- src/frontends/xforms/FormPreferences.C	(Revision 13453)
+++ src/frontends/xforms/FormPreferences.C	(Arbeitskopie)
@@ -27,6 +27,7 @@
 #include "lastfiles.h"
 #include "LColor.h"
 #include "lyxfont.h"
+#include "frontends/lyx_gui.h"
 
 #include "support/convert.h"
 #include "support/lstrings.h"
@@ -38,6 +39,8 @@
 
 #include <iomanip>
 
+using lyx::RGBColor;
+
 using std::endl;
 using std::make_pair;
 using std::max;
@@ -621,7 +624,7 @@ void FormPreferences::Colors::LoadBrowse
 		    || lc == LColor::ignore) continue;
 
 		RGBColor col;
-		bool const success = getRGBColor(lc, col.r, col.g, col.b);
+		bool const success = lyx_gui::getRGBColor(lc, col);
 		if (!success) {
 			lyxerr << "FormPreferences::Colors::LoadBrowserLyX:\n"
 			       << "LColor " << lcolor.getLyXName(lc)
Index: src/frontends/xforms/FormPreferences.h
===================================================================
--- src/frontends/xforms/FormPreferences.h	(Revision 13453)
+++ src/frontends/xforms/FormPreferences.h	(Arbeitskopie)
@@ -50,7 +50,6 @@ struct FD_preferences_screen_fonts;
 struct FD_preferences_spelloptions;
 
 class FormColorpicker;
-class RGBColor;
 
 /** This class provides an XForms implementation of the FormPreferences Dialog.
  *  The preferences dialog allows users to set/save their preferences.
Index: src/frontends/xforms/XWorkArea.C
===================================================================
--- src/frontends/xforms/XWorkArea.C	(Revision 13453)
+++ src/frontends/xforms/XWorkArea.C	(Arbeitskopie)
@@ -16,6 +16,7 @@
 #include "Color.h"
 #include "XFormsView.h"
 #include "XLyXKeySym.h"
+#include "lyx_gui.h"
 
 #include "debug.h"
 #include "funcrequest.h"
@@ -119,9 +120,9 @@ XWorkArea::XWorkArea(LyXView & owner, in
 	fl_set_object_resize(obj, FL_RESIZE_ALL);
 	fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
 
-	unsigned int r, g, b;
-	if (getRGBColor(LColor::background, r, g, b)) {
-		fl_mapcolor(FL_FREE_COL12, r, g, b);
+	RGBColor col;
+	if (lyx_gui::getRGBColor(LColor::background, col)) {
+		fl_mapcolor(FL_FREE_COL12, col.r, col.g, col.b);
 		fl_set_object_color(obj, FL_FREE_COL12, FL_MCOL);
 	}
 
Index: src/frontends/xforms/Makefile.am
===================================================================
--- src/frontends/xforms/Makefile.am	(Revision 13453)
+++ src/frontends/xforms/Makefile.am	(Arbeitskopie)
@@ -60,8 +60,6 @@ libxforms_la_SOURCES = \
 	xscreen.C \
 	xscreen.h \
 	Alert_pimpl.C \
-	Color.C \
-	Color.h \
 	ColorHandler.C \
 	ColorHandler.h \
 	Dialogs.C \
Index: src/frontends/xforms/FormColorpicker.C
===================================================================
--- src/frontends/xforms/FormColorpicker.C	(Revision 13453)
+++ src/frontends/xforms/FormColorpicker.C	(Arbeitskopie)
@@ -24,6 +24,7 @@
 
 #include "lyx_forms.h"
 
+using lyx::RGBColor;
 using std::string;
 
 namespace lyx {
Index: src/frontends/xforms/FormColorpicker.h
===================================================================
--- src/frontends/xforms/FormColorpicker.h	(Revision 13453)
+++ src/frontends/xforms/FormColorpicker.h	(Arbeitskopie)
@@ -35,7 +35,7 @@ public:
 	    a color is chosen (or the dialog is closed).
 	    \param color the color used to initialise the dialog.
 	 */
-	RGBColor const & requestColor(RGBColor const & color);
+	lyx::RGBColor const & requestColor(lyx::RGBColor const & color);
 
 	/** Input callback function.
 	 *  Invoked only by the xforms callback interface
@@ -71,8 +71,8 @@ private:
 	/// The title displayed by the Window Manager.
 	std::string title_;
 
-	RGBColor input_color_;
-	RGBColor color_;
+	lyx::RGBColor input_color_;
+	lyx::RGBColor color_;
 	bool finished_;
 
 	/// Passed to the window manager to give a pretty little symbol ;-)
Index: src/frontends/xforms/xformsImage.C
===================================================================
--- src/frontends/xforms/xformsImage.C	(Revision 13453)
+++ src/frontends/xforms/xformsImage.C	(Arbeitskopie)
@@ -12,6 +12,7 @@
 
 #include "xformsImage.h"
 #include "Color.h"
+#include "lyx_gui.h"
 
 #include "debug.h"
 #include "format.h"
@@ -33,8 +34,6 @@
 #include <boost/bind.hpp>
 #include <boost/tuple/tuple.hpp>
 
-using lyx::frontend::getRGBColor;
-
 using lyx::support::float_equal;
 using lyx::support::prefixIs;
 using lyx::support::rtrim;
@@ -470,13 +469,13 @@ void init_graphics()
 
 unsigned int packedcolor(LColor::color col)
 {
-	unsigned int r, g, b;
-	bool const success = getRGBColor(col, r, g, b);
+	lyx::RGBColor rgb;
+	bool const success = lyx_gui::getRGBColor(col, rgb);
 	if (!success)
 		// Set to black on failure
 		return FL_PACK(255, 255, 255);
 
-	return FL_PACK(r, g, b);
+	return FL_PACK(rgb.r, rgb.g, rgb.b);
 }
 
 } // namespace anon
Index: src/frontends/xforms/Color.C
===================================================================
--- src/frontends/xforms/Color.C	(Revision 13453)
+++ src/frontends/xforms/Color.C	(Arbeitskopie)
@@ -1,203 +0,0 @@
-/**
- * \file Color.C
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Angus Leeming
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#include <config.h>
-
-#include "Color.h"
-
-#include "lyx_forms.h"
-
-#include "LColor.h"
-
-#include <cmath>
-#include <sstream>
-#include <iomanip>
-
-#ifndef CXX_GLOBAL_CSTD
-using std::floor;
-#endif
-
-using std::max;
-using std::min;
-using std::setw;
-
-using std::istringstream;
-using std::ostringstream;
-using std::string;
-
-namespace lyx {
-namespace frontend {
-
-namespace {
-
-int const nohue = -1;
-
-int hexstrToInt(string const & str)
-{
-        int val = 0;
-        istringstream is(str);
-        is >> std::setbase(16) >> val;
-        return val;
-}
-
-} // 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;
-}
-
-
-string const X11hexname(RGBColor const & col)
-{
-	ostringstream ostr;
-
-	ostr << '#' << std::setbase(16) << std::setfill('0')
-	     << setw(2) << col.r
-	     << setw(2) << col.g
-	     << setw(2) << col.b;
-
-	return ostr.str();
-}
-
-
-RGBColor::RGBColor(string const & x11hexname)
-	: r(0), g(0), b(0)
-{
-	BOOST_ASSERT(x11hexname.size() == 7 && x11hexname[0] == '#');
-	r = hexstrToInt(x11hexname.substr(1,2));
-	g = hexstrToInt(x11hexname.substr(3,2));
-	b = hexstrToInt(x11hexname.substr(5,2));
-}
-
-
-RGBColor::RGBColor(HSVColor const & hsv)
-{
-	double h = hsv.h;
-	double const s = hsv.s;
-	double const v = hsv.v;
-
-	double rd, gd, bd;
-
-	if (h == nohue || s == 0.0) {
-		rd = gd = bd = v;
-	} else {
-		if (h == 360.0) h = 0.0;
-		h /= 60.0;
-
-		int const j = max(0, static_cast<int>(::floor(h)));
-		//if (j < 0) j = 0;
-
-		double const f = h - j;
-		double const p = v * (1.0 - s);
-		double const q = v * (1.0 - (s * f));
-		double const t = v * (1.0 - (s * (1.0 - f)));
-
-		switch (j) {
-		case 0:
-			rd = v;
-			gd = t;
-			bd = p;
-			break;
-		case 1:
-			rd = q;
-			gd = v;
-			bd = p;
-			break;
-		case 2:
-			rd = p;
-			gd = v;
-			bd = t;
-			break;
-		case 3:
-			rd = p;
-			gd = q;
-			bd = v;
-			break;
-		case 4:
-			rd = t;
-			gd = p;
-			bd = v;
-			break;
-		case 5:
-			rd = v;
-			gd = p;
-			bd = q;
-			break;
-		default:
-			rd = v;
-			gd = t;
-			bd = p;
-			break;  // should never happen.
-		}
-	}
-
-	r = static_cast<int>(::floor((rd * 255.0) + 0.5));
-	g = static_cast<int>(::floor((gd * 255.0) + 0.5));
-	b = static_cast<int>(::floor((bd * 255.0) + 0.5));
-}
-
-
-HSVColor::HSVColor(RGBColor const & rgb)
-{
-	double const r = rgb.r / 255.0;
-	double const g = rgb.g / 255.0;
-	double const b = rgb.b / 255.0;
-
-	double const maxval = max(max(r, g), b);
-	double const minval = min(min(r, g), b);
-
-	v = maxval;
-
-	double const diff = maxval - minval;
-	if (maxval != 0.0)
-		s = diff / maxval;
-	else
-		s = 0.0;
-
-	h = nohue;
-	if (s != 0.0) {
-		double const rc = (maxval - r) / diff;
-		double const gc = (maxval - g) / diff;
-		double const bc = (maxval - b) / diff;
-
-		if (r == maxval)
-			h = bc - gc;
-		else if (g == maxval)
-			h = 2.0 + rc - bc;
-		else if (b == maxval)
-			h = 4.0 + gc - rc;
-
-		h *= 60.0;
-		if (h < 0)
-			h += 360;
-	}
-}
-
-} // namespace frontend
-} // namespace lyx
Index: src/frontends/controllers/ControlDocument.C
===================================================================
--- src/frontends/controllers/ControlDocument.C	(Revision 13453)
+++ src/frontends/controllers/ControlDocument.C	(Arbeitskopie)
@@ -114,11 +114,8 @@ void ControlDocument::dispatchParams()
 		for (; it != end; ++it) {
 			string const & current_branch = it->getBranch();
 			Branch const * branch = branchlist.find(current_branch);
-			string x11hexname = branch->getColor();
-			// check that we have a valid color!
-			if (x11hexname.empty() || x11hexname[0] != '#')
-				x11hexname = 
-					lcolor.getX11Name(LColor::background);
+			string const x11hexname =
+					lyx::X11hexname(branch->getColor());
 			// display the new color
 			string const str = current_branch  + ' ' + x11hexname;
 			kernel().dispatch(FuncRequest(LFUN_SET_COLOR, str));
Index: src/frontends/lyx_gui.h
===================================================================
--- src/frontends/lyx_gui.h	(Revision 13453)
+++ src/frontends/lyx_gui.h	(Arbeitskopie)
@@ -27,6 +27,9 @@ class LyXComm;
 class LyXDataSocket;
 class LyXServerSocket;
 class FuncRequest;
+namespace lyx {
+struct RGBColor;
+}
 
 /// GUI interaction
 namespace lyx_gui {
@@ -74,6 +77,13 @@ void exit();
  */
 FuncStatus getStatus(FuncRequest const & ev);
 
+/**
+ * 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, lyx::RGBColor & rgbcol);
+
 /** Eg, passing LColor::black returns "000000",
  *      passing LColor::white returns "ffffff".
  */
Index: src/Makefile.am
===================================================================
--- src/Makefile.am	(Revision 13453)
+++ src/Makefile.am	(Arbeitskopie)
@@ -85,6 +85,8 @@ lyx_SOURCES = \
 	BranchList.h \
 	Chktex.C \
 	Chktex.h \
+	Color.C \
+	Color.h \
 	CutAndPaste.C \
 	CutAndPaste.h \
 	DepTable.C \
Index: src/Color.C
===================================================================
--- src/Color.C	(Revision 13453)
+++ src/Color.C	(Arbeitskopie)
@@ -12,8 +12,6 @@
 
 #include "Color.h"
 
-#include "lyx_forms.h"
-
 #include "LColor.h"
 
 #include <cmath>
@@ -33,7 +31,6 @@ using std::ostringstream;
 using std::string;
 
 namespace lyx {
-namespace frontend {
 
 namespace {
 
@@ -51,28 +48,6 @@ int hexstrToInt(string const & str)
 
 
 
-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;
-}
-
-
 string const X11hexname(RGBColor const & col)
 {
 	ostringstream ostr;
@@ -199,5 +174,4 @@ HSVColor::HSVColor(RGBColor const & rgb)
 	}
 }
 
-} // namespace frontend
 } // namespace lyx

Reply via email to