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

Detailed Description

This macro requires OpenGL, you can:

  1. call gStyle->SetCanvasPreferGL(kTRUE) before a canvas is created or
  2. set OpenGL.CanvasPreferGL to 1 in a $ROOTSYS/etc/system.rootrc.

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"
//Cocoa aux. functions.
#include "customcolorgl.h"
void gradients()
{
//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.
Color_t colorIndices[3] = {};
::Error("gradients", "failed to create new custom colors");
return;
}
//Better names:
const Color_t &radialFill = colorIndices[0];
const Color_t &linearFill = colorIndices[1];
const Color_t &transparentFill = colorIndices[2];
TCanvas * const c = new TCanvas("cpie","Gradient colours demo", 700, 700);
//Before we allocated any new colour or created any object:
if (!c->UseGL()) {
::Error("gradients", "This macro requires OpenGL");
delete c;
return;
}
//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.
//Colour positions in the gradient's palette (here I place colors at the
//ends of 0-1):
const Double_t locations[] = {0., 1.};
//Linear gradient fill (with an axis angle == 45):
const Double_t rgbaData1[] = {0.2, 0.2, 0.2, 1.,/*gray*/
0.8, 1., 0.9, 1. /*pale green*/};
TLinearGradient * const gradientFill1 = new TLinearGradient(linearFill, 2, locations, rgbaData1);
//45 degrees:
gradientFill1->SetStartEnd(TColorGradient::Point(0., 0.), TColorGradient::Point(1., 1.));
//Set as a background color in the canvas:
c->SetFillColor(linearFill);
//Draw a text in the canvas (the object above the text will be
//semi-transparent):
TText * const 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.
TPad * const pad = new TPad("p", "p", 0., 0., 1., 1.);
//TPad itself is fully transparent:
new TColor(transparentFill, 1., 1., 1., "transparent_fill_color", 0.);
pad->SetFillColor(transparentFill);
//Add our pad into the canvas:
pad->Draw();
pad->cd();
//Radial gradient fill for a TPie object:
const Double_t rgbaData2[] = {/*opaque orange at the start:*/1., 0.8, 0., 1.,
/*transparent red at the end:*/1., 0.2, 0., 0.65};
TRadialGradient * const gradientFill2 = new TRadialGradient(radialFill, 2,
locations, rgbaData2);
//Parameters for a gradient fill:
//the gradient is 'pad-related' - we calculate everything in a pad's
//space and consider it as a NDC (so pad's rect is (0,0), (1,1)).
//'Simple' radial gradient (center and radius):
gradientFill2->SetRadialGradient(TColorGradient::Point(0.5, 0.5), 0.4);
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
short Color_t
Definition RtypesCore.h:92
unsigned int UInt_t
Definition RtypesCore.h:46
double Double_t
Definition RtypesCore.h:59
constexpr 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:197
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:274
The most important graphics class in the ROOT system.
Definition TPad.h:28
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:1268
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:1324
void SetEntryRadiusOffset(Int_t, Double_t)
Set the distance, in the direction of the radius of the slice.
Definition TPie.cxx:1241
void SetRadius(Double_t)
Set the pie chart's radius' value.
Definition TPie.cxx:1340
void Draw(Option_t *option="l") override
Draw the pie chart.
Definition TPie.cxx:276
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])
Author
Timur Pocheptsov

Definition in file gradients.C.