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