ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
grad.C
Go to the documentation of this file.
1 //Author: Timur Pocheptsov, 25/09/2012 (?)
2 //This macro shows how to create and use linear gradients to fill
3 //a histogram or a pad.
4 //Requires OS X and ROOT configured with --enable-cocoa.
5 
6 //Includes for ACLiC (cling does not need them).
7 #include "TColorGradient.h"
8 #include "TVirtualX.h"
9 #include "TCanvas.h"
10 #include "TColor.h"
11 #include "TError.h"
12 #include "TH1F.h"
13 
14 //Aux. functions for tutorials/cocoa.
15 #include "customcolor.h"
16 
17 void grad()
18 {
19  //1. Try to 'allocate' five indices for our custom colors.
20  //We can use hard-coded indices like 1001, 1002, 1003, ... but
21  //I prefer to find free indices in the ROOT's color table
22  //to avoid possible conflicts with other tutorials.
23  Color_t colorIndices[5] = {};
24  if (ROOT::CocoaTutorials::FindFreeCustomColorIndices(colorIndices) != 5) {
25  ::Error("grad", "failed to create new custom colors");
26  return;
27  }
28 
29  //2. Test if we have the right GUI back-end:
30  TCanvas * const cnv = new TCanvas("gradient demo 1", "gradient demo 1", 100, 100, 600, 600);
31  //After canvas was created, gVirtualX should be non-null.
32  if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
33  ::Error("grad", "This macro works only on MacOS X with --enable-cocoa");
34  delete cnv;
35  return;
36  }
37 
38  typedef TColorGradient::Point Point;
39 
40  //3. Create custom colors.
41  //Linear gradient is defined by: 1) colors (to interpolate between them),
42  //2) coordinates for these colors along the gradient axis [0., 1.].
43  //3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
44  //and this rect is either: bounding rect of your polygon/object to fill
45  //(gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
46  //or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
47  //kObjectBoundingMode is the default one.
48 
49  const Color_t &frameGradient = colorIndices[2];//This gradient is a mixture of colorIndices[0] and colorIndices[1]
50  //Fill color for a pad frame:
51  {
52  new TColor(colorIndices[0], 0.25, 0.25, 0.25, "special pad color1", 0.55);
53  new TColor(colorIndices[1], 1., 1., 1., "special pad color2", 0.05);
54 
55  const Double_t locations[] = {0., 0.2, 0.8, 1.};
56  const Color_t gradientIndices[4] = {colorIndices[0], colorIndices[1], colorIndices[1], colorIndices[0]};
57 
58  //Gradient for a pad's frame.
59  TLinearGradient * const gradFill1 = new TLinearGradient(frameGradient, 4, locations, gradientIndices);
60  //Horizontal:
61  gradFill1->SetStartEnd(Point(0., 0.), Point(1., 0.));
62  }
63 
64  //This gradient is a mixture of two standard colors:
65  const Color_t &padGradient = colorIndices[3];
66  //Fill color for a pad:
67  {
68  const Double_t locations[] = {0., 1.};
69  const Color_t gradientIndices[2] = {30, 38};//We create a gradient from system colors.
70 
71  //Gradient for a pad.
72  TLinearGradient * const gradFill2 = new TLinearGradient(padGradient, 2, locations, gradientIndices);
73  //Vertical:
74  gradFill2->SetStartEnd(Point(0., 0.), Point(0., 1.));
75  }
76 
77  //Another gradient from three standard colors:
78  const Color_t &histGradient = colorIndices[4];
79  //Fill color for a histogram:
80  {
81  const Color_t gradientIndices[3] = {kYellow, kOrange, kRed};
82  const Double_t locations[3] = {0., 0.5, 1.};
83 
84  //Gradient for a histogram.
85  TLinearGradient * const gradFill3 = new TLinearGradient(histGradient, 3, locations, gradientIndices);
86  //Vertical:
87  gradFill3->SetStartEnd(Point(0., 0.), Point(0., 1.));
88  }
89 
90  cnv->SetFillColor(padGradient);
91  cnv->SetFrameFillColor(frameGradient);
92 
93  TH1F * const hist = new TH1F("a1", "b1", 20, -3., 3.);
94  hist->SetFillColor(histGradient);
95  hist->FillRandom("gaus", 100000);
96  hist->Draw();
97 }
void grad()
Definition: grad.C:17
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition: customcolor.h:44
Definition: Rtypes.h:61
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
Definition: Rtypes.h:61
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.
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.
void SetFrameFillColor(Color_t color=1)
Definition: TAttPad.h:83
Definition: Rtypes.h:62