Logo ROOT  
Reference Guide
gradients.C File Reference

Detailed Description

This macro requires OS X and ROOT compiled with –enable-cocoa to run.

Features:

  1. Radial and linear gradients
  2. Transparent/semitransparent colours.
  3. Shadows.
//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");
}
Author
Timur Pocheptsov

Definition in file gradients.C.

c
#define c(i)
Definition: RSha256.hxx:101
TStyle::SetCanvasPreferGL
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition: TStyle.h:325
TPie.h
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
ROOT::GLTutorials::FindFreeCustomColorIndices
unsigned FindFreeCustomColorIndices(T(&indices)[N])
Definition: customcolorgl.h:40
TColorGradient::kPadMode
@ kPadMode
Definition: TColorGradient.h:45
TPad
The most important graphics class in the ROOT system.
Definition: TPad.h:26
TPie::SetRadius
void SetRadius(Double_t)
Set the pie chart's radius' value.
Definition: TPie.cxx:1369
colors
Color * colors
Definition: X3DBuffer.c:21
TPie::SetLabelsOffset
void SetLabelsOffset(Float_t)
Set the distance between the label end the external line of the TPie.
Definition: TPie.cxx:1353
TColorGradient.h
TStyle.h
Int_t
int Int_t
Definition: RtypesCore.h:45
TRadialGradient
Define a radial color gradient.
Definition: TColorGradient.h:128
TPie
Draw a Pie Chart,.
Definition: TPie.h:23
TColorGradient::Point
Definition: TColorGradient.h:49
TCanvas.h
Color_t
short Color_t
Definition: RtypesCore.h:83
TPie::Draw
virtual void Draw(Option_t *option="l")
Draw the pie chart.
Definition: TPie.cxx:276
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TColor
The color creation and management class.
Definition: TColor.h:19
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TLinearGradient
Define a linear color gradient.
Definition: TColorGradient.h:99
TLinearGradient::SetStartEnd
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
Definition: TColorGradient.cxx:218
TObject::Draw
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
TText
Base class for several text objects.
Definition: TText.h:22
Double_t
double Double_t
Definition: RtypesCore.h:59
TRadialGradient::SetRadialGradient
void SetRadialGradient(const Point &center, Double_t radius)
Set radial gradient.
Definition: TColorGradient.cxx:324
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TPad::cd
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:594
TAttFill::SetFillColor
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
TColorGradient::SetCoordinateMode
void SetCoordinateMode(ECoordinateMode mode)
Set coordinate mode.
Definition: TColorGradient.cxx:127
TPie::SetEntryRadiusOffset
void SetEntryRadiusOffset(Int_t, Double_t)
Set the distance, in the direction of the radius of the slice.
Definition: TPie.cxx:1270
TText.h
customcolorgl.h
Error
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
TError.h
TPad::Draw
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1285