Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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-2023, 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/// There is no way to validate parameters here, so it's up to user
39/// to pass correct arguments.
40
42 const Color_t *indices, ECoordinateMode mode)
43 : fCoordinateMode(mode)
44{
45 assert(nPoints != 0 && "TColorGradient, number of points is 0");
46 assert(points != nullptr && "TColorGradient, points parameter is null");
47 assert(indices != nullptr && "TColorGradient, indices parameter is null");
48
49 ResetColor(nPoints, points, indices);
50 RegisterColor(colorIndex);
51}
52
53////////////////////////////////////////////////////////////////////////////////
54/// There is no way to validate parameters here, so it's up to user
55/// to pass correct arguments.
56
59 : fCoordinateMode(mode)
60{
61 assert(nPoints != 0 && "TColorGradient, number of points is 0");
62 assert(points != nullptr && "TColorGradient, points parameter is null");
63 assert(colors != nullptr && "TColorGradient, colors parameter is null");
64
65 ResetColor(nPoints, points, colors);
66 RegisterColor(colorIndex);
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Reset color.
72void TColorGradient::ResetColor(UInt_t nPoints, const Double_t *points, const Color_t *colorIndices)
74 assert(nPoints != 0 && "ResetColor, number of points is 0");
75 assert(points != nullptr && "ResetColor, points parameter is null");
76 assert(colorIndices != nullptr && "ResetColor, colorIndices parameter is null");
77
78 std::vector<Double_t> colors(nPoints * 4);
79
80 for (UInt_t i = 0, pos = 0; i < nPoints; ++i, pos += 4) {
81 const TColor *clearColor = gROOT->GetColor(colorIndices[i]);
82 if (!clearColor) {
83 Error("ResetColor", "Bad color for index %d, set to opaque black", colorIndices[i]);
84 colors[pos] = 0.;
85 colors[pos + 1] = 0.;
86 colors[pos + 2] = 0.;
87 colors[pos + 3] = 1.; //Alpha.
88 } else {
89 if (dynamic_cast<const TColorGradient *>(clearColor))
90 Warning("ResetColor", "Gradient color index %d used as base for other gradient color", colorIndices[i]);
91 colors[pos] = clearColor->GetRed();
92 colors[pos + 1] = clearColor->GetGreen();
93 colors[pos + 2] = clearColor->GetBlue();
94 colors[pos + 3] = clearColor->GetAlpha();
95 }
96 }
97
98 ResetColor(nPoints, points, colors.data());
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// Reset color.
103
105 const Double_t *colors)
106{
107 assert(nPoints != 0 && "ResetColor, number of points is 0");
108 assert(points != nullptr && "ResetColor, points parameter is null");
109 assert(colors != nullptr && "ResetColor, colors parameter is null");
110
111 fColorPositions.assign(points, points + nPoints);
112 fColors.assign(colors, colors + nPoints * 4);
113
114 Double_t sum[4] = { 0., 0., 0., 0. };
115 for (unsigned n = 0; n < fColors.size(); ++n)
116 sum[n % 4] += fColors[n];
117
118 SetRGB(sum[0] / nPoints, sum[1] / nPoints, sum[2] / nPoints);
119 SetAlpha(sum[3] / nPoints);
120}
121
122////////////////////////////////////////////////////////////////////////////////
123/// Change alpha parameter of the color
124
126{
127 if (indx*4 + 3 < fColors.size())
128 fColors[indx*4 + 3] = alpha;
129}
130
131////////////////////////////////////////////////////////////////////////////////
132/// Return alpha parameter of selected color
133
135{
136 if (indx*4 + 3 < fColors.size())
137 return fColors[indx*4 + 3];
138
139 return 1.;
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// Set coordinate mode.
144
146{
148}
149
150////////////////////////////////////////////////////////////////////////////////
151/// Get coordinate mode.
152
154{
155 return fCoordinateMode;
156}
157
158////////////////////////////////////////////////////////////////////////////////
159/// Get number of steps.
160
162{
163 return fColorPositions.size();
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// Get color positions
168
170{
171 return fColorPositions.data();
172}
173
174////////////////////////////////////////////////////////////////////////////////
175/// Get colors.
176
178{
179 return fColors.data();
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Register color
184
186{
187 fNumber = colorIndex;
188 SetName(TString::Format("Color%d", colorIndex));
189
190 if (gROOT) {
191 if (gROOT->GetColor(colorIndex)) {
192 Warning("RegisterColor", "Color with index %d is already defined", colorIndex);
193 return;
194 }
195
196 if (TObjArray *colors = (TObjArray*)gROOT->GetListOfColors()) {
197 colors->AddAtAndExpand(this, colorIndex);
198 } else {
199 Error("RegisterColor", "List of colors is a null pointer in gROOT, color was not registered");
200 return;
201 }
202 }
203}
204
205////////////////////////////////////////////////////////////////////////////////
206/// Set end and start.
207
208void TLinearGradient::SetStartEnd(const Point &p1, const Point &p2)
209{
210 fStart = p1;
211 fEnd = p2;
212}
213
214////////////////////////////////////////////////////////////////////////////////
215/// Get start.
216
218{
219 return fStart;
220}
221
222////////////////////////////////////////////////////////////////////////////////
223/// Get end.
224
226{
227 return fEnd;
228}
229
230
231////////////////////////////////////////////////////////////////////////////////
232/// Get gradient type.
233
235{
236 return fType;
237}
238
239////////////////////////////////////////////////////////////////////////////////
240/// Set start and end R1 and R2.
241
243{
244 fStart = p1;
245 fR1 = r1;
246 fEnd = p2;
247 fR2 = r2;
248
250}
251
252////////////////////////////////////////////////////////////////////////////////
253/// Get start.
254
256{
257 return fStart;
258}
259
260////////////////////////////////////////////////////////////////////////////////
261// Get R1.
262
264{
265 return fR1;
266}
267
268////////////////////////////////////////////////////////////////////////////////
269/// Get end.
270
272{
273 return fEnd;
274}
275
276////////////////////////////////////////////////////////////////////////////////
277/// Get R2.
278
280{
281 return fR2;
282}
283
284////////////////////////////////////////////////////////////////////////////////
285/// Set radial gradient.
286
288{
289 fStart = center;
290 fR1 = radius;
291
292 fType = kSimple;
293}
294
295////////////////////////////////////////////////////////////////////////////////
296/// Get center.
297
299{
300 return fStart;
301}
302
303////////////////////////////////////////////////////////////////////////////////
304/// Get radius.
305
307{
308 return fR1;
309}
short Color_t
Definition RtypesCore.h:85
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t TPoint TPoint const char mode
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t points
#define gROOT
Definition TROOT.h:406
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
Double_t GetColorAlpha(UInt_t indx) const
Return alpha parameter of selected color.
void SetCoordinateMode(ECoordinateMode mode)
Set coordinate mode.
void ResetColor(UInt_t nPoints, const Double_t *points, const Color_t *colorIndices)
Reset color.
void SetColorAlpha(UInt_t indx, Double_t alpha)
Change alpha parameter of the 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:21
virtual void SetRGB(Float_t r, Float_t g, Float_t b)
Initialize this color and its associated colors.
Definition TColor.cxx:1775
Float_t GetRed() const
Definition TColor.h:60
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition TColor.cxx:1839
Float_t GetAlpha() const
Definition TColor.h:66
Float_t GetBlue() const
Definition TColor.h:62
Float_t GetGreen() const
Definition TColor.h:61
Int_t fNumber
Color number identifier.
Definition TColor.h:23
virtual void SetAlpha(Float_t a)
Definition TColor.h:70
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:31
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:973
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
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:2378
const Int_t n
Definition legend1.C:16
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345