ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
grad.C
Go to the documentation of this file.
1 //Author: Timur Pocheptsov, 19/03/2014.
2 //This macro demonstrates how to create and use linear gradients to fill
3 //a histogram or a pad.
4 //To use this macro you need OpenGL enabled in pad:
5 //either set OpenGL.CanvasPreferGL to 1 in $ROOTSYS/etc/system.rootrc;
6 //or call gStyle->SetCanvasPreferGL(kTRUE); before canvas created.
7 
8 //Includes for ACLiC (cling does not need them).
9 #include "TColorGradient.h"
10 #include "TCanvas.h"
11 #include "TColor.h"
12 #include "TStyle.h"
13 #include "TError.h"
14 #include "TH1F.h"
15 
16 //Aux. functions for tutorials/gl.
17 #include "customcolorgl.h"
18 
19 //______________________________________________________________________
20 void grad()
21 {
22  //1. Try to 'allocate' five indices for our custom colors.
23  //We can use hard-coded indices like 1001, 1002, 1003, ... but
24  //I prefer to find free indices in the ROOT's color table
25  //to avoid possible conflicts with other tutorials.
26  Color_t colorIndices[5] = {};
27  if (ROOT::GLTutorials::FindFreeCustomColorIndices(colorIndices) != 5) {
28  ::Error("grad", "failed to create new custom colors");
29  return;
30  }
31 
32  //Make sure we enabled OpenGL support in a canvas.
34 
35  //2. Test if canvas supports OpenGL:
36  TCanvas * const cnv = new TCanvas("gradient demo 1", "gradient demo 1", 100, 100, 600, 600);
37  if (!cnv->UseGL()) {
38  ::Error("grad", "This macro requires OpenGL");
39  delete cnv;
40  return;
41  }
42 
43  typedef TColorGradient::Point Point;
44 
45  //3. Create custom colors:
46  //Linear gradient is defined by: 1) colors (to interpolate between them),
47  //2) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
48  //3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
49  //and this rect is either: bounding rect of your polygon/object to fill
50  //(gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
51  //or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
52  //kObjectBoundingMode is the default one.
53 
54  const Color_t &frameGradient = colorIndices[2];//This gradient is a mixture of colorIndices[0] and colorIndices[1]
55  //Fill color for a pad frame:
56  {
57  new TColor(colorIndices[0], 0.25, 0.25, 0.25, "special pad color1", 0.55);
58  new TColor(colorIndices[1], 1., 1., 1., "special pad color2", 0.05);
59 
60  //dark-white-white-dark:
61  const Double_t locations[] = {0., 0.2, 0.8, 1.};
62  const Color_t gradientIndices[4] = {colorIndices[0], colorIndices[1], colorIndices[1], colorIndices[0]};
63 
64  //Gradient for a pad's frame.
65  TLinearGradient * const gradFill1 = new TLinearGradient(frameGradient, 4, locations, gradientIndices);
66  //Horizontal:
67  gradFill1->SetStartEnd(Point(0., 0.), Point(1., 0.));
68  }
69 
70  //This gradient is a mixture of two standard colors.
71  const Color_t &padGradient = colorIndices[3];
72  //Fill color for a pad:
73  {
74  const Double_t locations[] = {0., 1.};
75  const Color_t gradientIndices[4] = {30, 38};//We create a gradient from system colors.
76 
77  //Gradient for a pad.
78  TLinearGradient * const gradFill2 = new TLinearGradient(padGradient, 2, locations, gradientIndices);
79  //Vertical:
80  gradFill2->SetStartEnd(Point(0., 0.), Point(0., 1.));
81  }
82 
83  //Another gradient built from three standard colors.
84  const Color_t &histGradient = colorIndices[4];
85  //Fill color for a histogram:
86  {
87  const Color_t gradientIndices[3] = {kYellow, kOrange, kRed};
88  const Double_t locations[3] = {0., 0.5, 1.};
89 
90  //Gradient for a histogram.
91  TLinearGradient * const gradFill3 = new TLinearGradient(histGradient, 3, locations, gradientIndices);
92  //Vertical:
93  gradFill3->SetStartEnd(Point(0., 0.), Point(0., 1.));
94  }
95 
96  cnv->SetFillColor(padGradient);
97  cnv->SetFrameFillColor(frameGradient);
98 
99  TH1F * const hist = new TH1F("a1", "b1", 20, -3., 3.);
100  hist->SetFillColor(histGradient);
101  hist->FillRandom("gaus", 100000);
102  hist->Draw();
103 }
unsigned FindFreeCustomColorIndices(T(&indices)[N])
Definition: customcolorgl.h:44
void grad()
Definition: grad.C:17
Bool_t UseGL() const
Definition: TCanvas.h:242
Definition: Rtypes.h:61
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
Definition: Rtypes.h:61
void Error(const char *location, const char *msgfmt,...)
short Color_t
Definition: RtypesCore.h:79
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition: TStyle.h:337
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3330
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
Define a linear color gradient.
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
The color creation and management class.
Definition: TColor.h:47
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
void SetFrameFillColor(Color_t color=1)
Definition: TAttPad.h:83
const Bool_t kTRUE
Definition: Rtypes.h:91
Definition: Rtypes.h:62