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
35
36////////////////////////////////////////////////////////////////////////////////
37/// There is no way to validate parameters here, so it's up to user
38/// to pass correct arguments.
39
41 const Color_t *indices, ECoordinateMode mode)
42 : fCoordinateMode(mode)
43{
44 assert(nPoints != 0 && "TColorGradient, number of points is 0");
45 assert(points != nullptr && "TColorGradient, points parameter is null");
46 assert(indices != nullptr && "TColorGradient, indices parameter is null");
47
48 ResetColor(nPoints, points, indices);
50}
51
52////////////////////////////////////////////////////////////////////////////////
53/// There is no way to validate parameters here, so it's up to user
54/// to pass correct arguments.
55
58 : fCoordinateMode(mode)
59{
60 assert(nPoints != 0 && "TColorGradient, number of points is 0");
61 assert(points != nullptr && "TColorGradient, points parameter is null");
62 assert(colors != nullptr && "TColorGradient, colors parameter is null");
63
66}
67
68////////////////////////////////////////////////////////////////////////////////
69/// Reset color.
70
72{
73 assert(nPoints != 0 && "ResetColor, number of points is 0");
74 assert(points != nullptr && "ResetColor, points parameter is null");
75 assert(colorIndices != nullptr && "ResetColor, colorIndices parameter is null");
76
77 std::vector<Double_t> colors(nPoints * 4);
78
79 for (UInt_t i = 0, pos = 0; i < nPoints; ++i, pos += 4) {
80 const TColor *clearColor = gROOT->GetColor(colorIndices[i]);
81 if (!clearColor) {
82 Error("ResetColor", "Bad color for index %d, set to opaque black", colorIndices[i]);
83 colors[pos] = 0.;
84 colors[pos + 1] = 0.;
85 colors[pos + 2] = 0.;
86 colors[pos + 3] = 1.; //Alpha.
87 } else {
88 if (dynamic_cast<const TColorGradient *>(clearColor))
89 Warning("ResetColor", "Gradient color index %d used as base for other gradient color", colorIndices[i]);
90 colors[pos] = clearColor->GetRed();
91 colors[pos + 1] = clearColor->GetGreen();
92 colors[pos + 2] = clearColor->GetBlue();
93 colors[pos + 3] = clearColor->GetAlpha();
94 }
95 }
96
98}
99
100////////////////////////////////////////////////////////////////////////////////
101/// Reset color.
102
104 const Double_t *colors)
105{
106 assert(nPoints != 0 && "ResetColor, number of points is 0");
107 assert(points != nullptr && "ResetColor, points parameter is null");
108 assert(colors != nullptr && "ResetColor, colors parameter is null");
109
111 fColors.assign(colors, colors + nPoints * 4);
112
113 Double_t sum[4] = { 0., 0., 0., 0. };
114#ifdef __clang__
115 // Clang vectoriser bug (confirmed for clang 20 - 22)
116 // https://github.com/llvm/llvm-project/issues/194008
117 // The store in the loop can smash the stack with e.g. -O2 -mavx2.
118#pragma clang loop vectorize(disable)
119#endif
120 for (unsigned n = 0; n < fColors.size(); ++n)
121 sum[n % 4] += fColors[n];
122
123 SetRGB(sum[0] / nPoints, sum[1] / nPoints, sum[2] / nPoints);
124 SetAlpha(sum[3] / nPoints);
125}
126
127////////////////////////////////////////////////////////////////////////////////
128/// Change alpha parameter of the color
129
131{
132 if (indx*4 + 3 < fColors.size())
133 fColors[indx*4 + 3] = alpha;
134}
135
136////////////////////////////////////////////////////////////////////////////////
137/// Return alpha parameter of selected color
138
140{
141 if (indx*4 + 3 < fColors.size())
142 return fColors[indx*4 + 3];
143
144 return 1.;
145}
146
147////////////////////////////////////////////////////////////////////////////////
148/// Set coordinate mode.
149
154
155////////////////////////////////////////////////////////////////////////////////
156/// Get coordinate mode.
157
162
163////////////////////////////////////////////////////////////////////////////////
164/// Get number of steps.
165
170
171////////////////////////////////////////////////////////////////////////////////
172/// Get color positions
173
175{
176 return fColorPositions.data();
177}
178
179////////////////////////////////////////////////////////////////////////////////
180/// Get colors.
181
183{
184 return fColors.data();
185}
186
187////////////////////////////////////////////////////////////////////////////////
188/// Register color
189
191{
194
195 if (gROOT) {
196 if (gROOT->GetColor(colorIndex)) {
197 Warning("RegisterColor", "Color with index %d is already defined", colorIndex);
198 return;
199 }
200
201 if (TObjArray *colors = (TObjArray*)gROOT->GetListOfColors()) {
202 colors->AddAtAndExpand(this, colorIndex);
203 } else {
204 Error("RegisterColor", "List of colors is a null pointer in gROOT, color was not registered");
205 return;
206 }
207 }
208}
209
210////////////////////////////////////////////////////////////////////////////////
211/// Set end and start.
212
214{
215 fStart = p1;
216 fEnd = p2;
217}
218
219////////////////////////////////////////////////////////////////////////////////
220/// Get start.
221
223{
224 return fStart;
225}
226
227////////////////////////////////////////////////////////////////////////////////
228/// Get end.
229
231{
232 return fEnd;
233}
234
235
236////////////////////////////////////////////////////////////////////////////////
237/// Get gradient type.
238
243
244////////////////////////////////////////////////////////////////////////////////
245/// Set start and end R1 and R2.
246
248{
249 fStart = p1;
250 fR1 = r1;
251 fEnd = p2;
252 fR2 = r2;
253
255}
256
257////////////////////////////////////////////////////////////////////////////////
258/// Get start.
259
261{
262 return fStart;
263}
264
265////////////////////////////////////////////////////////////////////////////////
266// Get R1.
267
269{
270 return fR1;
271}
272
273////////////////////////////////////////////////////////////////////////////////
274/// Get end.
275
277{
278 return fEnd;
279}
280
281////////////////////////////////////////////////////////////////////////////////
282/// Get R2.
283
285{
286 return fR2;
287}
288
289////////////////////////////////////////////////////////////////////////////////
290/// Set radial gradient.
291
293{
294 fStart = center;
295 fR1 = radius;
296
297 fType = kSimple;
298}
299
300////////////////////////////////////////////////////////////////////////////////
301/// Get center.
302
304{
305 return fStart;
306}
307
308////////////////////////////////////////////////////////////////////////////////
309/// Get radius.
310
312{
313 return fR1;
314}
short Color_t
Color number (short)
Definition RtypesCore.h:99
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:411
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:22
virtual void SetRGB(Float_t r, Float_t g, Float_t b)
Initialize this color and its "dark" and "bright" associated colors.
Definition TColor.cxx:1855
void SetName(const char *name) override
Set the color name and change also the name of the "dark" and "bright" associated colors if they exis...
Definition TColor.cxx:1831
Int_t fNumber
Color number identifier.
Definition TColor.h:24
virtual void SetAlpha(Float_t a)
Definition TColor.h:71
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
const Point & GetEnd() const
Get end.
const Point & GetStart() const
Get start.
An array of TObjects.
Definition TObjArray.h:31
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1057
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
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:2384
const Int_t n
Definition legend1.C:16
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2338