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 // b) Gradient (from our semi-transparent red to ROOT's kOrange).
51 // Linear gradient is defined by: 1) colors (to interpolate between them),
52 // 2) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
53 // 3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
54 // and this rect is either: bounding rect of your polygon/object to fill
55 // (gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
56 // or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
57 // kObjectBoundingMode is the default one.
58 const Double_t locations[] = {0., 1.};
59 const Color_t idx1[] = {customRed, kOrange};
60 TLinearGradient *const gradFill1 = new TLinearGradient(grad1, 2, locations, idx1);
61
62 typedef TColorGradient::Point Point;
63 // Starting and ending points for a gradient fill (it's a vertical gradient):
64 gradFill1->SetStartEnd(Point(0., 0.), Point(0., 1.));
65
66 // c) Custom semi-transparent green.
67 new TColor(customGreen, 0., 1., 0., "green", 0.5);
68
69 // d) Gradient from ROOT's kBlue to our custom green.
70 const Color_t idx2[] = {customGreen, kBlue};
71
72 TLinearGradient *const gradFill2 = new TLinearGradient(grad2, 2, locations, idx2);
73 // Vertical gradient fill.
74 gradFill2->SetStartEnd(Point(0., 0), Point(0., 1.));
75
76 TH1F *const hist = new TH1F("a2", "b2", 10, -2., 3.);
77 TH1F *const hist2 = new TH1F("c3", "d3", 10, -3., 3.);
78 hist->FillRandom("landau", 100000);
79 hist2->FillRandom("gaus", 100000);
80
81 hist->SetFillColor(grad1);
82 hist2->SetFillColor(grad2);
83
84 hist2->Draw();
85 hist->Draw("SAME");
86}
short Color_t
Definition RtypesCore.h:85
double Double_t
Definition RtypesCore.h:59
@ 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:185
#define gVirtualX
Definition TVirtualX.h:337
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:21
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:623
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3530
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3068
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition customcolor.h:38