ROOT  6.06/09
Reference Guide
TColorGradient.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Timur Pocheptsov 20/3/2012
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2012, 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 /** \class TColorGradient
13 TColorGradient extends basic TColor.
14 Actually, this is not a simple color, but linear gradient + shadow
15 for filled area. By inheriting from TColor, gradients can be placed
16 inside gROOT's list of colors and use it in all TAttXXX descendants
17 without modifying any existing code.
18 
19 Shadow, of course, is not a property of any color, and gradient is
20 not, but this is the best way to add new attributes to filled area
21 without re-writing all the graphics code.
22 */
23 
24 #include <cassert>
25 
26 #include "TColorGradient.h"
27 #include "TObjArray.h"
28 #include "TString.h"
29 #include "TError.h"
30 #include "TROOT.h"
31 
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 /// Constructor.
36 
38  : fCoordinateMode(kObjectBoundingMode)
39 {
40 }
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// There is no way to validate parameters here, so it's up to user
44 /// to pass correct arguments.
45 
47  const Color_t *indices, ECoordinateMode mode)
48  : fCoordinateMode(mode)
49 {
50  assert(nPoints != 0 && "TColorGradient, number of points is 0");
51  assert(points != 0 && "TColorGradient, points parameter is null");
52  assert(indices != 0 && "TColorGradient, indices parameter is null");
53 
54  ResetColor(nPoints, points, indices);
55  RegisterColor(colorIndex);
56 }
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// There is no way to validate parameters here, so it's up to user
60 /// to pass correct arguments.
61 
63  const Double_t *colors, ECoordinateMode mode)
64  : fCoordinateMode(mode)
65 {
66  assert(nPoints != 0 && "TColorGradient, number of points is 0");
67  assert(points != 0 && "TColorGradient, points parameter is null");
68  assert(colors != 0 && "TColorGradient, colors parameter is null");
69 
70  ResetColor(nPoints, points, colors);
71  RegisterColor(colorIndex);
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// Reset color.
76 
77 void TColorGradient::ResetColor(UInt_t nPoints, const Double_t *points, const Color_t *colorIndices)
78 {
79  assert(nPoints != 0 && "ResetColor, number of points is 0");
80  assert(points != 0 && "ResetColor, points parameter is null");
81  assert(colorIndices != 0 && "ResetColor, colorIndices parameter is null");
82 
83  fColorPositions.assign(points, points + nPoints);
84  fColors.resize(nPoints * 4);//4 == rgba.
85 
86  Float_t rgba[4];
87  for (UInt_t i = 0, pos = 0; i < nPoints; ++i, pos += 4) {
88  const TColor *clearColor = gROOT->GetColor(colorIndices[i]);
89  if (!clearColor || dynamic_cast<const TColorGradient *>(clearColor)) {
90  //TColorGradient can not be a step in TColorGradient.
91  Error("ResetColor", "Bad color for index %d, set to opaque black", colorIndices[i]);
92  fColors[pos] = 0.;
93  fColors[pos + 1] = 0.;
94  fColors[pos + 2] = 0.;
95  fColors[pos + 3] = 1.;//Alpha.
96  } else {
97  clearColor->GetRGB(rgba[0], rgba[1], rgba[2]);
98  rgba[3] = clearColor->GetAlpha();
99  fColors[pos] = rgba[0];
100  fColors[pos + 1] = rgba[1];
101  fColors[pos + 2] = rgba[2];
102  fColors[pos + 3] = rgba[3];
103  }
104  }
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Reset color.
109 
111  const Double_t *colors)
112 {
113  assert(nPoints != 0 && "ResetColor, number of points is 0");
114  assert(points != 0 && "ResetColor, points parameter is null");
115  assert(colors != 0 && "ResetColor, colors parameter is null");
116 
117  fColorPositions.assign(points, points + nPoints);
118  fColors.assign(colors, colors + nPoints * 4);
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Set coordinate mode.
123 
125 {
126  fCoordinateMode = mode;
127 }
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// Get coordinate mode.
131 
133 {
134  return fCoordinateMode;
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// Get number of steps.
139 
141 {
142  return fColorPositions.size();
143 }
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 /// Get color positions
147 
149 {
150  return &fColorPositions[0];
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Get colors.
155 
157 {
158  return &fColors[0];
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// Register color
163 
165 {
166  fNumber = colorIndex;
167  SetName(TString::Format("Color%d", colorIndex));
168 
169  if (gROOT) {
170  if (gROOT->GetColor(colorIndex)) {
171  Warning("RegisterColor", "Color with index %d is already defined", colorIndex);
172  return;
173  }
174 
175  if (TObjArray *colors = (TObjArray*)gROOT->GetListOfColors()) {
176  colors->AddAtAndExpand(this, colorIndex);
177  } else {
178  Error("RegisterColor", "List of colors is a null pointer in gROOT, color was not registered");
179  return;
180  }
181  }
182 }
183 
185 
186 /** \class TLinearGradient
187 Define a linear color gradient.
188 */
189 
191 {
192 }
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 /// Constructor.
196 
198  const Color_t *colorIndices, ECoordinateMode mode)
199  : TColorGradient(newColor, nPoints, points, colorIndices, mode)
200 {
201 }
202 
203 ////////////////////////////////////////////////////////////////////////////////
204 /// Constructor.
205 
207  const Double_t *colors, ECoordinateMode mode)
208  : TColorGradient(newColor, nPoints, points, colors, mode)
209 {
210 }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// Set end and start.
214 
216 {
217  fStart = p1;
218  fEnd = p2;
219 }
220 
221 ////////////////////////////////////////////////////////////////////////////////
222 /// Get start.
223 
225 {
226  return fStart;
227 }
228 
229 ////////////////////////////////////////////////////////////////////////////////
230 /// Get end.
231 
233 {
234  return fEnd;
235 }
236 
238 
239 /** \class TRadialGradient
240 Define a radial color gradient
241 */
242 
244 {
245 }
246 
247 ////////////////////////////////////////////////////////////////////////////////
248 /// Constructor.
249 
251  const Color_t *colorIndices, ECoordinateMode mode)
252  : TColorGradient(newColor, nPoints, points, colorIndices, mode)
253 {
254 }
255 
256 ////////////////////////////////////////////////////////////////////////////////
257 /// Constructor.
258 
260  const Double_t *colors, ECoordinateMode mode)
261  : TColorGradient(newColor, nPoints, points, colors, mode)
262 {
263 }
264 
265 ////////////////////////////////////////////////////////////////////////////////
266 /// Get gradient type.
267 
269 {
270  return fType;
271 }
272 
273 ////////////////////////////////////////////////////////////////////////////////
274 /// Set start and end R1 and R2.
275 
277 {
278  fStart = p1;
279  fR1 = r1;
280  fEnd = p2;
281  fR2 = r2;
282 
283  fType = kExtended;
284 }
285 
286 ////////////////////////////////////////////////////////////////////////////////
287 /// Get start.
288 
290 {
291  return fStart;
292 }
293 
294 ////////////////////////////////////////////////////////////////////////////////
295 // Get R1.
296 
298 {
299  return fR1;
300 }
301 
302 ////////////////////////////////////////////////////////////////////////////////
303 /// Get end.
304 
306 {
307  return fEnd;
308 }
309 
310 ////////////////////////////////////////////////////////////////////////////////
311 /// Get R2.
312 
314 {
315  return fR2;
316 }
317 
318 ////////////////////////////////////////////////////////////////////////////////
319 /// Set radial gradient.
320 
322 {
323  fStart = center;
324  fR1 = radius;
325 
326  fType = kSimple;
327 }
328 
329 ////////////////////////////////////////////////////////////////////////////////
330 /// Get center.
331 
333 {
334  return fStart;
335 }
336 
337 ////////////////////////////////////////////////////////////////////////////////
338 /// Get radius.
339 
341 {
342  return fR1;
343 }
An array of TObjects.
Definition: TObjArray.h:39
ECoordinateMode GetCoordinateMode() const
Get coordinate mode.
float Float_t
Definition: RtypesCore.h:53
#define assert(cond)
Definition: unittest.h:542
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
#define gROOT
Definition: TROOT.h:340
std::vector< Double_t > fColorPositions
void SetRadialGradient(const Point &center, Double_t radius)
Set radial gradient.
const Point & GetCenter() const
Get center.
Define a radial color gradient.
Double_t GetR2() const
Get R2.
void SetStartEndR1R2(const Point &p1, Double_t r1, const Point &p2, Double_t r2)
Set start and end R1 and R2.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2334
static double p2(double t, double a, double b, double c)
void SetCoordinateMode(ECoordinateMode mode)
Set coordinate mode.
const Point & GetEnd() const
Get end.
std::vector< Double_t > fColors
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
short Color_t
Definition: RtypesCore.h:79
void RegisterColor(Color_t colorIndex)
Register color.
std::vector< Color_t >::size_type SizeType_t
point * points
Definition: X3DBuffer.c:20
Double_t GetRadius() const
Get radius.
SizeType_t GetNumberOfSteps() const
Get number of steps.
unsigned int r1[N_CITIES]
Definition: simanTSP.cxx:321
virtual void GetRGB(Float_t &r, Float_t &g, Float_t &b) const
Definition: TColor.h:54
Double_t GetR1() const
unsigned int UInt_t
Definition: RtypesCore.h:42
const Point & GetEnd() const
Get end.
static double p1(double t, double a, double b)
ClassImp(TColorGradient) TColorGradient
Constructor.
Int_t fNumber
Definition: TColor.h:25
Define a linear color gradient.
EGradientType GetGradientType() const
Get gradient type.
Color * colors
Definition: X3DBuffer.c:19
const Double_t * GetColors() const
Get colors.
EGradientType fType
double Double_t
Definition: RtypesCore.h:55
The color creation and management class.
Definition: TColor.h:23
const Point & GetStart() const
Get start.
void ResetColor(UInt_t nPoints, const Double_t *points, const Color_t *colorIndices)
Reset color.
const Double_t * GetColorPositions() const
Get color positions.
Float_t GetAlpha() const
Definition: TColor.h:66
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
const Point & GetStart() const
Get start.
TColorGradient extends basic TColor.
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322
ECoordinateMode fCoordinateMode
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904