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 for (unsigned n = 0; n < fColors.size(); ++n)
115 sum[n % 4] += fColors[n];
116
117 SetRGB(sum[0] / nPoints, sum[1] / nPoints, sum[2] / nPoints);
118 SetAlpha(sum[3] / nPoints);
119}
120
121////////////////////////////////////////////////////////////////////////////////
122/// Change alpha parameter of the color
123
125{
126 if (indx*4 + 3 < fColors.size())
127 fColors[indx*4 + 3] = alpha;
128}
129
130////////////////////////////////////////////////////////////////////////////////
131/// Return alpha parameter of selected color
132
134{
135 if (indx*4 + 3 < fColors.size())
136 return fColors[indx*4 + 3];
137
138 return 1.;
139}
140
141////////////////////////////////////////////////////////////////////////////////
142/// Set coordinate mode.
143
148
149////////////////////////////////////////////////////////////////////////////////
150/// Get coordinate mode.
151
156
157////////////////////////////////////////////////////////////////////////////////
158/// Get number of steps.
159
164
165////////////////////////////////////////////////////////////////////////////////
166/// Get color positions
167
169{
170 return fColorPositions.data();
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Get colors.
175
177{
178 return fColors.data();
179}
180
181////////////////////////////////////////////////////////////////////////////////
182/// Register color
183
185{
188
189 if (gROOT) {
190 if (gROOT->GetColor(colorIndex)) {
191 Warning("RegisterColor", "Color with index %d is already defined", colorIndex);
192 return;
193 }
194
195 if (TObjArray *colors = (TObjArray*)gROOT->GetListOfColors()) {
196 colors->AddAtAndExpand(this, colorIndex);
197 } else {
198 Error("RegisterColor", "List of colors is a null pointer in gROOT, color was not registered");
199 return;
200 }
201 }
202}
203
204////////////////////////////////////////////////////////////////////////////////
205/// Set end and start.
206
208{
209 fStart = p1;
210 fEnd = p2;
211}
212
213////////////////////////////////////////////////////////////////////////////////
214/// Get start.
215
217{
218 return fStart;
219}
220
221////////////////////////////////////////////////////////////////////////////////
222/// Get end.
223
225{
226 return fEnd;
227}
228
229
230////////////////////////////////////////////////////////////////////////////////
231/// Get gradient type.
232
237
238////////////////////////////////////////////////////////////////////////////////
239/// Set start and end R1 and R2.
240
242{
243 fStart = p1;
244 fR1 = r1;
245 fEnd = p2;
246 fR2 = r2;
247
249}
250
251////////////////////////////////////////////////////////////////////////////////
252/// Get start.
253
255{
256 return fStart;
257}
258
259////////////////////////////////////////////////////////////////////////////////
260// Get R1.
261
263{
264 return fR1;
265}
266
267////////////////////////////////////////////////////////////////////////////////
268/// Get end.
269
271{
272 return fEnd;
273}
274
275////////////////////////////////////////////////////////////////////////////////
276/// Get R2.
277
279{
280 return fR2;
281}
282
283////////////////////////////////////////////////////////////////////////////////
284/// Set radial gradient.
285
287{
288 fStart = center;
289 fR1 = radius;
290
291 fType = kSimple;
292}
293
294////////////////////////////////////////////////////////////////////////////////
295/// Get center.
296
298{
299 return fStart;
300}
301
302////////////////////////////////////////////////////////////////////////////////
303/// Get radius.
304
306{
307 return fR1;
308}
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:2339