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
11 * \author André Pönitz
12 * \author Martin Vermeer
14 * Full author contact details are available in file CREDITS.
22 #include "support/lstrings.h"
30 #ifndef CXX_GLOBAL_CSTD
38 using std::istringstream
;
39 using std::ostringstream
;
43 using lyx::support::compare_ascii_no_case
;
44 using lyx::support::ascii_lowercase
;
50 lyx::Color::color lcolor
;
52 char const * latexname
;
59 int hexstrToInt(string
const & str
)
62 istringstream
is(str
);
63 is
>> std::setbase(16) >> val
;
74 /////////////////////////////////////////////////////////////////////
78 /////////////////////////////////////////////////////////////////////
81 string
const X11hexname(RGBColor
const & col
)
85 ostr
<< '#' << std::setbase(16) << std::setfill('0')
94 RGBColor::RGBColor(string
const & x11hexname
)
97 BOOST_ASSERT(x11hexname
.size() == 7 && x11hexname
[0] == '#');
98 r
= hexstrToInt(x11hexname
.substr(1,2));
99 g
= hexstrToInt(x11hexname
.substr(3,2));
100 b
= hexstrToInt(x11hexname
.substr(5,2));
104 RGBColor::RGBColor(HSVColor
const & hsv
)
107 double const s
= hsv
.s
;
108 double const v
= hsv
.v
;
112 if (h
== nohue
|| s
== 0.0) {
115 if (h
== 360.0) h
= 0.0;
118 int const j
= max(0, static_cast<int>(::floor(h
)));
121 double const f
= h
- j
;
122 double const p
= v
* (1.0 - s
);
123 double const q
= v
* (1.0 - (s
* f
));
124 double const t
= v
* (1.0 - (s
* (1.0 - f
)));
161 break; // should never happen.
165 r
= static_cast<int>(::floor((rd
* 255.0) + 0.5));
166 g
= static_cast<int>(::floor((gd
* 255.0) + 0.5));
167 b
= static_cast<int>(::floor((bd
* 255.0) + 0.5));
171 /////////////////////////////////////////////////////////////////////
175 /////////////////////////////////////////////////////////////////////
177 HSVColor::HSVColor(RGBColor
const & rgb
)
179 double const r
= rgb
.r
/ 255.0;
180 double const g
= rgb
.g
/ 255.0;
181 double const b
= rgb
.b
/ 255.0;
183 double const maxval
= max(max(r
, g
), b
);
184 double const minval
= min(min(r
, g
), b
);
188 double const diff
= maxval
- minval
;
196 double const rc
= (maxval
- r
) / diff
;
197 double const gc
= (maxval
- g
) / diff
;
198 double const bc
= (maxval
- b
) / diff
;
202 else if (g
== maxval
)
204 else if (b
== maxval
)
215 /////////////////////////////////////////////////////////////////////
219 /////////////////////////////////////////////////////////////////////
226 /// the name as it appears in the GUI
228 /// the name used in LaTeX
236 /// initialise a color entry
237 void fill(ColorEntry
const & entry
)
240 in
.lyxname
= entry
.lyxname
;
241 in
.latexname
= entry
.latexname
;
242 in
.x11name
= entry
.x11name
;
243 in
.guiname
= entry
.guiname
;
244 infotab
[entry
.lcolor
] = in
;
245 lyxcolors
[entry
.lyxname
] = entry
.lcolor
;
246 latexcolors
[entry
.latexname
] = entry
.lcolor
;
250 typedef std::map
<Color::color
, information
> InfoTab
;
251 /// the table of color information
254 typedef std::map
<string
, Color::color
> Transform
;
255 /// the transform between LyX color name string and integer code.
257 /// the transform between LaTeX color name string and integer code.
258 Transform latexcolors
;
266 // Color::color, gui, latex, x11, lyx
267 static ColorEntry
const items
[] = {
268 { none
, N_("none"), "none", "black", "none" },
269 { black
, N_("black"), "black", "black", "black" },
270 { white
, N_("white"), "white", "white", "white" },
271 { red
, N_("red"), "red", "red", "red" },
272 { green
, N_("green"), "green", "green", "green" },
273 { blue
, N_("blue"), "blue", "blue", "blue" },
274 { cyan
, N_("cyan"), "cyan", "cyan", "cyan" },
275 { magenta
, N_("magenta"), "magenta", "magenta", "magenta" },
276 { yellow
, N_("yellow"), "yellow", "yellow", "yellow" },
277 { cursor
, N_("cursor"), "cursor", "black", "cursor" },
278 { background
, N_("background"), "background", "linen", "background" },
279 { foreground
, N_("text"), "foreground", "black", "foreground" },
280 { selection
, N_("selection"), "selection", "LightBlue", "selection" },
281 { latex
, N_("LaTeX text"), "latex", "DarkRed", "latex" },
282 { preview
, N_("previewed snippet"), "preview", "black", "preview" },
283 { note
, N_("note"), "note", "blue", "note" },
284 { notebg
, N_("note background"), "notebg", "yellow", "notebg" },
285 { comment
, N_("comment"), "comment", "magenta", "comment" },
286 { commentbg
, N_("comment background"), "commentbg", "linen", "commentbg" },
287 { greyedout
, N_("greyedout inset"), "greyedout", "red", "greyedout" },
288 { greyedoutbg
, N_("greyedout inset background"), "greyedoutbg", "linen", "greyedoutbg" },
289 { shadedbg
, N_("shaded box"), "shaded", "#ff0000", "shaded" },
290 { depthbar
, N_("depth bar"), "depthbar", "IndianRed", "depthbar" },
291 { language
, N_("language"), "language", "Blue", "language" },
292 { command
, N_("command inset"), "command", "black", "command" },
293 { commandbg
, N_("command inset background"), "commandbg", "azure", "commandbg" },
294 { commandframe
, N_("command inset frame"), "commandframe", "black", "commandframe" },
295 { special
, N_("special character"), "special", "RoyalBlue", "special" },
296 { math
, N_("math"), "math", "DarkBlue", "math" },
297 { mathbg
, N_("math background"), "mathbg", "linen", "mathbg" },
298 { graphicsbg
, N_("graphics background"), "graphicsbg", "linen", "graphicsbg" },
299 { mathmacrobg
, N_("Math macro background"), "mathmacrobg", "linen", "mathmacrobg" },
300 { mathframe
, N_("math frame"), "mathframe", "Magenta", "mathframe" },
301 { mathcorners
, N_("math corners"), "mathcorners", "linen", "mathcorners" },
302 { mathline
, N_("math line"), "mathline", "Blue", "mathline" },
303 { captionframe
, N_("caption frame"), "captionframe", "DarkRed", "captionframe" },
304 { collapsable
, N_("collapsable inset text"), "collapsable", "DarkRed", "collapsable" },
305 { collapsableframe
, N_("collapsable inset frame"), "collapsableframe", "IndianRed", "collapsableframe" },
306 { insetbg
, N_("inset background"), "insetbg", "grey80", "insetbg" },
307 { insetframe
, N_("inset frame"), "insetframe", "IndianRed", "insetframe" },
308 { error
, N_("LaTeX error"), "error", "Red", "error" },
309 { eolmarker
, N_("end-of-line marker"), "eolmarker", "Brown", "eolmarker" },
310 { appendix
, N_("appendix marker"), "appendix", "Brown", "appendix" },
311 { changebar
, N_("change bar"), "changebar", "Blue", "changebar" },
312 { deletedtext
, N_("Deleted text"), "deletedtext", "#ff0000", "deletedtext" },
313 { addedtext
, N_("Added text"), "addedtext", "#0000ff", "addedtext" },
314 { added_space
, N_("added space markers"), "added_space", "Brown", "added_space" },
315 { topline
, N_("top/bottom line"), "topline", "Brown", "topline" },
316 { tabularline
, N_("table line"), "tabularline", "black", "tabularline" },
317 { tabularonoffline
, N_("table on/off line"), "tabularonoffline",
318 "LightSteelBlue", "tabularonoffline" },
319 { bottomarea
, N_("bottom area"), "bottomarea", "grey40", "bottomarea" },
320 { pagebreak
, N_("page break"), "pagebreak", "RoyalBlue", "pagebreak" },
321 { buttonframe
, N_("frame of button"), "buttonframe", "#dcd2c8", "buttonframe" },
322 { buttonbg
, N_("button background"), "buttonbg", "#dcd2c8", "buttonbg" },
323 { buttonhoverbg
, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "buttonhoverbg" },
324 { inherit
, N_("inherit"), "inherit", "black", "inherit" },
325 { ignore
, N_("ignore"), "ignore", "black", "ignore" },
326 { ignore
, 0, 0, 0, 0 }
329 for (int i
= 0; items
[i
].guiname
; ++i
)
330 pimpl_
->fill(items
[i
]);
334 Color::Color(Color
const & c
)
335 : pimpl_(new Pimpl(*c
.pimpl_
))
343 Color
& Color::operator=(Color tmp
)
345 boost::swap(pimpl_
, tmp
.pimpl_
);
350 docstring
const Color::getGUIName(Color::color c
) const
352 Pimpl::InfoTab::const_iterator it
= pimpl_
->infotab
.find(c
);
353 if (it
!= pimpl_
->infotab
.end())
354 return _(it
->second
.guiname
);
355 return from_ascii("none");
359 string
const Color::getX11Name(Color::color c
) const
361 Pimpl::InfoTab::const_iterator it
= pimpl_
->infotab
.find(c
);
362 if (it
!= pimpl_
->infotab
.end())
363 return it
->second
.x11name
;
365 lyxerr
<< "LyX internal error: Missing color"
366 " entry in Color.cpp for " << c
<< '\n'
367 << "Using black." << endl
;
372 string
const Color::getLaTeXName(Color::color c
) const
374 Pimpl::InfoTab::const_iterator it
= pimpl_
->infotab
.find(c
);
375 if (it
!= pimpl_
->infotab
.end())
376 return it
->second
.latexname
;
381 string
const Color::getLyXName(Color::color c
) const
383 Pimpl::InfoTab::const_iterator it
= pimpl_
->infotab
.find(c
);
384 if (it
!= pimpl_
->infotab
.end())
385 return it
->second
.lyxname
;
390 bool Color::setColor(Color::color col
, string
const & x11name
)
392 Pimpl::InfoTab::iterator it
= pimpl_
->infotab
.find(col
);
393 if (it
== pimpl_
->infotab
.end()) {
394 lyxerr
<< "Color " << col
<< " not found in database."
399 // "inherit" is returned for colors not in the database
400 // (and anyway should not be redefined)
401 if (col
== none
|| col
== inherit
|| col
== ignore
) {
402 lyxerr
<< "Color " << getLyXName(col
)
403 << " may not be redefined" << endl
;
407 it
->second
.x11name
= x11name
;
412 bool Color::setColor(string
const & lyxname
, string
const &x11name
)
414 string
const lcname
= ascii_lowercase(lyxname
);
415 if (pimpl_
->lyxcolors
.find(lcname
) == pimpl_
->lyxcolors
.end()) {
417 << "Color::setColor: Unknown color \""
418 << lyxname
<< '"' << endl
;
419 addColor(static_cast<color
>(pimpl_
->infotab
.size()), lcname
);
422 return setColor(pimpl_
->lyxcolors
[lcname
], x11name
);
426 void Color::addColor(Color::color c
, string
const & lyxname
) const
428 ColorEntry ce
= { c
, "", "", "", lyxname
.c_str() };
433 Color::color
Color::getFromLyXName(string
const & lyxname
) const
435 string
const lcname
= ascii_lowercase(lyxname
);
436 if (pimpl_
->lyxcolors
.find(lcname
) == pimpl_
->lyxcolors
.end()) {
437 lyxerr
<< "Color::getFromLyXName: Unknown color \""
438 << lyxname
<< '"' << endl
;
442 return pimpl_
->lyxcolors
[lcname
];
446 Color::color
Color::getFromLaTeXName(string
const & latexname
) const
448 if (pimpl_
->latexcolors
.find(latexname
) == pimpl_
->latexcolors
.end()) {
449 lyxerr
<< "Color::getFromLaTeXName: Unknown color \""
450 << latexname
<< '"' << endl
;
454 return pimpl_
->latexcolors
[latexname
];
458 // The evil global Color instance
460 // An equally evil global system Color instance