ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
palette.cpp
Go to the documentation of this file.
1 
11 #include "core/palette.hpp"
12 
13 #include <fstream>
14 #include <sstream>
15 
16 #include "TColor.h"
17 
18 #include "core/utilities.hpp"
19 
20 using namespace std;
21 
28 Palette::Palette(const string &file,
29  const string &palette):
30  file_(file),
31  palette_(palette){
32  }
33 
40 Palette::Palette(const string &palette):
41  Palette("txt/colors.txt", palette){
42 }
43 
48 const string & Palette::File() const{
49  return file_;
50 }
51 
58 Palette & Palette::File(const string &file){
59  file_ = file;
60  return *this;
61 }
62 
67 const string & Palette::PaletteName() const{
68  return palette_;
69 }
70 
77 Palette & Palette::PaletteName(const string &palette){
78  return PaletteName(file_, palette);
79 }
80 
89 Palette & Palette::PaletteName(const string &file,
90  const string &palette){
91  file_ = file;
92  palette_ = palette;
93  return *this;
94 }
95 
104 Int_t Palette::operator()(const string &color_name) const{
105  ifstream file(file_);
106  string line;
107  string current_palette_ = "";
108  int line_num = 0;
109  while(getline(file, line)){
110  ++line_num;
111  ReplaceAll(line, "=", " ");
112  ReplaceAll(line, "\t", " ");
113  auto start = line.find('[');
114  auto end = line.find(']');
115  if(start==string::npos && end!=string::npos){
116  ERROR("Could not find opening brace in line "+to_string(line_num));
117  }
118  if(start!=string::npos && end==string::npos){
119  ERROR("Could not find closing brace in line "+to_string(line_num));
120  }
121  if(start<end && start != string::npos && end != string::npos){
122  current_palette_ = line.substr(start+1, end-start-1);
123  }else if(current_palette_ == palette_
124  && line.size()
125  && line.at(0)!='#'){
126  istringstream iss(line);
127  string color;
128  iss >> color;
129  if(color != color_name) continue;
130  Int_t r, g, b;
131  iss >> r >> g >> b;
132  return RGB(r, g, b);
133  }
134  }
135  DBG("No color " << color_name << " in palette " << palette_ << " in file " << file_);
136  return 0;
137 }
138 
149 Int_t Palette::RGB(Int_t r, Int_t g, Int_t b){
150  return TColor::GetColor(r, g, b);
151 }
152 
163 Int_t Palette::RGB(Float_t r, Float_t g, Float_t b){
164  return TColor::GetColor(r, g, b);
165 }
166 
177 Int_t Palette::HSV(Float_t h, Float_t s, Float_t v){
178  Float_t r, g, b;
179  TColor::HSV2RGB(h, s, v, r, g, b);
180  return RGB(r, g, b);
181 }
182 
193 Int_t Palette::HLS(Int_t h, Int_t l, Int_t s){
194  Int_t r, g, b;
195  TColor::HLS2RGB(h, l, s, r, g, b);
196  return RGB(r, g, b);
197 }
198 
209 Int_t Palette::HLS(Float_t h, Float_t l, Float_t s){
210  Float_t r, g, b;
211  TColor::HLS2RGB(h, l, s, r, g, b);
212  return RGB(r, g, b);
213 }
#define DBG(x)
Definition: utilities.hpp:18
void ReplaceAll(std::string &str, const std::string &orig, const std::string &rep)
Definition: utilities.cpp:52
Int_t operator()(const std::string &color_name) const
Gets the ROOT color number corresponding to a color in the configuration file.
Definition: palette.cpp:104
STL namespace.
const std::string & PaletteName() const
Get the name of the palette from which colors are read.
Definition: palette.cpp:67
std::string palette_
Palette name from which to read color definitions.
Definition: palette.hpp:39
#define ERROR(x)
Definition: utilities.hpp:17
const std::string & File() const
Get the file from which colors are read.
Definition: palette.cpp:48
std::string file_
File from which to read color definitions.
Definition: palette.hpp:38
static Int_t RGB(Int_t r, Int_t g, Int_t b)
Gets the ROOT color number corresponding to a given RGB color.
Definition: palette.cpp:149
Palette(const std::string &file, const std::string &palette)
Construct from file and palette names.
Definition: palette.cpp:28
static Int_t HLS(Int_t h, Int_t l, Int_t s)
Gets the ROOT color number corresponding to a given HLS/HSL color.
Definition: palette.cpp:193
static Int_t HSV(Float_t h, Float_t s, Float_t v)
Gets the ROOT color number corresponding to a given HSV color.
Definition: palette.cpp:177
Loads colors from a text configuration file.
Definition: palette.hpp:8