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
16TColorGradient extends basic TColor.
17Actually, this is not a simple color, but linear gradient + shadow
18for filled area. By inheriting from TColor, gradients can be placed
19inside gROOT's list of colors and use it in all TAttXXX descendants
20without modifying any existing code.
21
22Shadow, of course, is not a property of any color, and gradient is
23not, but this is the best way to add new attributes to filled area
24without 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 != nullptr && "TColorGradient, points parameter is null");
55 assert(indices != nullptr && "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 != nullptr && "TColorGradient, points parameter is null");
71 assert(colors != nullptr && "TColorGradient, colors parameter is null");
72
73 ResetColor(nPoints, points, colors);
74 RegisterColor(colorIndex);
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Reset color.
79
80void 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 != nullptr && "ResetColor, points parameter is null");
84 assert(colorIndices != nullptr && "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 != nullptr && "ResetColor, points parameter is null");
118 assert(colors != nullptr && "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
190Define 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
218void 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
243Define 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
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}
unsigned int UInt_t
Definition: RtypesCore.h:46
double Double_t
Definition: RtypesCore.h:59
short Color_t
Definition: RtypesCore.h:92
float Float_t
Definition: RtypesCore.h:57
#define ClassImp(name)
Definition: Rtypes.h:364
#define gROOT
Definition: TROOT.h:404
point * points
Definition: X3DBuffer.c:22
Color * colors
Definition: X3DBuffer.c:21
TColorGradient extends basic TColor.
void RegisterColor(Color_t colorIndex)
Register color.
const Double_t * GetColors() const
Get colors.
std::vector< Double_t > fColorPositions
std::vector< Color_t >::size_type SizeType_t
TColorGradient()
Constructor.
void SetCoordinateMode(ECoordinateMode mode)
Set coordinate mode.
void ResetColor(UInt_t nPoints, const Double_t *points, const Color_t *colorIndices)
Reset color.
SizeType_t GetNumberOfSteps() const
Get number of steps.
ECoordinateMode fCoordinateMode
ECoordinateMode GetCoordinateMode() const
Get coordinate mode.
const Double_t * GetColorPositions() const
Get color positions.
std::vector< Double_t > fColors
The color creation and management class.
Definition: TColor.h:19
virtual void GetRGB(Float_t &r, Float_t &g, Float_t &b) const
Definition: TColor.h:52
Float_t GetAlpha() const
Definition: TColor.h:64
Int_t fNumber
Color number identifier.
Definition: TColor.h:21
Define a linear color gradient.
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
const Point & GetEnd() const
Get end.
const Point & GetStart() const
Get start.
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
An array of TObjects.
Definition: TObjArray.h:37
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:879
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:893
Define a radial color gradient.
Double_t GetR1() const
Double_t GetRadius() const
Get radius.
void SetStartEndR1R2(const Point &p1, Double_t r1, const Point &p2, Double_t r2)
Set start and end R1 and R2.
const Point & GetStart() const
Get start.
Double_t GetR2() const
Get R2.
const Point & GetCenter() const
Get center.
EGradientType fType
EGradientType GetGradientType() const
Get gradient type.
const Point & GetEnd() const
Get end.
void SetRadialGradient(const Point &center, Double_t radius)
Set radial gradient.
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:2336