ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
grad2.C
Go to the documentation of this file.
1 //Author: Timur Pocheptsov, 19/03/2014.
2 //Gradient fill with transparency and the "SAME" option.
3 //To use this macro you need OpenGL enabled in pad:
4 //either set OpenGL.CanvasPreferGL to 1 in $ROOTSYS/etc/system.rootrc;
5 //or call gStyle->SetCanvasPreferGL(kTRUE); before canvas created.
6 
7 //Includes for ACLiC (cling does not need them).
8 #include "TColorGradient.h"
9 #include "TCanvas.h"
10 #include "TError.h"
11 #include "TColor.h"
12 #include "TStyle.h"
13 #include "TH1F.h"
14 
15 //Aux. functions for tutorials/gl.
16 #include "customcolorgl.h"
17 
18 void grad2()
19 {
20  //1. 'Allocate' four indices for our custom colors.
21  //We can use hard-coded indices like 1001, 1002, 1003 ... but
22  //I prefer to find free indices in the ROOT's color table
23  //to avoid possible conflicts with other tutorials.
24  Color_t freeIndices[4] = {};
25  if (ROOT::GLTutorials::FindFreeCustomColorIndices(freeIndices) != 4) {
26  ::Error("grad2", "can not allocate new custom colors");
27  return;
28  }
29 
30  //'Aliases' (instead of freeIndices[someIndex])
31  const Color_t customRed = freeIndices[0], grad1 = freeIndices[1];
32  const Color_t customGreen = freeIndices[2], grad2 = freeIndices[3];
33 
34  //Make sure canvas supports OpenGL.
36 
37  //2. Check that we have a canvas with an OpenGL support.
38  TCanvas * const cnv = new TCanvas("gradiend demo 2", "gradient demo 2", 100, 100, 800, 600);
39  if (!cnv->UseGL()) {
40  ::Error("grad2", "This macro requires OpenGL");
41  delete cnv;
42  return;
43  }
44 
45  //3. Custom colors:
46  // a) Custom semi-transparent red.
47  new TColor(customRed, 1., 0., 0., "red", 0.5);
48 
49 
50 
51  // b) Gradient (from our semi-transparent red to ROOT's kOrange).
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 Double_t locations[] = {0., 1.};
61  const Color_t idx1[] = {customRed, kOrange};
62  TLinearGradient * const gradFill1 = new TLinearGradient(grad1, 2, locations, idx1);
63 
64  typedef TColorGradient::Point Point;
65  //Starting and ending points for a gradient fill (it's a vertical gradient):
66  gradFill1->SetStartEnd(Point(0., 0.), Point(0., 1.));
67 
68  // c) Custom semi-transparent green.
69  new TColor(customGreen, 0., 1., 0., "green", 0.5);
70 
71  // d) Gradient from ROOT's kBlue to our custom green.
72  const Color_t idx2[] = {customGreen, kBlue};
73 
74  TLinearGradient * const gradFill2 = new TLinearGradient(grad2, 2, locations, idx2);
75  //Vertical gradient fill.
76  gradFill2->SetStartEnd(Point(0., 0.), Point(0., 1.));
77 
78  TH1F * const hist = new TH1F("a2", "b2", 10, -2., 3.);
79  TH1F * const hist2 = new TH1F("c3", "d3", 10, -3., 3.);
80  hist->FillRandom("landau", 100000);
81  hist2->FillRandom("gaus", 100000);
82 
83  hist->SetFillColor(grad1);
84  hist2->SetFillColor(grad2);
85 
86  hist2->Draw();
87  hist->Draw("SAME");
88 }
unsigned FindFreeCustomColorIndices(T(&indices)[N])
Definition: customcolorgl.h:44
Bool_t UseGL() const
Definition: TCanvas.h:242
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
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.
void grad2()
Definition: grad2.C:16
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.
Definition: Rtypes.h:61
const Bool_t kTRUE
Definition: Rtypes.h:91
Definition: Rtypes.h:62