Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
gradients.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gl
3/// Features:
4/// 1. Radial and linear gradients
5/// 2. Transparent/semitransparent colours.
6///
7/// \macro_image(nobatch)
8/// \macro_code
9///
10/// \authors Timur Pocheptsov, Sergey Linev
11
12//Includes for ACLiC:
13#include "TColorGradient.h"
14#include "TCanvas.h"
15#include "TError.h"
16#include "TStyle.h"
17#include "TText.h"
18#include "TPie.h"
19
20
21void gradients(bool gl = true)
22{
23 //Find free colour indices in the ROOT's palette for:
24 //1. A radial gradient for TPie;
25 //2. A linear gradient for TCanvas
26 //3. A fully transparent fill color for a nested pad.
27
29
30 auto c = new TCanvas("cpie","Gradient colours demo", 700, 700);
31 //Before we allocated any new colour or created any object:
32 if (!c->UseGL() && !c->IsWeb())
33 ::Warning("gradients", "This macro requires either OpenGL or Web canvas to correctly handle gradient colors");
34
35 //Linear gradient is defined by: 1) colors (to interpolate between them),
36 //2) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
37 //3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
38 //and this rect is either: bounding rect of your polygon/object to fill
39 //(gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
40 //or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
41 //kObjectBoundingMode is the default one.
42
43
44 //Draw a text in the canvas (the object above the text will be
45 //semi-transparent):
46 auto t = new TText(0.05, 0.7, "Can you see the text?");
47 t->Draw();
48
49 //We create a nested pad on top to render a TPie in,
50 //this way we still have a text (below) + TPie with
51 //a fancy colour on top.
52 auto pad = new TPad("p", "p", 0., 0., 1., 1.);
53
54 //TPad itself is fully transparent:
55 auto transparentFill = TColor::GetColor((Float_t) 1., 1., 1., 0.);
56 pad->SetFillColor(transparentFill);
57 //Add our pad into the canvas:
58 pad->Draw();
59 pad->cd();
60
61
62 //Radial gradient fill for a TPie object:
63 auto col3 = TColor::GetColor((Float_t) 1., 0.8, 0., 1.); /*opaque orange at the start:*/
64 auto col4 = TColor::GetColor((Float_t) 1., 0.2, 0., 0.65); /*transparent red at the end:*/
65
66 //'Simple' radial gradient with radius 0.4
67 auto radialFill = TColor::GetRadialGradient(0.4, {col3, col4});
68
69
70 //Linear gradient fill (with an axis angle == 45):
71 auto col1 = TColor::GetColor((Float_t) 0.2, 0.2, 0.2); /*gray*/
72 auto col2 = TColor::GetColor((Float_t) 0.8, 1., 0.9); /*pale green*/
73 auto linearFill = TColor::GetLinearGradient(45., {col1, col2}); //45 degrees:
74
75 //Set as a background color in the canvas:
76 c->SetFillColor(linearFill);
77
78
79 const UInt_t nSlices = 5;
80 //Values for a TPie (non-const, that's how TPie's ctor is declared):
81 Double_t values[nSlices] = {0.8, 1.2, 1.2, 0.8, 1.};
82 Int_t colors[nSlices] = {radialFill, radialFill, radialFill,
83 radialFill, radialFill};
84
85 TPie * const pie = new TPie("pie", "TPie:", nSlices, values, colors);
86 //One slice is slightly shifted:
87 pie->SetEntryRadiusOffset(2, 0.05);
88 //Move labels to the center (to fit the pad's space):
89 pie->SetLabelsOffset(-0.08);
90 //
91 pie->SetRadius(0.4);
92 pie->Draw("rsc");
93}
#define c(i)
Definition RSha256.hxx:101
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
R__EXTERN TStyle * gStyle
Definition TStyle.h:433
Color * colors
Definition X3DBuffer.c:21
The Canvas class.
Definition TCanvas.h:23
static Int_t GetLinearGradient(Double_t angle, const std::vector< Int_t > &colors, const std::vector< Double_t > &positions={})
Static function: Returns the linear gradient color number corresponding to specified parameters.
Definition TColor.cxx:2115
static Int_t GetRadialGradient(Double_t r, const std::vector< Int_t > &colors, const std::vector< Double_t > &positions={})
Static function: Returns the radial gradient color number corresponding to specified parameters.
Definition TColor.cxx:2192
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition TColor.cxx:1839
The most important graphics class in the ROOT system.
Definition TPad.h:28
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:1325
void SetEntryRadiusOffset(Int_t, Double_t)
Set the distance, in the direction of the radius of the slice.
Definition TPie.cxx:1242
void SetRadius(Double_t)
Set the pie chart's radius' value.
Definition TPie.cxx:1341
void Draw(Option_t *option="l") override
Draw the pie chart.
Definition TPie.cxx:277
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition TStyle.h:339
Base class for several text objects.
Definition TText.h:22