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, 25/09/2012.
2 //Gradient fill with transparency and "SAME" option.
3 //Requires OS X and ROOT configured with --enable-cocoa.
4 
5 //Includes for ACLiC (cling does not need them).
6 #include "TColorGradient.h"
7 #include "TVirtualX.h"
8 #include "TCanvas.h"
9 #include "TError.h"
10 #include "TColor.h"
11 #include "TH1F.h"
12 
13 //Aux. functions for tutorials/cocoa.
14 #include "customcolor.h"
15 
16 void grad2()
17 {
18  //1. 'Allocate' four indices for our custom colors.
19  //We can use hard-coded indices like 1001, 1002, 1003 ... but
20  //I prefer to find free indices in the ROOT's color table
21  //to avoid possible conflicts with other tutorials.
22  Color_t freeIndices[4] = {};
24  ::Error("grad2", "can not allocate new custom colors");
25  return;
26  }
27 
28  //'Aliases' (instead of freeIndices[someIndex])
29  const Color_t customRed = freeIndices[0], grad1 = freeIndices[1];
30  const Color_t customGreen = freeIndices[2], grad2 = freeIndices[3];
31 
32  //2. Check that we are ROOT with Cocoa back-end enabled.
33  TCanvas * const cnv = new TCanvas("gradiend demo 2", "gradient demo 2", 100, 100, 800, 600);
34  //After canvas was created, gVirtualX should be non-null.
35  if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
36  ::Error("grad2", "This macro works only on OS X with --enable-cocoa");
37  delete cnv;
38  return;
39  }
40 
41  //3. Custom colors:
42  // a) Custom semi-transparent red.
43  new TColor(customRed, 1., 0., 0., "red", 0.5);
44 
45 
46  // b) Gradient (from our semi-transparent red to ROOT's kOrange).
47  // Linear gradient is defined by: 1) colors (to interpolate between them),
48  // 2) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
49  // 3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
50  // and this rect is either: bounding rect of your polygon/object to fill
51  // (gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
52  // or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
53  // kObjectBoundingMode is the default one.
54  const Double_t locations[] = {0., 1.};
55  const Color_t idx1[] = {customRed, kOrange};
56  TLinearGradient * const gradFill1 = new TLinearGradient(grad1, 2, locations, idx1);
57 
58  typedef TColorGradient::Point Point;
59  //Starting and ending points for a gradient fill (it's a vertical gradient):
60  gradFill1->SetStartEnd(Point(0., 0.), Point(0., 1.));
61 
62  // c) Custom semi-transparent green.
63  new TColor(customGreen, 0., 1., 0., "green", 0.5);
64 
65  // d) Gradient from ROOT's kBlue to our custom green.
66  const Color_t idx2[] = {customGreen, kBlue};
67 
68  TLinearGradient * const gradFill2 = new TLinearGradient(grad2, 2, locations, idx2);
69  //Vertical gradient fill.
70  gradFill2->SetStartEnd(Point(0., 0), Point(0., 1.));
71 
72  TH1F * const hist = new TH1F("a2", "b2", 10, -2., 3.);
73  TH1F * const hist2 = new TH1F("c3", "d3", 10, -3., 3.);
74  hist->FillRandom("landau", 100000);
75  hist2->FillRandom("gaus", 100000);
76 
77  hist->SetFillColor(grad1);
78  hist2->SetFillColor(grad2);
79 
80  hist2->Draw();
81  hist->Draw("SAME");
82 }
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition: customcolor.h:44
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
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 gVirtualX
Definition: TVirtualX.h:362
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
Definition: Rtypes.h:62