Logo ROOT  
Reference Guide
radialgradients.C File Reference

Detailed Description

This tutorial demonstrates how to use radial gradients, custom colors, transparency.

Requires OpenGL: either set OpenGL.CanvasPreferGL to 1 in the $ROOTSYS/etc/system.rootrc, or use gStyle->SetCanvasPreferGL(kTRUE).

//Includes for ACLiC:
#include <cassert>
#include <cstdlib>
#include "TColorGradient.h"
#include "TEllipse.h"
#include "TRandom.h"
#include "TCanvas.h"
#include "TStyle.h"
#include "TError.h"
//Aux. functions.
#include "customcolorgl.h"
namespace {
//Just some colors (rgba) to build our
//fancy gradients from.
const Double_t basicColors[][4] =
{
{1., 0., 0., 1.},
{1., 0.3, 0., 1.},
{0., 0., 1., 1.},
{1., 1., 0., 1.},
{1., 0., 1., 1.},
{0., 1., 1., 1.},
{0., 1., 0., 1.},
{0., 0.5, 0., 1.},
//transparent colors:
{1., 0., 0., 0.5},
{1., 0.3, 0., 0.5},
{0., 0., 1., 0.5},
{1., 1., 0., 0.5},
{1., 0., 1., 0.5},
{0., 1., 1., 0.5},
{0., 1., 0., 0.5},
{0., 0.5, 0., 0.5},
//and even more transparent:
{1., 0., 0., 0.2},
{1., 0.3, 0., 0.2},
{0., 0., 1., 0.2},
{1., 1., 0., 0.2},
{1., 0., 1., 0.2},
{0., 1., 1., 0.2},
{0., 1., 0., 0.2},
{0., 0.5, 0., 0.2}
};
const unsigned nBasicColors = sizeof basicColors / sizeof basicColors[0];
//______________________________________________________________________
Color_t CreateRandomGradientFill()
{
const Double_t * const fromRGBA = basicColors[(std::rand() % (nBasicColors / 2))];
//With odd number of colors the last one is never selected :)
const Double_t * const toRGBA = basicColors[nBasicColors / 2 + (std::rand() % (nBasicColors / 2))];
const Double_t locations[] = {0., 1.};
const Double_t rgbas[] = {fromRGBA[0], fromRGBA[1], fromRGBA[2], fromRGBA[3],
toRGBA[0], toRGBA[1], toRGBA[2], toRGBA[3]};
Color_t idx[1] = {};
return -1;
TRadialGradient * const grad = new TRadialGradient(idx[0], 2, locations, rgbas);
//
return idx[0];
}
//______________________________________________________________________
bool add_ellipse(const Double_t xC, const Double_t yC, const Double_t r)
{
assert(gPad != nullptr && "add_ellipse, no pad to add ellipse");
const Color_t newColor = CreateRandomGradientFill();
if (newColor == -1) {
::Error("add_ellipse", "failed to find a new color index for a gradient fill");
return false;
}
TEllipse * const newEllipse = new TEllipse(xC, yC, r, r);
newEllipse->SetFillColor(newColor);
newEllipse->Draw();
return true;
}
}
//______________________________________________________________________
void radialgradients()
{
gRandom->SetSeed(4357);//;)
TCanvas * const cnv = new TCanvas("radial gradients", "radial gradients", 800, 800);
if (!cnv->UseGL()) {
::Error("radialgradients", "this demo OpenGL");
delete cnv;
return;
}
for (unsigned i = 0; i < 100; ++i)
if (!add_ellipse(gRandom->Rndm(), gRandom->Rndm(), 0.5 * gRandom->Rndm()))
break;
cnv->Modified();
cnv->Update();
}
Author
Timur Pocheptsov

Definition in file radialgradients.C.

TStyle::SetCanvasPreferGL
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition: TStyle.h:325
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
ROOT::GLTutorials::FindFreeCustomColorIndices
unsigned FindFreeCustomColorIndices(T(&indices)[N])
Definition: customcolorgl.h:40
r
ROOT::R::TRInterface & r
Definition: Object.C:4
TRandom.h
TColorGradient.h
TEllipse
Draw Ellipses.
Definition: TEllipse.h:23
TStyle.h
TRadialGradient
Define a radial color gradient.
Definition: TColorGradient.h:128
TColorGradient::Point
Definition: TColorGradient.h:49
TCanvas::UseGL
Bool_t UseGL() const
Definition: TCanvas.h:223
TCanvas.h
Color_t
short Color_t
Definition: RtypesCore.h:83
TEllipse::Draw
virtual void Draw(Option_t *option="")
Draw this ellipse with its current attributes.
Definition: TEllipse.cxx:168
TPad::Modified
void Modified(Bool_t flag=1)
Definition: TPad.h:414
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
gRandom
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
TRandom::SetSeed
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:608
TRandom::Rndm
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:552
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
TAttFill::SetFillColor
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
TCanvas::Update
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2504
TEllipse.h
gPad
#define gPad
Definition: TVirtualPad.h:287
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