]> git.lyx.org Git - features.git/blob - src/Color.cpp
Introduce semantic label colors
[features.git] / src / Color.cpp
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.
5  *
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
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #include <config.h>
18
19 #include "Color.h"
20 #include "ColorSet.h"
21
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"
27
28 #include <map>
29 #include <cmath>
30 #include <sstream>
31 #include <iomanip>
32
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38
39
40 struct ColorSet::ColorEntry {
41         ColorCode lcolor;
42         char const * guiname;
43         char const * latexname;
44         char const * x11hexname;
45         char const * lyxname;
46 };
47
48
49 static int hexstrToInt(string const & str)
50 {
51         int val = 0;
52         istringstream is(str);
53         is >> setbase(16) >> val;
54         return val;
55 }
56
57
58 /////////////////////////////////////////////////////////////////////
59 //
60 // RGBColor
61 //
62 /////////////////////////////////////////////////////////////////////
63
64
65 string const X11hexname(RGBColor const & col)
66 {
67         ostringstream ostr;
68
69         ostr << '#' << setbase(16) << setfill('0')
70              << setw(2) << col.r
71              << setw(2) << col.g
72              << setw(2) << col.b;
73
74         return ostr.str();
75 }
76
77
78 RGBColor rgbFromHexName(string const & x11hexname)
79 {
80         RGBColor c;
81         LASSERT(x11hexname.size() == 7 && x11hexname[0] == '#',
82                 return c);
83         c.r = hexstrToInt(x11hexname.substr(1, 2));
84         c.g = hexstrToInt(x11hexname.substr(3, 2));
85         c.b = hexstrToInt(x11hexname.substr(5, 2));
86         return c;
87 }
88
89
90 string const outputLaTeXColor(RGBColor const & color)
91 {
92         // this routine returns a LaTeX readable color string in the form
93         // "red, green, blue" where the colors are a number in the range 0-1
94         int red = color.r;
95         int green = color.g;
96         int blue = color.b;
97 #ifdef USE_CORRECT_RGB_CONVERSION
98         int const scale = 255;
99 #else
100         // the color values are given in the range of 0-255, so to get
101         // an output of "0.5" for the value 127 we need to do the following
102         // FIXME: This is wrong, since it creates a nonlinear mapping:
103         //        There is a gap between 0/256 and 2/256!
104         //        0.5 cannot be represented in 8bit hex RGB, it would be 127.5.
105         if (red != 0)
106                 ++red;
107         if (green != 0)
108                 ++green;
109         if (blue != 0)
110                 ++blue;
111         int const scale = 256;
112 #endif
113         string output;
114         output = convert<string>(float(red) / scale) + ", "
115                          + convert<string>(float(green) / scale) + ", "
116                          + convert<string>(float(blue) / scale);
117         return output;
118 }
119
120
121 RGBColor const RGBColorFromLaTeX(string const & color)
122 {
123         vector<string> rgb = getVectorFromString(color);
124         while (rgb.size() < 3)
125                 rgb.push_back("0");
126         RGBColor c;
127         for (int i = 0; i < 3; ++i) {
128                 rgb[i] = trim(rgb[i]);
129                 if (!isStrDbl(rgb[i]))
130                         return c;
131         }
132 #ifdef USE_CORRECT_RGB_CONVERSION
133         int const scale = 255;
134 #else
135         // FIXME: This is wrong, since it creates a nonlinear mapping:
136         //        Both 0/256 and 1/256 are mapped to 0!
137         //        The wrong code exists only to match outputLaTeXColor().
138         int const scale = 256;
139 #endif
140         c.r = static_cast<unsigned int>(scale * convert<double>(rgb[0]) + 0.5);
141         c.g = static_cast<unsigned int>(scale * convert<double>(rgb[1]) + 0.5);
142         c.b = static_cast<unsigned int>(scale * convert<double>(rgb[2]) + 0.5);
143 #ifndef USE_CORRECT_RGB_CONVERSION
144         if (c.r != 0)
145                 c.r--;
146         if (c.g != 0)
147                 c.g--;
148         if (c.b != 0)
149                 c.b--;
150 #endif
151         return c;
152 }
153
154
155 Color::Color(ColorCode base_color) : baseColor(base_color),
156         mergeColor(Color_ignore)
157 {}
158
159
160 bool Color::operator==(Color const & color) const
161 {
162         return baseColor == color.baseColor;
163 }
164
165
166 bool Color::operator!=(Color const & color) const
167 {
168         return baseColor != color.baseColor;
169 }
170
171
172 bool Color::operator<(Color const & color) const
173 {
174         return baseColor < color.baseColor;
175 }
176
177
178 bool Color::operator<=(Color const & color) const
179 {
180         return baseColor <= color.baseColor;
181 }
182
183
184 std::ostream & operator<<(std::ostream & os, Color color)
185 {
186         os << to_ascii(lcolor.getGUIName(color.baseColor));
187         if (color.mergeColor != Color_ignore)
188                 os << "[merged with:"
189                         << to_ascii(lcolor.getGUIName(color.mergeColor)) << "]";
190         return os;
191 }
192
193
194 ColorSet::ColorSet()
195 {
196         char const * grey40 = "#666666";
197         char const * grey60 = "#999999";
198         char const * grey80 = "#cccccc";
199         // latex colors (xcolor package)
200         char const * black = "#000000";
201         char const * white = "#ffffff";
202         char const * blue = "#0000ff";
203         char const * brown = "#bf8040";
204         char const * cyan = "#00ffff";
205         char const * darkgray = "#404040";
206         char const * gray = "#808080";
207         char const * green = "#00ff00";
208         char const * lightgray = "#bfbfbf";
209         char const * lime = "#bfff00";
210         char const * magenta = "#ff00ff";
211         char const * olive = "#808000";
212         char const * orange = "#ff8000";
213         char const * pink = "#ffbfbf";
214         char const * purple = "#bf0040";
215         char const * red = "#ff0000";
216         char const * teal = "#008080";
217         char const * violet = "#800080";
218         char const * yellow = "#ffff00";
219         // svg colors
220         char const * Brown = "#a52a2a";
221         char const * DarkRed = "#8b0000";
222         char const * Green = "#008000";
223         char const * IndianRed = "#cd5c5c";
224         char const * Linen = "#faf0e6";
225         char const * RoyalBlue = "#4169e1";
226
227         //char const * grey90 = "#e5e5e5";
228         //  ColorCode, gui, latex, x11hexname, lyx
229         // Warning: several of these entries are overridden in GuiApplication constructor
230         static ColorEntry const items[] = {
231         { Color_none, N_("none"), "none", black, "none" },
232         { Color_black, N_("black"), "black", black, "black" },
233         { Color_white, N_("white"), "white", white, "white" },
234         { Color_blue, N_("blue"), "blue", blue, "blue" },
235         { Color_brown, N_("brown"), "brown", brown, "brown" },
236         { Color_cyan, N_("cyan"), "cyan", cyan, "cyan" },
237         { Color_darkgray, N_("darkgray"), "darkgray", darkgray, "darkgray" },
238         { Color_gray, N_("gray"), "gray", gray, "gray" },
239         { Color_green, N_("green"), "green", green, "green" },
240         { Color_lightgray, N_("lightgray"), "lightgray", lightgray, "lightgray" },
241         { Color_lime, N_("lime"), "lime", lime, "lime" },
242         { Color_magenta, N_("magenta"), "magenta", magenta, "magenta" },
243         { Color_olive, N_("olive"), "olive", olive, "olive" },
244         { Color_orange, N_("orange"), "orange", orange, "orange" },
245         { Color_pink, N_("pink"), "pink", pink, "pink" },
246         { Color_purple, N_("purple"), "purple", purple, "purple" },
247         { Color_red, N_("red"), "red", red, "red" },
248         { Color_teal, N_("teal"), "teal", teal, "teal" },
249         { Color_violet, N_("violet"), "violet", violet, "violet" },
250         { Color_yellow, N_("yellow"), "yellow", yellow, "yellow" },
251         { Color_cursor, N_("cursor"), "cursor", black, "cursor" },
252         { Color_background, N_("background"), "background", Linen, "background" },
253         { Color_foreground, N_("text"), "foreground", black, "foreground" },
254         { Color_selection, N_("selection"), "selection", "#add8e6", "selection" },
255         { Color_selectiontext, N_("selected text"), "selectiontext", black, "selectiontext" },
256         { Color_latex, N_("LaTeX text"), "latex", DarkRed, "latex" },
257         { Color_textlabel1, N_("Text label 1"), "textlabel1", blue, "textlabel1" },
258         { Color_textlabel2, N_("Text label 2"), "textlabel2", Green, "textlabel2" },
259         { Color_textlabel3, N_("Text label 3"), "textlabel3", magenta, "textlabel3" },
260         { Color_inlinecompletion, N_("inline completion"),
261                 "inlinecompletion", grey60, "inlinecompletion" },
262         { Color_nonunique_inlinecompletion, N_("non-unique inline completion"),
263                 "nonuniqueinlinecompletion", grey80, "nonuniqueinlinecompletion" },
264         { Color_preview, N_("previewed snippet"), "preview", black, "preview" },
265         { Color_notelabel, N_("note label"), "note", yellow, "note" },
266         { Color_notebg, N_("note background"), "notebg", yellow, "notebg" },
267         { Color_commentlabel, N_("comment label"), "comment", magenta, "comment" },
268         { Color_commentbg, N_("comment background"), "commentbg", Linen, "commentbg" },
269         { Color_greyedoutlabel, N_("greyedout inset label"), "greyedout", "#ff0080", "greyedout" },
270         { Color_greyedouttext, N_("greyedout inset text"), "greyedouttext", grey80, "greyedouttext" },
271         { Color_greyedoutbg, N_("greyedout inset background"), "greyedoutbg", Linen, "greyedoutbg" },
272         { Color_phantomtext, N_("phantom inset text"), "phantomtext", "#7f7f7f", "phantomtext" },
273         { Color_shadedbg, N_("shaded box"), "shaded", "#ff0000", "shaded" },
274         { Color_listingsbg, N_("listings background"), "listingsbg", white, "listingsbg" },
275         { Color_branchlabel, N_("branch label"), "branchlabel", "#c88000", "branchlabel" },
276         { Color_footlabel, N_("footnote label"), "footlabel", "#00aaff", "footlabel" },
277         { Color_indexlabel, N_("index label"), "indexlabel", Green, "indexlabel" },
278         { Color_marginlabel, N_("margin note label"), "marginlabel", "#aa55ff", "marginlabel" },
279         { Color_urllabel, N_("URL label"), "urllabel", blue, "urllabel" },
280         { Color_urltext, N_("URL text"), "urltext", blue, "urltext" },
281         { Color_depthbar, N_("depth bar"), "depthbar", IndianRed, "depthbar" },
282         { Color_scroll, N_("scroll indicator"), "scroll", IndianRed, "scroll" },
283         { Color_language, N_("language"), "language", blue, "language" },
284         { Color_command, N_("command inset"), "command", black, "command" },
285         { Color_commandbg, N_("command inset background"), "commandbg", "#f0ffff", "commandbg" },
286         { Color_commandframe, N_("command inset frame"), "commandframe", black, "commandframe" },
287         { Color_command_broken, N_("command inset (broken reference)"), "command", white, "command_broken" },
288         { Color_buttonbg_broken, N_("button background (broken reference)"), "commandbg", red, "commandbg_broken" },
289         { Color_buttonframe_broken, N_("button frame (broken reference)"), "commandframe", red, "commandframe_broken" },
290         { Color_buttonhoverbg_broken, N_("button background (broken reference) under focus"), "buttonhoverbg", "#DB0B0B", "buttonhoverbg_broken" },
291         { Color_special, N_("special character"), "special", RoyalBlue, "special" },
292         { Color_math, N_("math"), "math", "#00008B", "math" },
293         { Color_mathbg, N_("math background"), "mathbg", Linen, "mathbg" },
294         { Color_graphicsbg, N_("graphics background"), "graphicsbg", Linen, "graphicsbg" },
295         { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", Linen, "mathmacrobg" },
296         { Color_mathframe, N_("math frame"), "mathframe", magenta, "mathframe" },
297         { Color_mathcorners, N_("math corners"), "mathcorners", Linen, "mathcorners" },
298         { Color_mathline, N_("math line"), "mathline", blue, "mathline" },
299         { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "#ede2d8", "mathmacrobg" },
300         { Color_mathmacrohoverbg, N_("math macro hovered background"), "mathmacrohoverbg", "#cdc3b8", "mathmacrohoverbg" },
301         { Color_mathmacrolabel, N_("math macro label"), "mathmacrolabel", "#a19992", "mathmacrolabel" },
302         { Color_mathmacroframe, N_("math macro frame"), "mathmacroframe", "#ede2d8", "mathmacroframe" },
303         { Color_mathmacroblend, N_("math macro blended out"), "mathmacroblend", black, "mathmacroblend" },
304         { Color_mathmacrooldarg, N_("math macro old parameter"), "mathmacrooldarg", grey80, "mathmacrooldarg" },
305         { Color_mathmacronewarg, N_("math macro new parameter"), "mathmacronewarg", black, "mathmacronewarg" },
306         { Color_collapsible, N_("collapsible inset text"), "collapsible", DarkRed, "collapsible" },
307         { Color_collapsibleframe, N_("collapsible inset frame"), "collapsibleframe", IndianRed, "collapsibleframe" },
308         { Color_insetbg, N_("inset background"), "insetbg", grey80, "insetbg" },
309         { Color_insetframe, N_("inset frame"), "insetframe", IndianRed, "insetframe" },
310         { Color_error, N_("LaTeX error"), "error", red, "error" },
311         { Color_eolmarker, N_("end-of-line marker"), "eolmarker", Brown, "eolmarker" },
312         { Color_appendix, N_("appendix marker"), "appendix", Brown, "appendix" },
313         { Color_changebar, N_("change bar"), "changebar", blue, "changebar" },
314         { Color_deletedtext, N_("deleted text (output)"), "deletedtext", "#ff0000", "deletedtext" },
315         { Color_addedtext, N_("added text (output)"), "addedtext", "#0000ff", "addedtext" },
316         { Color_addedtextauthor1, N_("added text (workarea, 1st author)"), "changedtextauthor1", "#0000ff", "changedtextauthor1" },
317         { Color_addedtextauthor2, N_("added text (workarea, 2nd author)"), "changedtextauthor2", "#ff00ff", "changedtextauthor2" },
318         { Color_addedtextauthor3, N_("added text (workarea, 3rd author)"), "changedtextauthor3", "#ff0000", "changedtextauthor3" },
319         { Color_addedtextauthor4, N_("added text (workarea, 4th author)"), "changedtextauthor4", "#aa00ff", "changedtextauthor4" },
320         { Color_addedtextauthor5, N_("added text (workarea, 5th author)"), "changedtextauthor5", "#55aa00", "changedtextauthor5" },
321         { Color_deletedtextmodifier, N_("deleted text modifier (workarea)"), "deletedtextmodifier", white, "deletedtextmodifier" },
322         { Color_added_space, N_("added space markers"), "added_space", Brown, "added_space" },
323         { Color_tabularline, N_("table line"), "tabularline", black, "tabularline" },
324         { Color_tabularonoffline, N_("table on/off line"), "tabularonoffline", "#b0c4de", "tabularonoffline" },
325         { Color_bottomarea, N_("bottom area"), "bottomarea", grey40, "bottomarea" },
326         { Color_newpage, N_("new page"), "newpage", blue, "newpage" },
327         { Color_pagebreak, N_("page break / line break"), "pagebreak", RoyalBlue, "pagebreak" },
328         { Color_buttonframe, N_("button frame"), "buttonframe", "#dcd2c8", "buttonframe" },
329         { Color_buttonbg, N_("button background"), "buttonbg", "#dcd2c8", "buttonbg" },
330         { Color_buttonhoverbg, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "buttonhoverbg" },
331         { Color_paragraphmarker, N_("paragraph marker"), "paragraphmarker", grey80, "paragraphmarker"},
332         { Color_previewframe, N_("preview frame"), "previewframe", black, "previewframe"},
333         { Color_inherit, N_("inherit"), "inherit", black, "inherit" },
334         { Color_regexpframe, N_("regexp frame"), "regexpframe", Green, "regexpframe" },
335         { Color_ignore, N_("ignore"), "ignore", black, "ignore" },
336         { Color_ignore, nullptr, nullptr, nullptr, nullptr }
337         };
338
339         for (int i = 0; items[i].guiname; ++i)
340                 fill(items[i]);
341 }
342
343
344 /// initialise a color entry
345 void ColorSet::fill(ColorEntry const & entry)
346 {
347         Information in;
348         in.lyxname   = entry.lyxname;
349         in.latexname = entry.latexname;
350         in.x11hexname   = entry.x11hexname;
351         in.guiname   = entry.guiname;
352         infotab[entry.lcolor] = in;
353         lyxcolors[entry.lyxname] = entry.lcolor;
354         latexcolors[entry.latexname] = entry.lcolor;
355 }
356
357
358 docstring const ColorSet::getGUIName(ColorCode c) const
359 {
360         InfoTab::const_iterator it = infotab.find(c);
361         if (it != infotab.end())
362                 return _(it->second.guiname);
363         return from_ascii("none");
364 }
365
366
367 string const ColorSet::getX11HexName(ColorCode c) const
368 {
369         InfoTab::const_iterator it = infotab.find(c);
370         if (it != infotab.end())
371                 return it->second.x11hexname;
372
373         lyxerr << "LyX internal error: Missing color"
374                   " entry in Color.cpp for " << c << '\n'
375                << "Using black." << endl;
376         return "black";
377 }
378
379
380 string const ColorSet::getLaTeXName(ColorCode c) const
381 {
382         InfoTab::const_iterator it = infotab.find(c);
383         if (it != infotab.end())
384                 return it->second.latexname;
385         return "black";
386 }
387
388
389 string const ColorSet::getLyXName(ColorCode c) const
390 {
391         InfoTab::const_iterator it = infotab.find(c);
392         if (it != infotab.end())
393                 return it->second.lyxname;
394         return "black";
395 }
396
397
398 bool ColorSet::setColor(ColorCode col, string const & x11hexname)
399 {
400         InfoTab::iterator it = infotab.find(col);
401         if (it == infotab.end()) {
402                 LYXERR0("Color " << col << " not found in database.");
403                 return false;
404         }
405
406         // "inherit" is returned for colors not in the database
407         // (and anyway should not be redefined)
408         if (col == Color_none || col == Color_inherit || col == Color_ignore) {
409                 LYXERR0("Color " << getLyXName(col) << " may not be redefined.");
410                 return false;
411         }
412
413         it->second.x11hexname = x11hexname;
414         return true;
415 }
416
417
418 bool ColorSet::setColor(string const & lyxname, string const & x11hexname)
419 {
420         string const lcname = ascii_lowercase(lyxname);
421         if (lyxcolors.find(lcname) == lyxcolors.end()) {
422                 LYXERR(Debug::GUI, "ColorSet::setColor: Unknown color \""
423                        << lyxname << '"');
424                 addColor(static_cast<ColorCode>(infotab.size()), lcname);
425         }
426
427         return setColor(lyxcolors[lcname], x11hexname);
428 }
429
430
431 void ColorSet::addColor(ColorCode c, string const & lyxname)
432 {
433         ColorEntry ce = { c, "", "", "", lyxname.c_str() };
434         fill(ce);
435 }
436
437
438 ColorCode ColorSet::getFromLyXName(string const & lyxname) const
439 {
440         string const lcname = ascii_lowercase(lyxname);
441         Transform::const_iterator it = lyxcolors.find(lcname);
442         if (it == lyxcolors.end()) {
443                 LYXERR0("ColorSet::getFromLyXName: Unknown color \""
444                        << lyxname << '"');
445                 return Color_none;
446         }
447
448         return it->second;
449 }
450
451
452 ColorCode ColorSet::getFromLaTeXName(string const & latexname) const
453 {
454         Transform::const_iterator it = latexcolors.find(latexname);
455         if (it == latexcolors.end()) {
456                 lyxerr << "ColorSet::getFromLaTeXName: Unknown color \""
457                        << latexname << '"' << endl;
458                 return Color_none;
459         }
460
461         return it->second;
462 }
463
464
465 // The evil global Color instance
466 ColorSet lcolor;
467 // An equally evil global system Color instance
468 ColorSet system_lcolor;
469
470
471 } // namespace lyx