Logo ROOT  
Reference Guide
gradients.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gl
3/// This macro requires OpenGL, you can:
4/// 1. call gStyle->SetCanvasPreferGL(kTRUE) before a canvas is created or
5/// 2. set OpenGL.CanvasPreferGL to 1 in a $ROOTSYS/etc/system.rootrc.
6///
7/// Features:
8/// 1. Radial and linear gradients
9/// 2. Transparent/semitransparent colours.
10///
11/// \macro_image(nobatch)
12/// \macro_code
13///
14/// \author Timur Pocheptsov
15
16//Includes for ACLiC:
17#include "TColorGradient.h"
18#include "TCanvas.h"
19#include "TError.h"
20#include "TStyle.h"
21#include "TText.h"
22#include "TPie.h"
23
24//Cocoa aux. functions.
25#include "customcolorgl.h"
26
27void gradients()
28{
29 //Find free colour indices in the ROOT's palette for:
30 //1. A radial gradient for TPie;
31 //2. A linear gradient for TCanvas
32 //3. A fully transparent fill color for a nested pad.
33
34 Color_t colorIndices[3] = {};
35 if (ROOT::GLTutorials::FindFreeCustomColorIndices(colorIndices) != 3) {
36 ::Error("gradients", "failed to create new custom colors");
37 return;
38 }
39
40 //Better names:
41 const Color_t &radialFill = colorIndices[0];
42 const Color_t &linearFill = colorIndices[1];
43 const Color_t &transparentFill = colorIndices[2];
44
46
47 TCanvas * const c = new TCanvas("cpie","Gradient colours demo", 700, 700);
48 //Before we allocated any new colour or created any object:
49 if (!c->UseGL()) {
50 ::Error("gradients", "This macro requires OpenGL");
51 delete c;
52 return;
53 }
54
55 //Linear gradient is defined by: 1) colors (to interpolate between them),
56 //2) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
57 //3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
58 //and this rect is either: bounding rect of your polygon/object to fill
59 //(gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
60 //or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
61 //kObjectBoundingMode is the default one.
62
63
64 //Colour positions in the gradient's palette (here I place colors at the
65 //ends of 0-1):
66 const Double_t locations[] = {0., 1.};
67
68 //Linear gradient fill (with an axis angle == 45):
69 const Double_t rgbaData1[] = {0.2, 0.2, 0.2, 1.,/*gray*/
70 0.8, 1., 0.9, 1. /*pale green*/};
71 TLinearGradient * const gradientFill1 = new TLinearGradient(linearFill, 2, locations, rgbaData1);
72 //45 degrees:
73 gradientFill1->SetStartEnd(TColorGradient::Point(0., 0.), TColorGradient::Point(1., 1.));
74 //Set as a background color in the canvas:
75 c->SetFillColor(linearFill);
76
77 //Draw a text in the canvas (the object above the text will be
78 //semi-transparent):
79 TText * const t = new TText(0.05, 0.7, "Can you see the text?");
80 t->Draw();
81
82 //We create a nested pad on top to render a TPie in,
83 //this way we still have a text (below) + TPie with
84 //a fancy colour on top.
85 TPad * const pad = new TPad("p", "p", 0., 0., 1., 1.);
86
87 //TPad itself is fully transparent:
88 new TColor(transparentFill, 1., 1., 1., "transparent_fill_color", 0.);
89 pad->SetFillColor(transparentFill);
90 //Add our pad into the canvas:
91 pad->Draw();
92 pad->cd();
93
94
95 //Radial gradient fill for a TPie object:
96 const Double_t rgbaData2[] = {/*opaque orange at the start:*/1., 0.8, 0., 1.,
97 /*transparent red at the end:*/1., 0.2, 0., 0.65};
98
99 TRadialGradient * const gradientFill2 = new TRadialGradient(radialFill, 2,
100 locations, rgbaData2);
101 //Parameters for a gradient fill:
102 //the gradient is 'pad-related' - we calculate everything in a pad's
103 //space and consider it as a NDC (so pad's rect is (0,0), (1,1)).
105
106 //'Simple' radial gradient (center and radius):
107 gradientFill2->SetRadialGradient(TColorGradient::Point(0.5, 0.5), 0.4);
108
109
110 const UInt_t nSlices = 5;
111 //Values for a TPie (non-const, that's how TPie's ctor is declared):
112 Double_t values[nSlices] = {0.8, 1.2, 1.2, 0.8, 1.};
113 Int_t colors[nSlices] = {radialFill, radialFill, radialFill,
114 radialFill, radialFill};
115
116 TPie * const pie = new TPie("pie", "TPie:", nSlices, values, colors);
117 //One slice is slightly shifted:
118 pie->SetEntryRadiusOffset(2, 0.05);
119 //Move labels to the center (to fit the pad's space):
120 pie->SetLabelsOffset(-0.08);
121 //
122 pie->SetRadius(0.4);
123 pie->Draw("rsc");
124}
#define c(i)
Definition: RSha256.hxx:101
int Int_t
Definition: RtypesCore.h:45
short Color_t
Definition: RtypesCore.h:92
unsigned int UInt_t
Definition: RtypesCore.h:46
double Double_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:100
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
R__EXTERN TStyle * gStyle
Definition: TStyle.h:414
Color * colors
Definition: X3DBuffer.c:21
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
The Canvas class.
Definition: TCanvas.h:23
void SetCoordinateMode(ECoordinateMode mode)
Set coordinate mode.
The color creation and management class.
Definition: TColor.h:19
Define a linear color gradient.
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
The most important graphics class in the ROOT system.
Definition: TPad.h:27
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition: TPad.cxx:597
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1267
Draw a Pie Chart,.
Definition: TPie.h:23
void SetLabelsOffset(Float_t)
Set the distance between the label end the external line of the TPie.
Definition: TPie.cxx:1346
void SetEntryRadiusOffset(Int_t, Double_t)
Set the distance, in the direction of the radius of the slice.
Definition: TPie.cxx:1263
void SetRadius(Double_t)
Set the pie chart's radius' value.
Definition: TPie.cxx:1362
void Draw(Option_t *option="l") override
Draw the pie chart.
Definition: TPie.cxx:275
Define a radial color gradient.
void SetRadialGradient(const Point &center, Double_t radius)
Set radial gradient.
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition: TStyle.h:326
Base class for several text objects.
Definition: TText.h:22
unsigned FindFreeCustomColorIndices(T(&indices)[N])
Definition: customcolorgl.h:40