Logo ROOT   6.10/09
Reference Guide
grad2.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_gl
3 /// Gradient fill with transparency and the "SAME" option.
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 /// \macro_image(nobatch)
9 /// \macro_code
10 ///
11 /// \author Timur Pocheptsov
12 
13 //Includes for ACLiC (cling does not need them).
14 #include "TColorGradient.h"
15 #include "TCanvas.h"
16 #include "TError.h"
17 #include "TColor.h"
18 #include "TStyle.h"
19 #include "TH1F.h"
20 
21 //Aux. functions for tutorials/gl.
22 #include "customcolorgl.h"
23 
24 void grad2()
25 {
26  //1. 'Allocate' four indices for our custom colors.
27  //We can use hard-coded indices like 1001, 1002, 1003 ... but
28  //I prefer to find free indices in the ROOT's color table
29  //to avoid possible conflicts with other tutorials.
30  Color_t freeIndices[4] = {};
31  if (ROOT::GLTutorials::FindFreeCustomColorIndices(freeIndices) != 4) {
32  ::Error("grad2", "can not allocate new custom colors");
33  return;
34  }
35 
36  //'Aliases' (instead of freeIndices[someIndex])
37  const Color_t customRed = freeIndices[0], grad1 = freeIndices[1];
38  const Color_t customGreen = freeIndices[2], grad2 = freeIndices[3];
39 
40  //Make sure canvas supports OpenGL.
42 
43  //2. Check that we have a canvas with an OpenGL support.
44  TCanvas * const cnv = new TCanvas("gradiend demo 2", "gradient demo 2", 100, 100, 800, 600);
45  if (!cnv->UseGL()) {
46  ::Error("grad2", "This macro requires OpenGL");
47  delete cnv;
48  return;
49  }
50 
51  //3. Custom colors:
52  // a) Custom semi-transparent red.
53  new TColor(customRed, 1., 0., 0., "red", 0.5);
54 
55 
56 
57  // b) Gradient (from our semi-transparent red to ROOT's kOrange).
58  // Linear gradient is defined by: 1) colors (to interpolate between them),
59  // 2) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
60  // 3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
61  // and this rect is either: bounding rect of your polygon/object to fill
62  // (gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
63  // or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
64  // kObjectBoundingMode is the default one.
65 
66  const Double_t locations[] = {0., 1.};
67  const Color_t idx1[] = {customRed, kOrange};
68  TLinearGradient * const gradFill1 = new TLinearGradient(grad1, 2, locations, idx1);
69 
70  typedef TColorGradient::Point Point;
71  //Starting and ending points for a gradient fill (it's a vertical gradient):
72  gradFill1->SetStartEnd(Point(0., 0.), Point(0., 1.));
73 
74  // c) Custom semi-transparent green.
75  new TColor(customGreen, 0., 1., 0., "green", 0.5);
76 
77  // d) Gradient from ROOT's kBlue to our custom green.
78  const Color_t idx2[] = {customGreen, kBlue};
79 
80  TLinearGradient * const gradFill2 = new TLinearGradient(grad2, 2, locations, idx2);
81  //Vertical gradient fill.
82  gradFill2->SetStartEnd(Point(0., 0.), Point(0., 1.));
83 
84  TH1F * const hist = new TH1F("a2", "b2", 10, -2., 3.);
85  TH1F * const hist2 = new TH1F("c3", "d3", 10, -3., 3.);
86  hist->FillRandom("landau", 100000);
87  hist2->FillRandom("gaus", 100000);
88 
89  hist->SetFillColor(grad1);
90  hist2->SetFillColor(grad2);
91 
92  hist2->Draw();
93  hist->Draw("SAME");
94 }
unsigned FindFreeCustomColorIndices(T(&indices)[N])
Definition: customcolorgl.h:40
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:311
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
void Error(const char *location, const char *msgfmt,...)
short Color_t
Definition: RtypesCore.h:79
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition: TStyle.h:316
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3294
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
Define a linear color gradient.
The Canvas class.
Definition: TCanvas.h:31
double Double_t
Definition: RtypesCore.h:55
The color creation and management class.
Definition: TColor.h:19
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
Definition: Rtypes.h:56
Bool_t UseGL() const
Definition: TCanvas.h:226
const Bool_t kTRUE
Definition: RtypesCore.h:91
Definition: Rtypes.h:57