Logo ROOT  
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 
218 void TLinearGradient::SetStartEnd(const Point &p1, const Point &p2)
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 
279 void TRadialGradient::SetStartEndR1R2(const Point &p1, Double_t r1, const Point &p2, Double_t r2)
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 
324 void TRadialGradient::SetRadialGradient(const Point &center, Double_t radius)
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 }
TObjArray
An array of TObjects.
Definition: TObjArray.h:37
TNamed::SetName
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
TColorGradient::GetCoordinateMode
ECoordinateMode GetCoordinateMode() const
Get coordinate mode.
Definition: TColorGradient.cxx:135
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TRadialGradient::GetCenter
const Point & GetCenter() const
Get center.
Definition: TColorGradient.cxx:335
TColorGradient::TColorGradient
TColorGradient()
Constructor.
Definition: TColorGradient.cxx:40
TColorGradient::RegisterColor
void RegisterColor(Color_t colorIndex)
Register color.
Definition: TColorGradient.cxx:167
TColorGradient::fCoordinateMode
ECoordinateMode fCoordinateMode
Definition: TColorGradient.h:70
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
colors
Color * colors
Definition: X3DBuffer.c:21
TColorGradient::ResetColor
void ResetColor(UInt_t nPoints, const Double_t *points, const Color_t *colorIndices)
Reset color.
Definition: TColorGradient.cxx:80
TRadialGradient::fEnd
Point fEnd
Definition: TColorGradient.h:164
Float_t
float Float_t
Definition: RtypesCore.h:57
TColorGradient.h
TRadialGradient
Define a radial color gradient.
Definition: TColorGradient.h:128
TLinearGradient::GetStart
const Point & GetStart() const
Get start.
Definition: TColorGradient.cxx:227
TColorGradient::Point
Definition: TColorGradient.h:49
TString::Format
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:2311
TRadialGradient::GetR2
Double_t GetR2() const
Get R2.
Definition: TColorGradient.cxx:316
Color_t
short Color_t
Definition: RtypesCore.h:83
TString.h
TColor::GetAlpha
Float_t GetAlpha() const
Definition: TColor.h:63
TRadialGradient::SetStartEndR1R2
void SetStartEndR1R2(const Point &p1, Double_t r1, const Point &p2, Double_t r2)
Set start and end R1 and R2.
Definition: TColorGradient.cxx:279
TColorGradient
TColorGradient extends basic TColor.
Definition: TColorGradient.h:35
TROOT.h
TLinearGradient::TLinearGradient
TLinearGradient()
Definition: TColorGradient.cxx:193
TColorGradient::ECoordinateMode
ECoordinateMode
Definition: TColorGradient.h:44
TRadialGradient::fStart
Point fStart
Definition: TColorGradient.h:162
TColor
The color creation and management class.
Definition: TColor.h:19
TRadialGradient::GetStart
const Point & GetStart() const
Get start.
Definition: TColorGradient.cxx:292
TColor::GetRGB
virtual void GetRGB(Float_t &r, Float_t &g, Float_t &b) const
Definition: TColor.h:51
TColorGradient::GetColors
const Double_t * GetColors() const
Get colors.
Definition: TColorGradient.cxx:159
TLinearGradient
Define a linear color gradient.
Definition: TColorGradient.h:99
TColorGradient::fColorPositions
std::vector< Double_t > fColorPositions
Definition: TColorGradient.h:66
TColorGradient::GetColorPositions
const Double_t * GetColorPositions() const
Get color positions.
Definition: TColorGradient.cxx:151
TRadialGradient::fType
EGradientType fType
Definition: TColorGradient.h:167
TRadialGradient::kExtended
@ kExtended
Definition: TColorGradient.h:132
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:876
TColorGradient::GetNumberOfSteps
SizeType_t GetNumberOfSteps() const
Get number of steps.
Definition: TColorGradient.cxx:143
unsigned int
TRadialGradient::fR1
Double_t fR1
Definition: TColorGradient.h:163
TRadialGradient::kSimple
@ kSimple
Definition: TColorGradient.h:131
TLinearGradient::fStart
Point fStart
Definition: TColorGradient.h:114
TLinearGradient::SetStartEnd
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
Definition: TColorGradient.cxx:218
Double_t
double Double_t
Definition: RtypesCore.h:59
TRadialGradient::SetRadialGradient
void SetRadialGradient(const Point &center, Double_t radius)
Set radial gradient.
Definition: TColorGradient.cxx:324
TObjArray.h
TLinearGradient::GetEnd
const Point & GetEnd() const
Get end.
Definition: TColorGradient.cxx:235
TRadialGradient::GetGradientType
EGradientType GetGradientType() const
Get gradient type.
Definition: TColorGradient.cxx:271
TColorGradient::SizeType_t
std::vector< Color_t >::size_type SizeType_t
Definition: TColorGradient.h:37
TColorGradient::fColors
std::vector< Double_t > fColors
Definition: TColorGradient.h:67
points
point * points
Definition: X3DBuffer.c:22
TColor::fNumber
Int_t fNumber
Color number identifier.
Definition: TColor.h:21
TRadialGradient::TRadialGradient
TRadialGradient()
Definition: TColorGradient.cxx:246
TRadialGradient::fR2
Double_t fR2
Definition: TColorGradient.h:165
TRadialGradient::EGradientType
EGradientType
Definition: TColorGradient.h:130
TColorGradient::SetCoordinateMode
void SetCoordinateMode(ECoordinateMode mode)
Set coordinate mode.
Definition: TColorGradient.cxx:127
TRadialGradient::GetRadius
Double_t GetRadius() const
Get radius.
Definition: TColorGradient.cxx:343
TLinearGradient::fEnd
Point fEnd
Definition: TColorGradient.h:115
TRadialGradient::GetEnd
const Point & GetEnd() const
Get end.
Definition: TColorGradient.cxx:308
TRadialGradient::GetR1
Double_t GetR1() const
Definition: TColorGradient.cxx:300
gROOT
#define gROOT
Definition: TROOT.h:406
TError.h