PlottI
 All Classes Files Functions Variables Enumerations Enumerator Pages
Color.h
1 
2 // ~~ Colors ~~
3 
4 // ----------------------------------------------------------------------------
5 //
6 // This file contains various functionalities for easy use of colors in ROOT
7 // - Predefined Colors and Palettes
8 //
9 // ----------------------------------------------------------------------------
10 
11 #define COLOR_H
12 
13 // ----------------------------------------------------------------------------
14 // Functionality
15 // ----------------------------------------------------------------------------
16 
18 struct color {
19 
21  color(Float_t red, Float_t green, Float_t blue) : r(red), g(green), b(blue)
22  {
23  index = TColor::GetColor(r, g, b);
24  }
25 
27  color(Int_t hexvalue) : r(((hexvalue >> 16) & 0xFF) / 255.0), g(((hexvalue >> 8) & 0xFF) / 255.0), b(((hexvalue) & 0xFF) / 255.0)
28  {
29  index = TColor::GetColor(r, g, b);
30  }
31 
32  Float_t r;
33  Float_t g;
34  Float_t b;
35 
36  Color_t index;
37 
38 };
39 
42 {
43 
44 public:
45 
46  ColorGradient();
47  ColorGradient(Int_t nPoints, const vector<color> &rgbEndpoints, const vector<Double_t> &stops = {}, Float_t alpha = 1);
48  ~ColorGradient() {};
49 
50  vector<Int_t> GetPalette();
51  vector<Color_t> GetGradient();
52  Int_t GetNpoints();
53 
54 private:
55 
56  Int_t firstColorIndex {0};
57  Int_t nColorPoints {0};
58 
59 };
60 
62 ColorGradient::ColorGradient(Int_t nPoints, const vector<color> &rgbEndpoints, const vector<Double_t> &stops, Float_t alpha):
63  nColorPoints(nPoints)
64 {
65 
75  Int_t nColors = rgbEndpoints.size();
76 
77  vector<Double_t> red;
78  vector<Double_t> green;
79  vector<Double_t> blue;
80  vector<Double_t> length;
81 
82  if (stops.empty()){
83  for(Int_t col = 0; col < nColors; col++){
84  length.push_back((Float_t)col/(nColors-1));
85  }
86  std::cout << std::endl;
87  }
88  else if (stops.size() != nColors){
89  std::cout << "\033[1;31mERROR:\033[0m Number of given stops does not match number of given colors!!" << std::endl;
90  throw;
91  }
92  else length = std::move(stops);
93 
94  for (const color& rgb : rgbEndpoints) {
95 
96  red.push_back(rgb.r);
97  green.push_back(rgb.g);
98  blue.push_back(rgb.b);
99 
100  }
101 
102  firstColorIndex = TColor::CreateGradientColorTable(nColors, length.data(), red.data(), green.data(), blue.data(), nPoints, alpha);
103 
104 }
105 
107 
110  std::vector<Int_t> gradientColors(nColorPoints);
111  std::iota(gradientColors.begin(), gradientColors.end(), firstColorIndex);
112  return gradientColors;
113 
114 }
115 
116 vector<Color_t> ColorGradient::GetGradient(){
117 
120  std::vector<Color_t> gradientColors(nColorPoints);
121  std::iota(gradientColors.begin(), gradientColors.end(), firstColorIndex);
122  return gradientColors;
123 
124 }
125 
127 
130  return nColorPoints;
131 
132 }
133 
134 // ----------------------------------------------------------------------------
135 // Predefined Colors
136 // ----------------------------------------------------------------------------
137 
138 color blue {0.00, 0.00, 1.00};
139 color green {0.00, 1.00, 0.00};
140 color red {1.00, 0.00, 0.00};
141 color cyan {0.00, 1.00, 1.00};
142 color yellow {1.00, 1.00, 0.00};
143 color magenta {1.00, 0.00, 1.00};
144 color purple {0.40, 0.00, 0.60};
145 
146 color alice_red {0.851, 0.027, 0.094};
147 color alice_blue {0.012, 0.039, 0.549};
148 color alice_grey {0.169, 0.220, 0.251};
149 color alice_orange {0.949, 0.475, 0.059};
150 color alice_rosered {0.749, 0.255, 0.255};
151 
152 color deep_pink {0x990066};
153 color light_pink {0xC82F5C};
154 color grapefruit {0xE95F51};
155 color light_orange {0xFC9249};
156 color egg_yellow {0xFFC551};
157 color bright_yellow {0xF9F871};
158 
159 color blurple {0x330099};
160 color ocean_blue {0x004DD2};
161 color dark_sky_blue {0x0079EF};
162 color light_sky_blue {0x00A0F1};
163 color sky_cyan {0x00C5DF};
164 color bluish_green {0x00E7C4};
165 
166 color full {0x83B910}; //{0xDB7E00}; green
167 color towards {0x10A7C1}; //{0xAC001E}; cyan
168 color away {0x874BDA}; //{0x00007F}; purple
169 color transverse {0xF36F19}; //{0x669900}; orange
170 
171 // ----------------------------------------------------------------------------
172 // Predefined Palettes
173 // ----------------------------------------------------------------------
174 
175 vector<color> rgbRainbow = {blue, cyan, green, yellow, red, magenta};
176 ColorGradient rainbow = ColorGradient(20, rgbRainbow);
177 
178 vector<color> rgbAlice = {alice_grey, alice_blue, alice_red, alice_rosered, alice_orange};
179 ColorGradient alice_logo = ColorGradient(100, rgbAlice);
180 
181 vector<color> purple_and_yellow = {purple, yellow};
182 ColorGradient purple_to_yellow = ColorGradient(100, purple_and_yellow);
183 
184 vector<color> pink_to_yellow = {deep_pink, light_pink, grapefruit, light_orange, egg_yellow, bright_yellow};
185 ColorGradient citrus = ColorGradient(100, pink_to_yellow);
186 
187 vector<color> ocean_blues = {blurple, ocean_blue, dark_sky_blue, light_sky_blue, sky_cyan, bluish_green};
188 ColorGradient ocean = ColorGradient(100, ocean_blues, {0.0, 0.3, 0.6, 0.75, 0.9, 1.0});
189 
190 ColorGradient sunset = ColorGradient(100, {0x210156, 0x3900AA, 0x9C0091, 0xCB177A, 0xE3576E, 0xEE8972, 0xF2B68C});
191 
192 //
vector< Int_t > GetPalette()
Definition: Color.h:106
vector< Color_t > GetGradient()
Definition: Color.h:116
Color_t index
ROOT color index of color.
Definition: Color.h:36
Structure for saving RGB colors.
Definition: Color.h:18
color(Float_t red, Float_t green, Float_t blue)
Constructor using relative RGB values.
Definition: Color.h:21
Class for saving color gradients (palettes)
Definition: Color.h:41
color(Int_t hexvalue)
Constructor using hex integer value, see StackOverflow
Definition: Color.h:27
Int_t GetNpoints()
Definition: Color.h:126
Float_t g
green part of color
Definition: Color.h:33
Float_t r
red part of color
Definition: Color.h:32
Float_t b
blue part of color
Definition: Color.h:34