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