Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
35 ClassImp(TColorGradient);
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// Constructor.
39 
40 TColorGradient::TColorGradient()
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 
49 TColorGradient::TColorGradient(Color_t colorIndex, UInt_t nPoints, const Double_t *points,
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 
65 TColorGradient::TColorGradient(Color_t colorIndex, UInt_t nPoints, const Double_t *points,
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 
113 void TColorGradient::ResetColor(UInt_t nPoints, const Double_t *points,
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 
127 void TColorGradient::SetCoordinateMode(ECoordinateMode mode)
128 {
129  fCoordinateMode = mode;
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Get coordinate mode.
134 
135 TColorGradient::ECoordinateMode TColorGradient::GetCoordinateMode()const
136 {
137  return fCoordinateMode;
138 }
139 
140 ////////////////////////////////////////////////////////////////////////////////
141 /// Get number of steps.
142 
143 TColorGradient::SizeType_t TColorGradient::GetNumberOfSteps()const
144 {
145  return fColorPositions.size();
146 }
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// Get color positions
150 
151 const Double_t *TColorGradient::GetColorPositions()const
152 {
153  return &fColorPositions[0];
154 }
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 /// Get colors.
158 
159 const Double_t *TColorGradient::GetColors()const
160 {
161  return &fColors[0];
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// Register color
166 
167 void TColorGradient::RegisterColor(Color_t colorIndex)
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 
187 ClassImp(TLinearGradient);
188 
189 /** \class TLinearGradient
190 Define a linear color gradient.
191 */
192 
193 TLinearGradient::TLinearGradient()
194 {
195 }
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 /// Constructor.
199 
200 TLinearGradient::TLinearGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
201  const Color_t *colorIndices, ECoordinateMode mode)
202  : TColorGradient(newColor, nPoints, points, colorIndices, mode)
203 {
204 }
205 
206 ////////////////////////////////////////////////////////////////////////////////
207 /// Constructor.
208 
209 TLinearGradient::TLinearGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
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 
227 const TColorGradient::Point &TLinearGradient::GetStart()const
228 {
229  return fStart;
230 }
231 
232 ////////////////////////////////////////////////////////////////////////////////
233 /// Get end.
234 
235 const TColorGradient::Point &TLinearGradient::GetEnd()const
236 {
237  return fEnd;
238 }
239 
240 ClassImp(TRadialGradient);
241 
242 /** \class TRadialGradient
243 Define a radial color gradient
244 */
245 
246 TRadialGradient::TRadialGradient()
247 {
248 }
249 
250 ////////////////////////////////////////////////////////////////////////////////
251 /// Constructor.
252 
253 TRadialGradient::TRadialGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
254  const Color_t *colorIndices, ECoordinateMode mode)
255  : TColorGradient(newColor, nPoints, points, colorIndices, mode)
256 {
257 }
258 
259 ////////////////////////////////////////////////////////////////////////////////
260 /// Constructor.
261 
262 TRadialGradient::TRadialGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
263  const Double_t *colors, ECoordinateMode mode)
264  : TColorGradient(newColor, nPoints, points, colors, mode)
265 {
266 }
267 
268 ////////////////////////////////////////////////////////////////////////////////
269 /// Get gradient type.
270 
271 TRadialGradient::EGradientType TRadialGradient::GetGradientType()const
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 
292 const TColorGradient::Point &TRadialGradient::GetStart()const
293 {
294  return fStart;
295 }
296 
297 ////////////////////////////////////////////////////////////////////////////////
298 // Get R1.
299 
300 Double_t TRadialGradient::GetR1()const
301 {
302  return fR1;
303 }
304 
305 ////////////////////////////////////////////////////////////////////////////////
306 /// Get end.
307 
308 const TColorGradient::Point &TRadialGradient::GetEnd()const
309 {
310  return fEnd;
311 }
312 
313 ////////////////////////////////////////////////////////////////////////////////
314 /// Get R2.
315 
316 Double_t TRadialGradient::GetR2()const
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 
335 const TColorGradient::Point &TRadialGradient::GetCenter()const
336 {
337  return fStart;
338 }
339 
340 ////////////////////////////////////////////////////////////////////////////////
341 /// Get radius.
342 
343 Double_t TRadialGradient::GetRadius()const
344 {
345  return fR1;
346 }