Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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//Includes for ACLiC (cling does not need them).
11#include "TColorGradient.h"
12#include "TVirtualX.h"
13#include "TCanvas.h"
14#include "TError.h"
15#include "TColor.h"
16#include "TH1F.h"
17
18//Aux. functions for tutorials/cocoa.
19#include "customcolor.h"
20
21void grad2()
22{
23 //1. 'Allocate' four indices for our custom colors.
24 //We can use hard-coded indices like 1001, 1002, 1003 ... but
25 //I prefer to find free indices in the ROOT's color table
26 //to avoid possible conflicts with other tutorials.
27 Color_t freeIndices[4] = {};
29 ::Error("grad2", "can not allocate new custom colors");
30 return;
31 }
32
33 //'Aliases' (instead of freeIndices[someIndex])
34 const Color_t customRed = freeIndices[0], grad1 = freeIndices[1];
35 const Color_t customGreen = freeIndices[2], grad2 = freeIndices[3];
36
37 //2. Check that we are ROOT with Cocoa back-end enabled.
38 TCanvas * const cnv = new TCanvas("gradient demo 2", "gradient demo 2", 100, 100, 800, 600);
39 //After canvas was created, gVirtualX should be non-null.
40 if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
41 ::Error("grad2", "This macro works only on OS X with --enable-cocoa");
42 delete cnv;
43 return;
44 }
45
46 //3. Custom colors:
47 // a) Custom semi-transparent red.
48 new TColor(customRed, 1., 0., 0., "red", 0.5);
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 const Double_t locations[] = {0., 1.};
60 const Color_t idx1[] = {customRed, kOrange};
61 TLinearGradient * const gradFill1 = new TLinearGradient(grad1, 2, locations, idx1);
62
63 typedef TColorGradient::Point Point;
64 //Starting and ending points for a gradient fill (it's a vertical gradient):
65 gradFill1->SetStartEnd(Point(0., 0.), Point(0., 1.));
66
67 // c) Custom semi-transparent green.
68 new TColor(customGreen, 0., 1., 0., "green", 0.5);
69
70 // d) Gradient from ROOT's kBlue to our custom green.
71 const Color_t idx2[] = {customGreen, kBlue};
72
73 TLinearGradient * const gradFill2 = new TLinearGradient(grad2, 2, locations, idx2);
74 //Vertical gradient fill.
75 gradFill2->SetStartEnd(Point(0., 0), Point(0., 1.));
76
77 TH1F * const hist = new TH1F("a2", "b2", 10, -2., 3.);
78 TH1F * const hist2 = new TH1F("c3", "d3", 10, -3., 3.);
79 hist->FillRandom("landau", 100000);
80 hist2->FillRandom("gaus", 100000);
81
82 hist->SetFillColor(grad1);
83 hist2->SetFillColor(grad2);
84
85 hist2->Draw();
86 hist->Draw("SAME");
87}
double Double_t
Definition RtypesCore.h:59
short Color_t
Definition RtypesCore.h:92
@ kOrange
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:66
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:187
#define gVirtualX
Definition TVirtualX.h:338
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
The Canvas class.
Definition TCanvas.h:23
The color creation and management class.
Definition TColor.h:19
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition TH1.cxx:3526
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3074
Define a linear color gradient.
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition customcolor.h:38