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