Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TColorGradient.h
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 //Author: Timur Pocheptsov 20/03/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 #ifndef ROOT_TColorGradient
13 #define ROOT_TColorGradient
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TColorGradient //
19 // //
20 // TColorGradient extends basic TColor. //
21 // Actually, this is not a simple color, but linear or radial gradient //
22 // for a filled area. By inheriting from TColor, gradients can be //
23 // placed inside gROOT's list of colors and use it in all TAttXXX //
24 // descendants without modifying any existing code. //
25 // //
26 //////////////////////////////////////////////////////////////////////////
27 
28 #include <vector>
29 
30 #include "Rtypes.h"
31 
32 #include "TColor.h"
33 
34 
35 class TColorGradient : public TColor {
36 public:
37  typedef std::vector<Color_t>::size_type SizeType_t;
38 
39  //TODO: Replace with enum class as soon as we have C++11 enabled by default.
40  //CoordinateMode: both linear and radial gradients require some points - the
41  //start and end points.
42  //We can use either pad's rectangle as a coordinate system
43  //or an object's bounding rect.
44  enum ECoordinateMode {
45  kPadMode,//NDC, in a pad's rectangle (pad is 0,0 - 1,1).
46  kObjectBoundingMode //NDC in an object's bounding rect (this rect is 0,0 - 1, 1).
47  };
48 
49  struct Point {
50  Double_t fX;
51  Double_t fY;
52 
53  Point()
54  : fX(0.), fY(0.)
55  {
56  }
57 
58  Point(Double_t x, Double_t y)
59  : fX(x), fY(y)
60  {
61  }
62  };
63 
64 private:
65  //Positions of color nodes in a gradient, in NDC.
66  std::vector<Double_t> fColorPositions;
67  std::vector<Double_t> fColors;//RGBA values.
68 
69  //'default value' is kObjectBoundingMode.
70  ECoordinateMode fCoordinateMode;
71 
72 protected:
73  TColorGradient();
74 
75  TColorGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
76  const Color_t *colorIndices, ECoordinateMode mode = kObjectBoundingMode);
77  TColorGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
78  const Double_t *colors, ECoordinateMode mode = kObjectBoundingMode);
79 
80 public:
81  void ResetColor(UInt_t nPoints, const Double_t *points,
82  const Color_t *colorIndices);
83  void ResetColor(UInt_t nPoints, const Double_t *points,
84  const Double_t *colorIndices);
85 
86  void SetCoordinateMode(ECoordinateMode mode);
87  ECoordinateMode GetCoordinateMode()const;
88 
89  SizeType_t GetNumberOfSteps()const;
90  const Double_t *GetColorPositions()const;
91  const Double_t *GetColors()const;
92 
93 private:
94  void RegisterColor(Color_t colorIndex);
95 
96  ClassDef(TColorGradient, 0); //Gradient fill.
97 };
98 
99 class TLinearGradient : public TColorGradient {
100 public:
101  //With C++11 we'll use inherited constructors!!!
102  TLinearGradient();
103  TLinearGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
104  const Color_t *colorIndices, ECoordinateMode mode = kObjectBoundingMode);
105  TLinearGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
106  const Double_t *colors, ECoordinateMode mode = kObjectBoundingMode);
107 
108  //points are always in NDC (and also affected by fCoordinateMode).
109  void SetStartEnd(const Point &p1, const Point &p2);
110  const Point &GetStart()const;
111  const Point &GetEnd()const;
112 
113 private:
114  Point fStart;
115  Point fEnd;
116 
117  ClassDef(TLinearGradient, 0); //Linear gradient fill.
118 };
119 
120 //
121 //Radial gradient. Can be either "simple": you specify a center
122 //and radius in NDC coordinates (see comments about linear gradient
123 //and coordinate modes above), or "extended": you have two centers
124 //(start,end) and two radiuses (R1, R2) and interpolation between them;
125 //still start/end and radiuses are in NDC.
126 //
127 
128 class TRadialGradient : public TColorGradient {
129 public:
130  enum EGradientType {
131  kSimple,
132  kExtended
133  };
134 
135 
136  //With C++11 we'll use inherited constructors!!!
137  TRadialGradient();
138  TRadialGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
139  const Color_t *colorIndices, ECoordinateMode mode = kObjectBoundingMode);
140  TRadialGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
141  const Double_t *colors, ECoordinateMode mode = kObjectBoundingMode);
142 
143 
144  EGradientType GetGradientType()const;
145 
146 
147  //Extended gradient.
148  void SetStartEndR1R2(const Point &p1, Double_t r1,
149  const Point &p2, Double_t r2);
150  const Point &GetStart()const;
151  Double_t GetR1()const;
152  const Point &GetEnd()const;
153  Double_t GetR2()const;
154 
155  //Simple radial gradient: the same as extended with
156  //start == end, r1 = 0, r2 = radius.
157  void SetRadialGradient(const Point &center, Double_t radius);
158  const Point &GetCenter()const;
159  Double_t GetRadius()const;
160 
161 private:
162  Point fStart;
163  Double_t fR1 = 0.;
164  Point fEnd;
165  Double_t fR2 = 0.;
166 
167  EGradientType fType = kSimple;
168 
169  ClassDef(TRadialGradient, 0); //Radial gradient fill.
170 };
171 
172 
173 #endif