aGrUM
3.3.2
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
dotColor.cpp
Go to the documentation of this file.
1
/****************************************************************************
2
* This file is part of the aGrUM/pyAgrum library. *
3
* *
4
* Copyright (c) 2005-2026 by *
5
* - Pierre-Henri WUILLEMIN(_at_LIP6) *
6
* - Christophe GONZALES(_at_AMU) *
7
* *
8
* The aGrUM/pyAgrum library is free software; you can redistribute it *
9
* and/or modify it under the terms of either : *
10
* *
11
* - the GNU Lesser General Public License as published by *
12
* the Free Software Foundation, either version 3 of the License, *
13
* or (at your option) any later version, *
14
* - the MIT license (MIT), *
15
* - or both in dual license, as here. *
16
* *
17
* (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18
* *
19
* This aGrUM/pyAgrum library is distributed in the hope that it will be *
20
* useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26
* OTHER DEALINGS IN THE SOFTWARE. *
27
* *
28
* See LICENCES for more details. *
29
* *
30
* SPDX-FileCopyrightText: Copyright 2005-2026 *
31
* - Pierre-Henri WUILLEMIN(_at_LIP6) *
32
* - Christophe GONZALES(_at_AMU) *
33
* SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34
* *
35
* Contact : info_at_agrum_dot_org *
36
* homepage : http://agrum.gitlab.io *
37
* gitlab : https://gitlab.com/agrumery/agrum *
38
* *
39
****************************************************************************/
40
47
#include <algorithm>
48
#include <array>
49
#include <cstdint>
50
51
#include <
agrum/base/core/dot/dotColor.h
>
52
53
#include <
agrum/base/core/utils_string.h
>
54
55
namespace
gum::dot
{
56
57
namespace
{
58
// the HTML colors, in the alphabetical order of their (lowercase) names
59
struct
NamedColor {
60
std::string_view name;
61
std::uint32_t rgb;
62
};
63
64
constexpr
std::array< NamedColor, 146 > _htmlColors_{{
65
{
"aliceblue"
, 0xF0F8FF},
66
{
"antiquewhite"
, 0xFAEBD7},
67
{
"aqua"
, 0x00FFFF},
68
{
"aquamarine"
, 0x7FFFD4},
69
{
"azure"
, 0xF0FFFF},
70
{
"beige"
, 0xF5F5DC},
71
{
"bisque"
, 0xFFE4C4},
72
{
"black"
, 0x000000},
73
{
"blanchedalmond"
, 0xFFEBCD},
74
{
"blue"
, 0x0000FF},
75
{
"blueviolet"
, 0x8A2BE2},
76
{
"brown"
, 0xA52A2A},
77
{
"burlywood"
, 0xDEB887},
78
{
"cadetblue"
, 0x5F9EA0},
79
{
"chartreuse"
, 0x7FFF00},
80
{
"chocolate"
, 0xD2691E},
81
{
"coral"
, 0xFF7F50},
82
{
"cornflowerblue"
, 0x6495ED},
83
{
"cornsilk"
, 0xFFF8DC},
84
{
"crimson"
, 0xDC143C},
85
{
"cyan"
, 0x00FFFF},
86
{
"darkblue"
, 0x00008B},
87
{
"darkcyan"
, 0x008B8B},
88
{
"darkgoldenrod"
, 0xB8860B},
89
{
"darkgray"
, 0xA9A9A9},
90
{
"darkgreen"
, 0x006400},
91
{
"darkgrey"
, 0xA9A9A9},
92
{
"darkkhaki"
, 0xBDB76B},
93
{
"darkmagenta"
, 0x8B008B},
94
{
"darkolivegreen"
, 0x556B2F},
95
{
"darkorange"
, 0xFF8C00},
96
{
"darkorchid"
, 0x9932CC},
97
{
"darkred"
, 0x8B0000},
98
{
"darksalmon"
, 0xE9967A},
99
{
"darkseagreen"
, 0x8FBC8F},
100
{
"darkslateblue"
, 0x483D8B},
101
{
"darkslategray"
, 0x2F4F4F},
102
{
"darkslategrey"
, 0x2F4F4F},
103
{
"darkturquoise"
, 0x00CED1},
104
{
"darkviolet"
, 0x9400D3},
105
{
"deeppink"
, 0xFF1493},
106
{
"deepskyblue"
, 0x00BFFF},
107
{
"dimgray"
, 0x696969},
108
{
"dimgrey"
, 0x696969},
109
{
"dodgerblue"
, 0x1E90FF},
110
{
"firebrick"
, 0xB22222},
111
{
"floralwhite"
, 0xFFFAF0},
112
{
"forestgreen"
, 0x228B22},
113
{
"fuchsia"
, 0xFF00FF},
114
{
"gainsboro"
, 0xDCDCDC},
115
{
"ghostwhite"
, 0xF8F8FF},
116
{
"gold"
, 0xFFD700},
117
{
"goldenrod"
, 0xDAA520},
118
{
"gray"
, 0x808080},
119
{
"green"
, 0x008000},
120
{
"greenyellow"
, 0xADFF2F},
121
{
"grey"
, 0x808080},
122
{
"honeydew"
, 0xF0FFF0},
123
{
"hotpink"
, 0xFF69B4},
124
{
"ivory"
, 0xFFFFF0},
125
{
"khaki"
, 0xF0E68C},
126
{
"lavender"
, 0xE6E6FA},
127
{
"lavenderblush"
, 0xFFF0F5},
128
{
"lawngreen"
, 0x7CFC00},
129
{
"lemonchiffon"
, 0xFFFACD},
130
{
"lightblue"
, 0xADD8E6},
131
{
"lightcoral"
, 0xF08080},
132
{
"lightcyan"
, 0xE0FFFF},
133
{
"lightgoldenrodyellow"
, 0xFAFAD2},
134
{
"lightgray"
, 0xD3D3D3},
135
{
"lightgreen"
, 0x90EE90},
136
{
"lightgrey"
, 0xD3D3D3},
137
{
"lightpink"
, 0xFFB6C1},
138
{
"lightsalmon"
, 0xFFA07A},
139
{
"lightseagreen"
, 0x20B2AA},
140
{
"lightskyblue"
, 0x87CEFA},
141
{
"lightslategray"
, 0x778899},
142
{
"lightslategrey"
, 0x778899},
143
{
"lightsteelblue"
, 0xB0C4DE},
144
{
"lightyellow"
, 0xFFFFE0},
145
{
"lime"
, 0x00FF00},
146
{
"limegreen"
, 0x32CD32},
147
{
"linen"
, 0xFAF0E6},
148
{
"magenta"
, 0xFF00FF},
149
{
"maroon"
, 0x800000},
150
{
"mediumaquamarine"
, 0x66CDAA},
151
{
"mediumblue"
, 0x0000CD},
152
{
"mediumorchid"
, 0xBA55D3},
153
{
"mediumpurple"
, 0x9370DB},
154
{
"mediumseagreen"
, 0x3CB371},
155
{
"mediumslateblue"
, 0x7B68EE},
156
{
"mediumspringgreen"
, 0x00FA9A},
157
{
"mediumturquoise"
, 0x48D1CC},
158
{
"mediumvioletred"
, 0xC71585},
159
{
"midnightblue"
, 0x191970},
160
{
"mintcream"
, 0xF5FFFA},
161
{
"mistyrose"
, 0xFFE4E1},
162
{
"moccasin"
, 0xFFE4B5},
163
{
"navajowhite"
, 0xFFDEAD},
164
{
"navy"
, 0x000080},
165
{
"oldlace"
, 0xFDF5E6},
166
{
"olive"
, 0x808000},
167
{
"olivedrab"
, 0x6B8E23},
168
{
"orange"
, 0xFFA500},
169
{
"orangered"
, 0xFF4500},
170
{
"orchid"
, 0xDA70D6},
171
{
"palegoldenrod"
, 0xEEE8AA},
172
{
"palegreen"
, 0x98FB98},
173
{
"paleturquoise"
, 0xAFEEEE},
174
{
"palevioletred"
, 0xDB7093},
175
{
"papayawhip"
, 0xFFEFD5},
176
{
"peachpuff"
, 0xFFDAB9},
177
{
"peru"
, 0xCD853F},
178
{
"pink"
, 0xFFC0CB},
179
{
"plum"
, 0xDDA0DD},
180
{
"powderblue"
, 0xB0E0E6},
181
{
"purple"
, 0x800080},
182
{
"rebeccapurple"
, 0x663399},
183
{
"red"
, 0xFF0000},
184
{
"rosybrown"
, 0xBC8F8F},
185
{
"royalblue"
, 0x4169E1},
186
{
"saddlebrown"
, 0x8B4513},
187
{
"salmon"
, 0xFA8072},
188
{
"sandybrown"
, 0xF4A460},
189
{
"seagreen"
, 0x2E8B57},
190
{
"seashell"
, 0xFFF5EE},
191
{
"sienna"
, 0xA0522D},
192
{
"silver"
, 0xC0C0C0},
193
{
"skyblue"
, 0x87CEEB},
194
{
"slateblue"
, 0x6A5ACD},
195
{
"slategray"
, 0x708090},
196
{
"slategrey"
, 0x708090},
197
{
"snow"
, 0xFFFAFA},
198
{
"springgreen"
, 0x00FF7F},
199
{
"steelblue"
, 0x4682B4},
200
{
"tan"
, 0xD2B48C},
201
{
"teal"
, 0x008080},
202
{
"thistle"
, 0xD8BFD8},
203
{
"tomato"
, 0xFF6347},
204
{
"turquoise"
, 0x40E0D0},
205
{
"violet"
, 0xEE82EE},
206
{
"wheat"
, 0xF5DEB3},
207
{
"white"
, 0xFFFFFF},
208
{
"whitesmoke"
, 0xF5F5F5},
209
{
"yellow"
, 0xFFFF00},
210
{
"yellowgreen"
, 0x9ACD32},
211
}};
212
213
// the value of a hexadecimal digit, or -1
214
int
_hexDigit_(
char
c) {
215
if
(c >=
'0'
&& c <=
'9'
)
return
c -
'0'
;
216
if
(c >=
'a'
&& c <=
'f'
)
return
c -
'a'
+ 10;
217
if
(c >=
'A'
&& c <=
'F'
)
return
c -
'A'
+ 10;
218
return
-1;
219
}
220
221
// the digits of `#RGB`, `#RGBA`, `#RRGGBB` or `#RRGGBBAA` (without the '#')
222
bool
_parseHexadecimal_(std::string_view digits,
Rgb
& rgb) {
223
const
auto
n = digits.size();
224
if
(n != 3 && n != 4 && n != 6 && n != 8)
return
false
;
225
226
const
auto
step = (n <= 4) ? std::size_t{1} : std::size_t{2};
227
int
comp[3];
228
for
(std::size_t i = 0; i < 3; ++i) {
229
const
int
high = _hexDigit_(digits[i * step]);
230
const
int
low = (step == 2) ? _hexDigit_(digits[i * step + 1]) : high;
231
if
(high < 0 || low < 0)
return
false
;
232
comp[i] = (step == 2) ? 16 * high + low : 17 * high;
233
}
234
// the transparency, ignored, must be well formed
235
for
(std::size_t i = 3 * step; i < n; ++i)
236
if
(_hexDigit_(digits[i]) < 0)
return
false
;
237
238
rgb = {
static_cast<
unsigned
char
>
(comp[0]),
239
static_cast<
unsigned
char
>
(comp[1]),
240
static_cast<
unsigned
char
>
(comp[2])};
241
return
true
;
242
}
243
}
// namespace
244
245
bool
parseColor
(std::string_view color,
Rgb
& rgb) {
246
if
(color.size() >= 2 && color.front() ==
'"'
&& color.back() ==
'"'
)
247
color = color.substr(1, color.size() - 2);
248
if
(color.empty())
return
false
;
249
250
if
(color.front() ==
'#'
)
return
_parseHexadecimal_(color.substr(1), rgb);
251
252
const
std::string name =
gum::toLower
(color);
253
const
auto
it
254
= std::lower_bound(_htmlColors_.begin(),
255
_htmlColors_.end(),
256
std::string_view(name),
257
[](
const
NamedColor& c, std::string_view n) { return c.name < n; });
258
if
(it == _htmlColors_.end() || it->name != name)
return
false
;
259
260
rgb = {
static_cast<
unsigned
char
>
(it->rgb >> 16),
261
static_cast<
unsigned
char
>
((it->rgb >> 8) & 0xFF),
262
static_cast<
unsigned
char
>
(it->rgb & 0xFF)};
263
return
true
;
264
}
265
266
namespace
{
267
// 1000 times the brightness, in integers: the threshold of contrastColor() is then exact,
268
// whatever the contraction of floating point operations done by the compiler
269
int
_millibrightness_(
const
Rgb& rgb) {
270
return
299 * rgb.
red
+ 587 * rgb.green + 114 * rgb.blue;
271
}
272
}
// namespace
273
274
double
brightness
(
const
Rgb
& rgb) {
return
_millibrightness_(rgb) / 1000.0; }
275
276
std::string
contrastColor
(
const
Rgb
& rgb) {
277
return
_millibrightness_(rgb) <= 153000 ?
"white"
:
"black"
;
278
}
279
280
std::string
resolveFgColor
(std::string_view fgColor,
281
std::string_view bgColor,
282
std::string_view fallback) {
283
if
(
gum::toLower
(fgColor) !=
ADAPTIVE_COLOR
)
return
std::string(fgColor);
284
285
Rgb
rgb;
286
if
(!
parseColor
(bgColor, rgb))
return
std::string(fallback);
287
return
contrastColor
(rgb);
288
}
289
290
}
// namespace gum::dot
dotColor.h
Colors of the graphs drawn with graphviz: reading of a color, brightness, readable text color.
gum::toLower
GUM_SHARED_PUBLIC std::string toLower(std::string_view str)
Returns the lowercase version of str.
Definition
utils_string_inl.h:60
gum::dot
Definition
dotColor.cpp:55
gum::dot::ADAPTIVE_COLOR
constexpr std::string_view ADAPTIVE_COLOR
the special value of a font color: black or white, according to the brightness of the background
Definition
dotColor.h:68
gum::dot::resolveFgColor
std::string resolveFgColor(std::string_view fgColor, std::string_view bgColor, std::string_view fallback)
The font color to use on a background, once the special value adaptive is resolved.
Definition
dotColor.cpp:280
gum::dot::brightness
double brightness(const Rgb &rgb)
Perceived brightness of a color, between 0 (black) and 255 (white): 0.299 R + 0.587 G + 0....
Definition
dotColor.cpp:274
gum::dot::parseColor
bool parseColor(std::string_view color, Rgb &rgb)
Reads a color as it is written in a dot attribute.
Definition
dotColor.cpp:245
gum::dot::contrastColor
std::string contrastColor(const Rgb &rgb)
The text color which is readable on a background: white if its brightness is at most 153,...
Definition
dotColor.cpp:276
gum::dot::Rgb
a color given by its three components, each one between 0 and 255
Definition
dotColor.h:60
gum::dot::Rgb::red
unsigned char red
Definition
dotColor.h:61
utils_string.h
Utilities for manipulating strings.
aGrUM
3.3.2
© PHW&CG&others - 2022
DoXyGeN 1.18.0