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);
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
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Reset color.
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
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
112 fColors.assign(colors, colors + nPoints * 4);
113
114 Double_t sum[4] = { 0., 0., 0., 0. };
115#ifdef __clang__
116 // Clang vectoriser bug (confirmed for clang 20 - 22)
117 // https://github.com/llvm/llvm-project/issues/194008
118 // The store in the loop can smash the stack with e.g. -O2 -mavx2.
119#pragma clang loop vectorize(disable)
120#endif
121 for (unsigned n = 0; n < fColors.size(); ++n)
122 sum[n % 4] += fColors[n];
123
124 SetRGB(sum[0] / nPoints, sum[1] / nPoints, sum[2] / nPoints);
125 SetAlpha(sum[3] / nPoints);
126}
127
128////////////////////////////////////////////////////////////////////////////////
129/// Change alpha parameter of the color
130
132{
133 if (indx*4 + 3 < fColors.size())
134 fColors[indx*4 + 3] = alpha;
135}
136
137////////////////////////////////////////////////////////////////////////////////
138/// Return alpha parameter of selected color
139
141{
142 if (indx*4 + 3 < fColors.size())
143 return fColors[indx*4 + 3];
144
145 return 1.;
146}
147
148////////////////////////////////////////////////////////////////////////////////
149/// Set coordinate mode.
150
155
156////////////////////////////////////////////////////////////////////////////////
157/// Get coordinate mode.
158
163
164////////////////////////////////////////////////////////////////////////////////
165/// Get number of steps.
166
171
172////////////////////////////////////////////////////////////////////////////////
173/// Get color positions
174
176{
177 return fColorPositions.data();
178}
179
180////////////////////////////////////////////////////////////////////////////////
181/// Get colors.
182
184{
185 return fColors.data();
186}
187
188////////////////////////////////////////////////////////////////////////////////
189/// Register color
190
192{
195
196 if (gROOT) {
197 if (gROOT->GetColor(colorIndex)) {
198 Warning("RegisterColor", "Color with index %d is already defined", colorIndex);
199 return;
200 }
201
202 if (TObjArray *colors = (TObjArray*)gROOT->GetListOfColors()) {
203 colors->AddAtAndExpand(this, colorIndex);
204 } else {
205 Error("RegisterColor", "List of colors is a null pointer in gROOT, color was not registered");
206 return;
207 }
208 }
209}
210
211////////////////////////////////////////////////////////////////////////////////
212/// Set end and start.
213
215{
216 fStart = p1;
217 fEnd = p2;
218}
219
220////////////////////////////////////////////////////////////////////////////////
221/// Get start.
222
224{
225 return fStart;
226}
227
228////////////////////////////////////////////////////////////////////////////////
229/// Get end.
230
232{
233 return fEnd;
234}
235
236
237////////////////////////////////////////////////////////////////////////////////
238/// Get gradient type.
239
244
245////////////////////////////////////////////////////////////////////////////////
246/// Set start and end R1 and R2.
247
249{
250 fStart = p1;
251 fR1 = r1;
252 fEnd = p2;
253 fR2 = r2;
254
256}
257
258////////////////////////////////////////////////////////////////////////////////
259/// Get start.
260
262{
263 return fStart;
264}
265
266////////////////////////////////////////////////////////////////////////////////
267// Get R1.
268
270{
271 return fR1;
272}
273
274////////////////////////////////////////////////////////////////////////////////
275/// Get end.
276
278{
279 return fEnd;
280}
281
282////////////////////////////////////////////////////////////////////////////////
283/// Get R2.
284
286{
287 return fR2;
288}
289
290////////////////////////////////////////////////////////////////////////////////
291/// Set radial gradient.
292
294{
295 fStart = center;
296 fR1 = radius;
297
298 fType = kSimple;
299}
300
301////////////////////////////////////////////////////////////////////////////////
302/// Get center.
303
305{
306 return fStart;
307}
308
309////////////////////////////////////////////////////////////////////////////////
310/// Get radius.
311
313{
314 return fR1;
315}
short Color_t
Definition RtypesCore.h:85
#define ClassImp(name)
Definition Rtypes.h:374
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:414
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:1856
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:1832
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:2378
const Int_t n
Definition legend1.C:16
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345