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