Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TEveRGBAPalette.cxx
Go to the documentation of this file.
1 // @(#)root/eve:$Id$
2 // Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TEveRGBAPalette.h"
13 
14 #include "TColor.h"
15 #include "TStyle.h"
16 #include "TMath.h"
17 
18 /** \class TEveRGBAPalette
19 \ingroup TEve
20 A generic, speed-optimised mapping from value to RGBA color
21 supporting different wrapping and range truncation modes.
22 
23 Flag fFixColorRange: specifies how the palette is mapped to signal values:
24  - true - LowLimit -> HighLimit
25  - false - MinValue -> MaxValue
26 */
27 
28 ClassImp(TEveRGBAPalette);
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Constructor.
32 
33 TEveRGBAPalette::TEveRGBAPalette() :
34  TObject(), TQObject(),
35  TEveRefCnt(),
36 
37  fUIf(1), fUIc(0),
38 
39  fLowLimit(0), fHighLimit(0), fMinVal(0), fMaxVal(0),
40 
41  fUIDoubleRep (kFALSE),
42  fInterpolate (kTRUE),
43  fShowDefValue (kTRUE),
44  fFixColorRange (kFALSE),
45  fUnderflowAction (kLA_Cut),
46  fOverflowAction (kLA_Clip),
47 
48  fDefaultColor(-1),
49  fUnderColor (-1),
50  fOverColor (-1),
51 
52  fNBins(0), fCAMin(0), fCAMax(0), fColorArray(0)
53 {
54  SetLimits(0, 1024);
55  SetMinMax(0, 512);
56 
57  SetDefaultColor(0);
58  SetUnderColor(1);
59  SetOverColor(2);
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// Constructor.
64 
65 TEveRGBAPalette::TEveRGBAPalette(Int_t min, Int_t max, Bool_t interp,
66  Bool_t showdef, Bool_t fixcolrng) :
67  TObject(), TQObject(),
68  TEveRefCnt(),
69 
70  fUIf(1), fUIc(0),
71 
72  fLowLimit(0), fHighLimit(0), fMinVal(0), fMaxVal(0),
73 
74  fUIDoubleRep (kFALSE),
75  fInterpolate (interp),
76  fShowDefValue (showdef),
77  fFixColorRange (fixcolrng),
78  fUnderflowAction (kLA_Cut),
79  fOverflowAction (kLA_Clip),
80 
81  fDefaultColor(-1),
82  fUnderColor (-1),
83  fOverColor (-1),
84 
85  fNBins(0), fCAMin(0), fCAMax(0), fColorArray(0)
86 {
87  SetLimits(min, max);
88  SetMinMax(min, max);
89 
90  SetDefaultColor(0);
91  SetUnderColor(1);
92  SetOverColor(2);
93 }
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// Destructor.
97 
98 TEveRGBAPalette::~TEveRGBAPalette()
99 {
100  delete [] fColorArray;
101 }
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// Set RGBA color 'pixel' for signal-value 'val'.
105 
106 void TEveRGBAPalette::SetupColor(Int_t val, UChar_t* pixel) const
107 {
108  using namespace TMath;
109  Float_t div = Max(1, fCAMax - fCAMin);
110  Int_t nCol = gStyle->GetNumberOfColors();
111 
112  Float_t f;
113  if (val >= fCAMax) f = nCol - 1;
114  else if (val <= fCAMin) f = 0;
115  else f = (val - fCAMin)/div*(nCol - 1);
116 
117  if (fInterpolate) {
118  Int_t bin = (Int_t) f;
119  Float_t f2 = f - bin, f1 = 1.0f - f2;
120  TEveUtil::ColorFromIdx(f1, gStyle->GetColorPalette(bin),
121  f2, gStyle->GetColorPalette(Min(bin + 1, nCol - 1)),
122  pixel);
123  } else {
124  TEveUtil::ColorFromIdx(gStyle->GetColorPalette((Int_t) Nint(f)), pixel);
125  }
126 }
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// Construct internal color array that maps signal value to RGBA color.
130 
131 void TEveRGBAPalette::SetupColorArray() const
132 {
133  if (fColorArray)
134  delete [] fColorArray;
135 
136  if (fFixColorRange) {
137  fCAMin = fLowLimit; fCAMax = fHighLimit;
138  } else {
139  fCAMin = fMinVal; fCAMax = fMaxVal;
140  }
141  fNBins = fCAMax - fCAMin + 1;
142 
143  fColorArray = new UChar_t [4 * fNBins];
144  UChar_t* p = fColorArray;
145  for(Int_t v = fCAMin; v <= fCAMax; ++v, p+=4)
146  SetupColor(v, p);
147 }
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 /// Clear internal color array.
151 
152 void TEveRGBAPalette::ClearColorArray()
153 {
154  if (fColorArray) {
155  delete [] fColorArray;
156  fColorArray = 0;
157  fNBins = fCAMin = fCAMax = 0;
158  }
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// Set low/high limits on signal value. Current min/max values are
163 /// clamped into the new limits.
164 
165 void TEveRGBAPalette::SetLimits(Int_t low, Int_t high)
166 {
167  fLowLimit = low;
168  fHighLimit = high;
169 
170  if (fMaxVal < fLowLimit) SetMax(fLowLimit);
171  if (fMinVal < fLowLimit) SetMin(fLowLimit);
172  if (fMinVal > fHighLimit) SetMin(fHighLimit);
173  if (fMaxVal > fHighLimit) SetMax(fHighLimit);
174 
175  ClearColorArray();
176 }
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 /// Set low/high limits and rescale current min/max values.
180 
181 void TEveRGBAPalette::SetLimitsScaleMinMax(Int_t low, Int_t high)
182 {
183  Float_t rng_old = fHighLimit - fLowLimit;
184  Float_t rng_new = high - low;
185 
186  fMinVal = TMath::Nint(low + (fMinVal - fLowLimit)*rng_new/rng_old);
187  fMaxVal = TMath::Nint(low + (fMaxVal - fLowLimit)*rng_new/rng_old);
188  fLowLimit = low;
189  fHighLimit = high;
190 
191  ClearColorArray();
192 }
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 /// Set current min value.
196 
197 void TEveRGBAPalette::SetMin(Int_t min)
198 {
199  fMinVal = TMath::Min(min, fMaxVal);
200  ClearColorArray();
201 }
202 
203 ////////////////////////////////////////////////////////////////////////////////
204 /// Set current max value.
205 
206 void TEveRGBAPalette::SetMax(Int_t max)
207 {
208  fMaxVal = TMath::Max(max, fMinVal);
209  ClearColorArray();
210 }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// Set current min/max values.
214 
215 void TEveRGBAPalette::SetMinMax(Int_t min, Int_t max)
216 {
217  fMinVal = min;
218  fMaxVal = max;
219  ClearColorArray();
220 }
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 /// Set flag determining whether GUI editor and overlays should show limits
224 /// and axis values as real values with mapping from integer value i to real
225 /// value d as: d = f*i + fc
226 
227 void TEveRGBAPalette::SetUIDoubleRep(Bool_t b, Double_t f, Double_t c)
228 {
229  fUIDoubleRep = b;
230  if (fUIDoubleRep) {
231  fUIf = f; fUIc = c;
232  } else {
233  fUIf = 1; fUIc = 0;
234  }
235 }
236 
237 ////////////////////////////////////////////////////////////////////////////////
238 /// Set interpolation flag. This determines how colors from ROOT's
239 /// palette are mapped into RGBA values for given signal.
240 
241 void TEveRGBAPalette::SetInterpolate(Bool_t b)
242 {
243  fInterpolate = b;
244  ClearColorArray();
245 }
246 
247 ////////////////////////////////////////////////////////////////////////////////
248 /// Set flag specifying how the palette is mapped to signal values:
249 /// true - LowLimit -> HighLimit
250 /// false - MinValue -> MaxValue
251 
252 void TEveRGBAPalette::SetFixColorRange(Bool_t v)
253 {
254  fFixColorRange = v;
255  ClearColorArray();
256 }
257 
258 ////////////////////////////////////////////////////////////////////////////////
259 /// Set default color.
260 
261 void TEveRGBAPalette::SetDefaultColor(Color_t ci)
262 {
263  fDefaultColor = ci;
264  TEveUtil::ColorFromIdx(ci, fDefaultRGBA, kTRUE);
265 }
266 
267 ////////////////////////////////////////////////////////////////////////////////
268 /// Set default color.
269 
270 void TEveRGBAPalette::SetDefaultColorPixel(Pixel_t pix)
271 {
272  SetDefaultColor(Color_t(TColor::GetColor(pix)));
273 }
274 
275 ////////////////////////////////////////////////////////////////////////////////
276 /// Set default color.
277 
278 void TEveRGBAPalette::SetDefaultColorRGBA(UChar_t r, UChar_t g, UChar_t b, UChar_t a)
279 {
280  fDefaultColor = Color_t(TColor::GetColor(r, g, b));
281  fDefaultRGBA[0] = r;
282  fDefaultRGBA[1] = g;
283  fDefaultRGBA[2] = b;
284  fDefaultRGBA[3] = a;
285 }
286 
287 ////////////////////////////////////////////////////////////////////////////////
288 /// Set underflow color.
289 
290 void TEveRGBAPalette::SetUnderColor(Color_t ci)
291 {
292  fUnderColor = ci;
293  TEveUtil::ColorFromIdx(ci, fUnderRGBA, kTRUE);
294 }
295 
296 ////////////////////////////////////////////////////////////////////////////////
297 /// Set underflow color.
298 
299 void TEveRGBAPalette::SetUnderColorPixel(Pixel_t pix)
300 {
301  SetUnderColor(Color_t(TColor::GetColor(pix)));
302 }
303 
304 ////////////////////////////////////////////////////////////////////////////////
305 /// Set underflow color.
306 
307 void TEveRGBAPalette::SetUnderColorRGBA(UChar_t r, UChar_t g, UChar_t b, UChar_t a)
308 {
309  fUnderColor = Color_t(TColor::GetColor(r, g, b));
310  fUnderRGBA[0] = r;
311  fUnderRGBA[1] = g;
312  fUnderRGBA[2] = b;
313  fUnderRGBA[3] = a;
314 }
315 
316 ////////////////////////////////////////////////////////////////////////////////
317 /// Set overflow color.
318 
319 void TEveRGBAPalette::SetOverColor(Color_t ci)
320 {
321  fOverColor = ci;
322  TEveUtil::ColorFromIdx(ci, fOverRGBA, kTRUE);
323 }
324 
325 ////////////////////////////////////////////////////////////////////////////////
326 /// Set overflow color.
327 
328 void TEveRGBAPalette::SetOverColorPixel(Pixel_t pix)
329 {
330  SetOverColor(Color_t(TColor::GetColor(pix)));
331 }
332 
333 ////////////////////////////////////////////////////////////////////////////////
334 /// Set overflow color.
335 
336 void TEveRGBAPalette::SetOverColorRGBA(UChar_t r, UChar_t g, UChar_t b, UChar_t a)
337 {
338  fOverColor = Color_t(TColor::GetColor(r, g, b));
339  fOverRGBA[0] = r;
340  fOverRGBA[1] = g;
341  fOverRGBA[2] = b;
342  fOverRGBA[3] = a;
343 }
344 
345 ////////////////////////////////////////////////////////////////////////////////
346 /// Emit the "MinMaxValChanged()" signal.
347 /// This is NOT called automatically from SetMin/Max functions but
348 /// it IS called from TEveRGBAPaletteEditor after it changes the
349 /// min/max values.
350 
351 void TEveRGBAPalette::MinMaxValChanged()
352 {
353  Emit("MinMaxValChanged()");
354 }