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