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
20void gradients(bool gl = true)
21{
22 // Find free colour indices in the ROOT's palette for:
23 // 1. A radial gradient for TPie;
24 // 2. A linear gradient for TCanvas
25 // 3. A fully transparent fill color for a nested pad.
26
28
29 auto c = new TCanvas("cpie", "Gradient colours demo", 700, 700);
30 // Before we allocated any new colour or created any object:
31 if (!c->UseGL() && !c->IsWeb())
32 ::Warning("gradients", "This macro requires either OpenGL or Web canvas to correctly handle gradient colors");
33
34 // Linear gradient is defined by: 1) colors (to interpolate between them),
35 // 2) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
36 // 3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
37 // and this rect is either: bounding rect of your polygon/object to fill
38 //(gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
39 // or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
40 // kObjectBoundingMode is the default one.
41
42 // Draw a text in the canvas (the object above the text will be
43 // semi-transparent):
44 auto t = new TText(0.05, 0.7, "Can you see the text?");
45 t->Draw();
46
47 // We create a nested pad on top to render a TPie in,
48 // this way we still have a text (below) + TPie with
49 // a fancy colour on top.
50 auto pad = new TPad("p", "p", 0., 0., 1., 1.);
51
52 // TPad itself is fully transparent:
53 auto transparentFill = TColor::GetColor((Float_t)1., 1., 1., 0.);
54 pad->SetFillColor(transparentFill);
55 // Add our pad into the canvas:
56 pad->Draw();
57 pad->cd();
58
59 // Radial gradient fill for a TPie object:
60 auto col3 = TColor::GetColor((Float_t)1., 0.8, 0., 1.); /*opaque orange at the start:*/
61 auto col4 = TColor::GetColor((Float_t)1., 0.2, 0., 0.65); /*transparent red at the end:*/
62
63 //'Simple' radial gradient with radius 0.4
65
66 // Linear gradient fill (with an axis angle == 45):
67 auto col1 = TColor::GetColor((Float_t)0.2, 0.2, 0.2); /*gray*/
68 auto col2 = TColor::GetColor((Float_t)0.8, 1., 0.9); /*pale green*/
69 auto linearFill = TColor::GetLinearGradient(45., {col1, col2}); // 45 degrees:
70
71 // Set as a background color in the canvas:
72 c->SetFillColor(linearFill);
73
74 const UInt_t nSlices = 5;
75 // Values for a TPie (non-const, that's how TPie's ctor is declared):
76 Double_t values[nSlices] = {0.8, 1.2, 1.2, 0.8, 1.};
78
79 TPie *const pie = new TPie("pie", "TPie:", nSlices, values, colors);
80 // One slice is slightly shifted:
81 pie->SetEntryRadiusOffset(2, 0.05);
82 // Move labels to the center (to fit the pad's space):
83 pie->SetLabelsOffset(-0.08);
84 //
85 pie->SetRadius(0.4);
86 pie->Draw("rsc");
87}
#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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:436
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:2214
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:2291
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:1924
The most important graphics class in the ROOT system.
Definition TPad.h:28
Draw a Pie Chart,.
Definition TPie.h:23
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition TStyle.h:341
Base class for several text objects.
Definition TText.h:22