Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
gradients.C File Reference

Detailed Description

Features:

  1. Radial and linear gradients
  2. Transparent/semitransparent colours.
//Includes for ACLiC:
#include "TColorGradient.h"
#include "TCanvas.h"
#include "TError.h"
#include "TStyle.h"
#include "TText.h"
#include "TPie.h"
void gradients(bool gl = true)
{
//Find free colour indices in the ROOT's palette for:
//1. A radial gradient for TPie;
//2. A linear gradient for TCanvas
//3. A fully transparent fill color for a nested pad.
auto c = new TCanvas("cpie","Gradient colours demo", 700, 700);
//Before we allocated any new colour or created any object:
if (!c->UseGL() && !c->IsWeb())
::Warning("gradients", "This macro requires either OpenGL or Web canvas to correctly handle gradient colors");
//Linear gradient is defined by: 1) colors (to interpolate between them),
//2) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
//3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
//and this rect is either: bounding rect of your polygon/object to fill
//(gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
//or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
//kObjectBoundingMode is the default one.
//Draw a text in the canvas (the object above the text will be
//semi-transparent):
auto t = new TText(0.05, 0.7, "Can you see the text?");
t->Draw();
//We create a nested pad on top to render a TPie in,
//this way we still have a text (below) + TPie with
//a fancy colour on top.
auto pad = new TPad("p", "p", 0., 0., 1., 1.);
//TPad itself is fully transparent:
auto transparentFill = TColor::GetColor((Float_t) 1., 1., 1., 0.);
pad->SetFillColor(transparentFill);
//Add our pad into the canvas:
pad->Draw();
pad->cd();
//Radial gradient fill for a TPie object:
auto col3 = TColor::GetColor((Float_t) 1., 0.8, 0., 1.); /*opaque orange at the start:*/
auto col4 = TColor::GetColor((Float_t) 1., 0.2, 0., 0.65); /*transparent red at the end:*/
//'Simple' radial gradient with radius 0.4
auto radialFill = TColor::GetRadialGradient(0.4, {col3, col4});
//Linear gradient fill (with an axis angle == 45):
auto col1 = TColor::GetColor((Float_t) 0.2, 0.2, 0.2); /*gray*/
auto col2 = TColor::GetColor((Float_t) 0.8, 1., 0.9); /*pale green*/
auto linearFill = TColor::GetLinearGradient(45., {col1, col2}); //45 degrees:
//Set as a background color in the canvas:
c->SetFillColor(linearFill);
const UInt_t nSlices = 5;
//Values for a TPie (non-const, that's how TPie's ctor is declared):
Double_t values[nSlices] = {0.8, 1.2, 1.2, 0.8, 1.};
Int_t colors[nSlices] = {radialFill, radialFill, radialFill,
radialFill, radialFill};
TPie * const pie = new TPie("pie", "TPie:", nSlices, values, colors);
//One slice is slightly shifted:
pie->SetEntryRadiusOffset(2, 0.05);
//Move labels to the center (to fit the pad's space):
pie->SetLabelsOffset(-0.08);
//
pie->SetRadius(0.4);
pie->Draw("rsc");
}
#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:2092
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:2169
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
Authors
Timur Pocheptsov, Sergey Linev

Definition in file gradients.C.