Skip to content

Commit 34271b3

Browse files
committed
[core] Add "kWhite" and "kBlack" to the special TColorNumber map
There is a map in the TColorNumber constructor to get colors by special name. The "kWhite" and "kBlack" keys have to be added there, as these are the only color names that don't have have a corresponding entry in the global list of colors. Unfortunately, we can't add "kWhite" and "kBlack" to the global list of colors, because the indices corresponding to these enum values are alredy taken by "background" and "black". And using different indices would break the 1-to-1 correspondence between the color enum integer values and the global color names. That's why we just treat them as special colors in the TColorNumber constructor. You can verify that kBlack and kWhite are special with this code: ```c++ // all the EColor enum values #define COLOR_LIST \ X(kWhite) \ X(kBlack) \ X(kGray) \ X(kRed) \ X(kGreen) \ X(kBlue) \ X(kYellow) \ X(kMagenta) \ X(kCyan) \ X(kOrange) \ X(kSpring) \ X(kTeal) \ X(kAzure) \ X(kViolet) \ X(kPink) \ X(kGrape) \ X(kBrown) \ X(kAsh) \ X(kP6Blue) \ X(kP6Yellow) \ X(kP6Red) \ X(kP6Grape) \ X(kP6Gray) \ X(kP6Violet) \ X(kP8Blue) \ X(kP8Orange) \ X(kP8Red) \ X(kP8Pink) \ X(kP8Green) \ X(kP8Cyan) \ X(kP8Azure) \ X(kP8Gray) \ X(kP10Blue) \ X(kP10Yellow) \ X(kP10Red) \ X(kP10Gray) \ X(kP10Violet) \ X(kP10Brown) \ X(kP10Orange) \ X(kP10Green) \ X(kP10Ash) \ X(kP10Cyan) #define X(color) std::cout << TColor::GetColorByName(#color) << " " << color << std::endl; COLOR_LIST #undef X ``` The output will be: ```txt -1 0 -1 1 920 920 632 632 416 416 600 600 400 400 616 616 432 432 800 800 820 820 840 840 860 860 880 880 900 900 100 100 101 101 102 102 103 103 104 104 105 105 106 106 107 107 108 108 109 109 110 110 111 111 112 112 113 113 114 114 115 115 116 116 117 117 118 118 119 119 120 120 121 121 122 122 123 123 124 124 125 125 126 126 ```
1 parent f80ee47 commit 34271b3

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

bindings/pyroot/pythonizations/test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ ROOT_ADD_PYUNITTEST(pyroot_pyz_ttree_iterable ttree_iterable.py)
5555
ROOT_ADD_PYUNITTEST(pyroot_pyz_ttree_setbranchaddress ttree_setbranchaddress.py PYTHON_DEPS numpy)
5656
ROOT_ADD_PYUNITTEST(pyroot_pyz_ttree_branch ttree_branch.py PYTHON_DEPS numpy)
5757

58+
# TColor-related pythonizations
59+
ROOT_ADD_PYUNITTEST(pyroot_pyz_tcolor tcolor.py)
60+
5861
# TH1 and subclasses pythonizations
5962
ROOT_ADD_PYUNITTEST(pyroot_pyz_th1_operators th1_operators.py)
6063
ROOT_ADD_PYUNITTEST(pyroot_pyz_th1_fillN th1_fillN.py PYTHON_DEPS numpy)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import unittest
2+
3+
import ROOT
4+
5+
# All values of the EColor enum
6+
ecolor_values = [
7+
"kAsh",
8+
"kAzure",
9+
"kBlack",
10+
"kBlue",
11+
"kBrown",
12+
"kCyan",
13+
"kGrape",
14+
"kGray",
15+
"kGreen",
16+
"kMagenta",
17+
"kOrange",
18+
"kP10Ash",
19+
"kP10Blue",
20+
"kP10Brown",
21+
"kP10Cyan",
22+
"kP10Gray",
23+
"kP10Green",
24+
"kP10Orange",
25+
"kP10Red",
26+
"kP10Violet",
27+
"kP10Yellow",
28+
"kP6Blue",
29+
"kP6Grape",
30+
"kP6Gray",
31+
"kP6Red",
32+
"kP6Violet",
33+
"kP6Yellow",
34+
"kP8Azure",
35+
"kP8Blue",
36+
"kP8Cyan",
37+
"kP8Gray",
38+
"kP8Green",
39+
"kP8Orange",
40+
"kP8Pink",
41+
"kP8Red",
42+
"kPink",
43+
"kRed",
44+
"kSpring",
45+
"kTeal",
46+
"kViolet",
47+
"kWhite",
48+
"kYellow",
49+
]
50+
51+
52+
class TColorTests(unittest.TestCase):
53+
"""
54+
Tests related to TColor.
55+
"""
56+
57+
# Tests
58+
def test_implicit_tcolor_from_string(self):
59+
"""Test that we can set colors using Python strings."""
60+
61+
lgnd = ROOT.TLegend(0.53, 0.73, 0.87, 0.87)
62+
63+
for string_val in ecolor_values:
64+
enum_val = getattr(ROOT, string_val)
65+
lgnd.SetFillColor(string_val)
66+
# Check consistency
67+
assert enum_val == lgnd.GetFillColor()
68+
69+
70+
if __name__ == "__main__":
71+
unittest.main()

core/base/src/TColor.cxx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3696,9 +3696,15 @@ void TColor::InvertPalette()
36963696
TColorNumber::TColorNumber(std::string const &color)
36973697
{
36983698
using Map = std::unordered_map<std::string, Int_t>;
3699-
// Color dictionary to define matplotlib conventions
3700-
static Map colorMap{{"r", kRed}, {"b", kBlue}, {"g", kGreen}, {"y", kYellow},
3701-
{"w", kWhite}, {"k", kBlack}, {"m", kMagenta}, {"c", kCyan}};
3699+
// Color dictionary to define matplotlib conventions and other special colors.
3700+
// We can't use TColor::GetColorByName() with "kWhite" and "kBlack", as
3701+
// these are the only identifiers that are an enum values, but there is no
3702+
// corresponding color with the same name in the global list of colors.
3703+
// That's why they are looked up in this special map instead.
3704+
static Map colorMap{
3705+
{"r", kRed}, {"b", kBlue}, {"g", kGreen}, {"y", kYellow}, {"w", kWhite},
3706+
{"k", kBlack}, {"m", kMagenta}, {"c", kCyan}, {"kWhite", kWhite}, {"kBlack", kBlack},
3707+
};
37023708
auto found = colorMap.find(color);
37033709
if (found != colorMap.end()) {
37043710
fNumber = found->second;

0 commit comments

Comments
 (0)