RootPalette.cxx
Go to the documentation of this file.
1 // RootPalette.cxx
2 
3 #include "RootPalette.h"
4 #include "TStyle.h"
5 
6 using std::vector;
7 
8 //**********************************************************************
9 
10 namespace {
11 
12 std::vector<const RootPalette*>& fixedPals() {
13  static std::vector<const RootPalette*> pals(3000, nullptr);
14  return pals;
15 }
16 
17 } // end unnamed namespace
18 
19 //**********************************************************************
20 // Static methods.
21 //**********************************************************************
22 
24  static bool first = true;
25  if ( first ) {
26  first = false;
27  fixedPals()[0] = new RootPalette;
28  }
29  return fixedPals()[0];
30 }
31 
32 //**********************************************************************
33 
34 const RootPalette* RootPalette::find(unsigned int ipal) {
35  if ( ipal >= fixedPals().size() ) return nullptr;
36  if ( fixedPals()[ipal] != nullptr ) return fixedPals()[ipal];
38  if ( ipal == 0 ) {
39  return defaultPalette();
40  } else if ( ipal < 1000 ) {
41  if ( fixedPals()[ipal] == nullptr ) {
42  fixedPals()[ipal] = new RootPalette(ipal);
43  }
44  } else if ( (ipal >= 1010 && ipal < 1020) ||
45  (ipal >= 2010 && ipal < 2020) ) {
46  TStyle* pstyleSave = gStyle;
47  gStyle = dynamic_cast<TStyle*>(gStyle->Clone("TmpStyle"));
48  TStyle* pstyleTmp = gStyle;
49  const double alpha = 1.0;
50  const int nRGBs = 5;
51  const int ncol = 200;
52  Double_t stops[nRGBs] = { 0.00, 0.30, 0.44, 0.70, 1.00};
53  Double_t red[nRGBs] = { 1.00, 1.00, 1.00, 0.70, 0.00};
54  Double_t green[nRGBs] = { 1.00, 0.75, 0.55, 0.20, 0.00};
55  Double_t blue[nRGBs] = { 1.00, 0.00, 0.00, 0.10, 0.00};
56  if ( ipal > 2000 ) {
57  stops[1] = 0.20;
58  stops[2] = 0.40;
59  stops[3] = 0.70;
60  green[1] = 0.90;
61  blue[1] = 0.55;
62  green[2] = 0.60;
63  blue[2] = 0.05;
64  red[4] = 0.20;
65  }
66  TColor::CreateGradientColorTable(nRGBs, stops, red, green, blue, ncol, alpha);
67  RootPalette* ppal = new RootPalette;
68  vector<unsigned int> ncfxs = {0, 1, 2, 4, 6, 8, 10, 20, 30, 40};
69  int ipal0 = ipal < 2000 ? 1010 : 2010;
70  ppal->setFirstColors(ncfxs[ipal - ipal0]);
71  fixedPals()[ipal] = ppal;
72  gStyle = pstyleSave;
73  delete pstyleTmp;
74  } else if ( (ipal >= 1020 && ipal < 1030) ||
75  (ipal >= 2020 && ipal < 2030) ) {
76  TStyle* pstyleSave = gStyle;
77  gStyle = dynamic_cast<TStyle*>(gStyle->Clone("TmpStyle"));
78  TStyle* pstyleTmp = gStyle;
79  const double alpha = 1.0;
80  const int nRGBs = 7;
81  const int ncol = 400;
82  Double_t stops[nRGBs] = { 0.00, 0.50, 0.50, 0.65, 0.72, 0.85, 1.00};
83  Double_t red[nRGBs] = { 0.09, 0.90, 1.00, 1.00, 1.00, 0.70, 0.00};
84  Double_t green[nRGBs] = { 0.60, 0.95, 1.00, 0.75, 0.55, 0.20, 0.00};
85  Double_t blue[nRGBs] = { 0.48, 1.00, 1.00, 0.00, 0.00, 0.10, 0.00};
86  if ( ipal > 2000 ) {
87  stops[3] = 0.60;
88  stops[4] = 0.70;
89  stops[5] = 0.85;
90  green[0] = 0.30;
91  blue[0] = 0.50;
92  red[1] = 1.00;
93  green[1] = 1.00;
94  blue[1] = 1.00;
95  red[2] = 1.00;
96  green[2] = 1.00;
97  blue[2] = 1.00;
98  green[3] = 0.90;
99  blue[3] = 0.55;
100  green[4] = 0.60;
101  blue[4] = 0.05;
102  red[6] = 0.20;
103  }
104  TColor::CreateGradientColorTable(nRGBs, stops, red, green, blue, ncol, alpha);
105  RootPalette* ppal = new RootPalette;
106  vector<unsigned int> ncfxs = {0, 1, 2, 4, 6, 8, 10, 20, 30, 40};
107  int ipal0 = ipal < 2000 ? 1020 : 2020;
108  ppal->setCentralColors(ncfxs[ipal - ipal0]);
109  fixedPals()[ipal] = ppal;
110  gStyle = pstyleSave;
111  delete pstyleTmp;
112  }
113  return fixedPals()[ipal];
114 }
115 
116 //**********************************************************************
117 
118 bool RootPalette::set(int ipalin) {
119  unsigned int ipal = ipalin >= 0 ? ipalin : -ipalin;
120  defaultPalette();
121  if ( ipal >= fixedPals().size() ) return false;
122  // Use predfined map if it exists.
123  const RootPalette* ppal = RootPalette::find(ipal);
124  // Otherwise, treat this as a Root palette index.
125  if ( ppal == nullptr && ipal > 0 && ipal < 1000 ) {
126  ppal = new RootPalette(ipal);
127  fixedPals()[ipal] = ppal;
128  }
129  if ( ppal != nullptr && ppal->setRootPalette() == 0 ) {
130  if ( ipalin < 0 ) TColor::InvertPalette();
131  return true;
132  }
133  return false;
134 }
135 
136 //**********************************************************************
137 // Non-static methods.
138 //**********************************************************************
139 
141  defaultPalette();
142  getRootPalette();
143 }
144 
145 //**********************************************************************
146 
147 RootPalette::RootPalette(TStyle* pstyle) {
148  defaultPalette();
149  getRootPalette(pstyle);
150 }
151 
152 //**********************************************************************
153 
155  defaultPalette();
156  if ( ipal > 0 && ipal < 1000 ) m_rootPalette = ipal;
157 }
158 
159 //**********************************************************************
160 
162  return getRootPalette(gStyle);
163 }
164 
165 //**********************************************************************
166 
167 int RootPalette::getRootPalette(TStyle* pstyle) {
168  m_cols.clear();
169  if ( pstyle == nullptr ) return 2;
170  unsigned int nscol = pstyle->GetNumberOfColors();
171  if ( nscol == 0 ) return 1;
172  for ( unsigned int icol=0; icol<nscol; ++icol ) m_cols.push_back(pstyle->GetColorPalette(icol));
173  return 0;
174 }
175 
176 //**********************************************************************
177 
179  return setRootPalette(gStyle);
180 }
181 
182 //**********************************************************************
183 
184 int RootPalette::setRootPalette(TStyle* pstyle) const {
185  if ( pstyle == nullptr ) return 2;
186  int ipal = rootPaletteIndex();
187  if ( ipal >0 && ipal < 1000 ) {
188  pstyle->SetPalette(ipal);
189  } else if ( ncol() == 0 ) {
190  return 1;
191  } else {
192  vector<int> cols(ncol());
193  for ( unsigned int icol=0; icol<ncol(); ++icol ) cols[icol] = m_cols[icol];
194  pstyle->SetPalette(ncol(), &cols[0]);
195  }
196  return 0;
197 }
198 
199 //**********************************************************************
200 
201 void RootPalette::setFirstColors(unsigned int ncfx, unsigned int icol) {
202  for ( unsigned int icfx=0; icfx<ncfx; ++icfx ) mutableColorVector()[icfx] = icol;
203 }
204 
205 //**********************************************************************
206 
207 void RootPalette::setCentralColors(unsigned int ncfx, unsigned int icol) {
208  unsigned int icz = ncol()/2;
209  unsigned int icfx1 = icz > ncfx ? icz - ncfx : 0;
210  unsigned int icfx2 = icz + ncfx;
211  if ( 2*icz + ncol() ) icfx2 -= 1; // Handle odd # colors.
212  if ( icfx2 > ncol() ) icfx2 = ncol();
213  for ( unsigned int icfx=icfx1; icfx<icz+ncfx; ++icfx ) mutableColorVector()[icfx] = icol;
214 }
215 
216 //**********************************************************************
217 
static bool set(int ipal)
std::vector< int > & mutableColorVector()
Definition: RootPalette.h:92
static const RootPalette * defaultPalette()
Definition: RootPalette.cxx:23
unsigned int ncol() const
Definition: RootPalette.h:77
struct vector vector
int m_rootPalette
Definition: RootPalette.h:103
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
int getRootPalette()
double alpha
Definition: doAna.cpp:15
void setCentralColors(unsigned int ncol, unsigned int icol=0)
std::vector< int > m_cols
Definition: RootPalette.h:106
int setRootPalette() const
void setFirstColors(unsigned int ncol, unsigned int icol=0)
static const RootPalette * find(unsigned int ipal)
Definition: RootPalette.cxx:34
int rootPaletteIndex() const
Definition: RootPalette.h:76