Another minor change, but this should almost get us to the point that we
[lyx.git] / src / Color.cpp
blob1b1f513862397c7f7d08138f1e25b7c7de028994
1 /**
2 * \file Color.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author John Levon
11 * \author André Pönitz
12 * \author Martin Vermeer
14 * Full author contact details are available in file CREDITS.
17 #include <config.h>
19 #include "Color.h"
20 #include "ColorSet.h"
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
26 #include "support/lassert.h"
28 #include <map>
29 #include <cmath>
30 #include <sstream>
31 #include <iomanip>
34 using namespace std;
35 using namespace lyx::support;
37 namespace lyx {
40 struct ColorSet::ColorEntry {
41 ColorCode lcolor;
42 char const * guiname;
43 char const * latexname;
44 char const * x11name;
45 char const * lyxname;
49 static int hexstrToInt(string const & str)
51 int val = 0;
52 istringstream is(str);
53 is >> setbase(16) >> val;
54 return val;
58 /////////////////////////////////////////////////////////////////////
60 // RGBColor
62 /////////////////////////////////////////////////////////////////////
65 string const X11hexname(RGBColor const & col)
67 ostringstream ostr;
69 ostr << '#' << setbase(16) << setfill('0')
70 << setw(2) << col.r
71 << setw(2) << col.g
72 << setw(2) << col.b;
74 return ostr.str();
78 RGBColor rgbFromHexName(string const & x11hexname)
80 RGBColor c;
81 LASSERT(x11hexname.size() == 7 && x11hexname[0] == '#', /**/);
82 c.r = hexstrToInt(x11hexname.substr(1, 2));
83 c.g = hexstrToInt(x11hexname.substr(3, 2));
84 c.b = hexstrToInt(x11hexname.substr(5, 2));
85 return c;
88 string const outputLaTeXColor(RGBColor const & color)
90 // this routine returns a LaTeX readable color string in the form
91 // "red, green, blue" where the colors are a number in the range 0-1
92 int red = color.r;
93 int green = color.g;
94 int blue = color.b;
95 // the color values are given in the range of 0-255, so to get
96 // an output of "0.5" for the value 127 we need to do the following
97 if (red != 0)
98 ++red;
99 if (green != 0)
100 ++green;
101 if (blue != 0)
102 ++blue;
103 string output;
104 output = convert<string>(float(red) / 256) + ", "
105 + convert<string>(float(green) / 256) + ", "
106 + convert<string>(float(blue) / 256);
107 return output;
111 Color::Color(ColorCode base_color) : baseColor(base_color),
112 mergeColor(Color_ignore)
116 bool Color::operator==(Color const & color) const
118 return baseColor == color.baseColor;
122 bool Color::operator!=(Color const & color) const
124 return baseColor != color.baseColor;
128 bool Color::operator<(Color const & color) const
130 return baseColor < color.baseColor;
134 bool Color::operator<=(Color const & color) const
136 return baseColor <= color.baseColor;
140 std::ostream & operator<<(std::ostream & os, Color color)
142 os << to_ascii(lcolor.getGUIName(color.baseColor));
143 if (color.mergeColor != Color_ignore)
144 os << "[merged with:"
145 << to_ascii(lcolor.getGUIName(color.mergeColor)) << "]";
146 return os;
150 ColorSet::ColorSet()
152 char const * grey40 = "#666666";
153 char const * grey60 = "#999999";
154 char const * grey80 = "#cccccc";
155 //char const * grey90 = "#e5e5e5";
156 // ColorCode, gui, latex, x11, lyx
157 static ColorEntry const items[] = {
158 { Color_none, N_("none"), "none", "black", "none" },
159 { Color_black, N_("black"), "black", "black", "black" },
160 { Color_white, N_("white"), "white", "white", "white" },
161 { Color_red, N_("red"), "red", "red", "red" },
162 { Color_green, N_("green"), "green", "green", "green" },
163 { Color_blue, N_("blue"), "blue", "blue", "blue" },
164 { Color_cyan, N_("cyan"), "cyan", "cyan", "cyan" },
165 { Color_magenta, N_("magenta"), "magenta", "magenta", "magenta" },
166 { Color_yellow, N_("yellow"), "yellow", "yellow", "yellow" },
167 { Color_cursor, N_("cursor"), "cursor", "black", "cursor" },
168 { Color_background, N_("background"), "background", "linen", "background" },
169 { Color_foreground, N_("text"), "foreground", "black", "foreground" },
170 { Color_selection, N_("selection"), "selection", "LightBlue", "selection" },
171 { Color_selectiontext, N_("selected text"),
172 "selectiontext", "black", "selectiontext" },
173 { Color_latex, N_("LaTeX text"), "latex", "DarkRed", "latex" },
174 { Color_inlinecompletion, N_("inline completion"),
175 "inlinecompletion", grey60, "inlinecompletion" },
176 { Color_nonunique_inlinecompletion, N_("non-unique inline completion"),
177 "nonuniqueinlinecompletion", grey80, "nonuniqueinlinecompletion" },
178 { Color_preview, N_("previewed snippet"), "preview", "black", "preview" },
179 { Color_notelabel, N_("note label"), "note", "yellow", "note" },
180 { Color_notebg, N_("note background"), "notebg", "yellow", "notebg" },
181 { Color_commentlabel, N_("comment label"), "comment", "magenta", "comment" },
182 { Color_commentbg, N_("comment background"), "commentbg", "linen", "commentbg" },
183 { Color_greyedoutlabel, N_("greyedout inset label"), "greyedout", "#ff0080", "greyedout" },
184 { Color_greyedoutbg, N_("greyedout inset background"), "greyedoutbg", "linen", "greyedoutbg" },
185 { Color_phantomtext, N_("phantom inset text"), "phantomtext", "#7f7f7f", "phantomtext" },
186 { Color_shadedbg, N_("shaded box"), "shaded", "#ff0000", "shaded" },
187 { Color_listingsbg, N_("listings background"), "listingsbg", "white", "listingsbg" },
188 { Color_branchlabel, N_("branch label"), "branchlabel", "#c88000", "branchlabel" },
189 { Color_footlabel, N_("footnote label"), "footlabel", "#00aaff", "footlabel" },
190 { Color_indexlabel, N_("index label"), "indexlabel", "green", "indexlabel" },
191 { Color_marginlabel, N_("margin note label"), "marginlabel", "#aa55ff", "marginlabel" },
192 { Color_urllabel, N_("URL label"), "urllabel", "blue", "urllabel" },
193 { Color_urltext, N_("URL text"), "urltext", "blue", "urltext" },
194 { Color_depthbar, N_("depth bar"), "depthbar", "IndianRed", "depthbar" },
195 { Color_language, N_("language"), "language", "Blue", "language" },
196 { Color_command, N_("command inset"), "command", "black", "command" },
197 { Color_commandbg, N_("command inset background"), "commandbg", "azure", "commandbg" },
198 { Color_commandframe, N_("command inset frame"), "commandframe", "black", "commandframe" },
199 { Color_special, N_("special character"), "special", "RoyalBlue", "special" },
200 { Color_math, N_("math"), "math", "DarkBlue", "math" },
201 { Color_mathbg, N_("math background"), "mathbg", "linen", "mathbg" },
202 { Color_graphicsbg, N_("graphics background"), "graphicsbg", "linen", "graphicsbg" },
203 { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "linen", "mathmacrobg" },
204 { Color_mathframe, N_("math frame"), "mathframe", "Magenta", "mathframe" },
205 { Color_mathcorners, N_("math corners"), "mathcorners", "linen", "mathcorners" },
206 { Color_mathline, N_("math line"), "mathline", "Blue", "mathline" },
207 { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "#ede2d8", "mathmacrobg" },
208 { Color_mathmacrohoverbg, N_("math macro hovered background"), "mathmacrohoverbg", "#cdc3b8", "mathmacrohoverbg" },
209 { Color_mathmacrolabel, N_("math macro label"), "mathmacrolabel", "#a19992", "mathmacrolabel" },
210 { Color_mathmacroframe, N_("math macro frame"), "mathmacroframe", "#ede2d8", "mathmacroframe" },
211 { Color_mathmacroblend, N_("math macro blended out"), "mathmacroblend", "black", "mathmacroblend" },
212 { Color_mathmacrooldarg, N_("math macro old parameter"), "mathmacrooldarg", grey80, "mathmacrooldarg" },
213 { Color_mathmacronewarg, N_("math macro new parameter"), "mathmacronewarg", "black", "mathmacronewarg" },
214 { Color_captionframe, N_("caption frame"), "captionframe", "DarkRed", "captionframe" },
215 { Color_collapsable, N_("collapsable inset text"), "collapsable", "DarkRed", "collapsable" },
216 { Color_collapsableframe, N_("collapsable inset frame"), "collapsableframe", "IndianRed", "collapsableframe" },
217 { Color_insetbg, N_("inset background"), "insetbg", grey80, "insetbg" },
218 { Color_insetframe, N_("inset frame"), "insetframe", "IndianRed", "insetframe" },
219 { Color_error, N_("LaTeX error"), "error", "Red", "error" },
220 { Color_eolmarker, N_("end-of-line marker"), "eolmarker", "Brown", "eolmarker" },
221 { Color_appendix, N_("appendix marker"), "appendix", "Brown", "appendix" },
222 { Color_changebar, N_("change bar"), "changebar", "Blue", "changebar" },
223 { Color_deletedtext, N_("deleted text"), "deletedtext", "#ff0000", "deletedtext" },
224 { Color_addedtext, N_("added text"), "addedtext", "#0000ff", "addedtext" },
225 { Color_changedtextauthor1, N_("changed text 1st author"), "changedtextauthor1", "#0000ff", "changedtextauthor1" },
226 { Color_changedtextauthor2, N_("changed text 2nd author"), "changedtextauthor2", "#ff00ff", "changedtextauthor2" },
227 { Color_changedtextauthor3, N_("changed text 3rd author"), "changedtextauthor3", "#ff0000", "changedtextauthor3" },
228 { Color_changedtextauthor4, N_("changed text 4th author"), "changedtextauthor4", "#aa00ff", "changedtextauthor4" },
229 { Color_changedtextauthor5, N_("changed text 5th author"), "changedtextauthor5", "#55aa00", "changedtextauthor5" },
230 { Color_deletedtextmodifier, N_("deleted text modifier"), "deletedtextmodifier", "white", "deletedtextmodifier" },
231 { Color_added_space, N_("added space markers"), "added_space", "Brown", "added_space" },
232 { Color_topline, N_("top/bottom line"), "topline", "Brown", "topline" },
233 { Color_tabularline, N_("table line"), "tabularline", "black", "tabularline" },
234 { Color_tabularonoffline, N_("table on/off line"), "tabularonoffline",
235 "LightSteelBlue", "tabularonoffline" },
236 { Color_bottomarea, N_("bottom area"), "bottomarea", grey40, "bottomarea" },
237 { Color_newpage, N_("new page"), "newpage", "Blue", "newpage" },
238 { Color_pagebreak, N_("page break / line break"), "pagebreak", "RoyalBlue", "pagebreak" },
239 { Color_buttonframe, N_("frame of button"), "buttonframe", "#dcd2c8", "buttonframe" },
240 { Color_buttonbg, N_("button background"), "buttonbg", "#dcd2c8", "buttonbg" },
241 { Color_buttonhoverbg, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "buttonhoverbg" },
242 { Color_paragraphmarker, N_("paragraph marker"), "paragraphmarker", grey80, "paragraphmarker"},
243 { Color_inherit, N_("inherit"), "inherit", "black", "inherit" },
244 { Color_ignore, N_("ignore"), "ignore", "black", "ignore" },
245 { Color_ignore, 0, 0, 0, 0 }
248 for (int i = 0; items[i].guiname; ++i)
249 fill(items[i]);
253 /// initialise a color entry
254 void ColorSet::fill(ColorEntry const & entry)
256 Information in;
257 in.lyxname = entry.lyxname;
258 in.latexname = entry.latexname;
259 in.x11name = entry.x11name;
260 in.guiname = entry.guiname;
261 infotab[entry.lcolor] = in;
262 lyxcolors[entry.lyxname] = entry.lcolor;
263 latexcolors[entry.latexname] = entry.lcolor;
267 docstring const ColorSet::getGUIName(ColorCode c) const
269 InfoTab::const_iterator it = infotab.find(c);
270 if (it != infotab.end())
271 return _(it->second.guiname);
272 return from_ascii("none");
276 string const ColorSet::getX11Name(ColorCode c) const
278 InfoTab::const_iterator it = infotab.find(c);
279 if (it != infotab.end())
280 return it->second.x11name;
282 lyxerr << "LyX internal error: Missing color"
283 " entry in Color.cpp for " << c << '\n'
284 << "Using black." << endl;
285 return "black";
289 string const ColorSet::getLaTeXName(ColorCode c) const
291 InfoTab::const_iterator it = infotab.find(c);
292 if (it != infotab.end())
293 return it->second.latexname;
294 return "black";
298 string const ColorSet::getLyXName(ColorCode c) const
300 InfoTab::const_iterator it = infotab.find(c);
301 if (it != infotab.end())
302 return it->second.lyxname;
303 return "black";
307 bool ColorSet::setColor(ColorCode col, string const & x11name)
309 InfoTab::iterator it = infotab.find(col);
310 if (it == infotab.end()) {
311 LYXERR0("Color " << col << " not found in database.");
312 return false;
315 // "inherit" is returned for colors not in the database
316 // (and anyway should not be redefined)
317 if (col == Color_none || col == Color_inherit || col == Color_ignore) {
318 LYXERR0("Color " << getLyXName(col) << " may not be redefined.");
319 return false;
322 it->second.x11name = x11name;
323 return true;
327 bool ColorSet::setColor(string const & lyxname, string const &x11name)
329 string const lcname = ascii_lowercase(lyxname);
330 if (lyxcolors.find(lcname) == lyxcolors.end()) {
331 LYXERR(Debug::GUI, "ColorSet::setColor: Unknown color \""
332 << lyxname << '"');
333 addColor(static_cast<ColorCode>(infotab.size()), lcname);
336 return setColor(lyxcolors[lcname], x11name);
340 void ColorSet::addColor(ColorCode c, string const & lyxname)
342 ColorEntry ce = { c, "", "", "", lyxname.c_str() };
343 fill(ce);
347 ColorCode ColorSet::getFromLyXName(string const & lyxname) const
349 string const lcname = ascii_lowercase(lyxname);
350 Transform::const_iterator it = lyxcolors.find(lcname);
351 if (it == lyxcolors.end()) {
352 LYXERR0("ColorSet::getFromLyXName: Unknown color \""
353 << lyxname << '"');
354 return Color_none;
357 return it->second;
361 ColorCode ColorSet::getFromLaTeXName(string const & latexname) const
363 Transform::const_iterator it = latexcolors.find(latexname);
364 if (it == latexcolors.end()) {
365 lyxerr << "ColorSet::getFromLaTeXName: Unknown color \""
366 << latexname << '"' << endl;
367 return Color_none;
370 return it->second;
374 // The evil global Color instance
375 ColorSet lcolor;
376 // An equally evil global system Color instance
377 ColorSet system_lcolor;
380 } // namespace lyx