Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches

Detailed Description

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

Requires ROOT built for OS X with –enable-cocoa.

// Includes for ACLiC:
#include "TColorGradient.h"
#include "TEllipse.h"
#include "TRandom.h"
#include "TCanvas.h"
#include "TStyle.h"
#include "TError.h"
//______________________________________________________________________
Color_t CreateRandomGradientFill()
{
std::vector<Int_t> colors;
for (int n = 0; n < 2; ++n)
colors.emplace_back(gRandom->Integer(10) + 2);
auto indx = TColor::GetRadialGradient(0.5, colors);
// example how to modify gradient
auto gradient = dynamic_cast<TRadialGradient *>(gROOT->GetColor(indx));
if (gradient) {
// change center and radius
gradient->SetRadialGradient({0.3, 0.3}, 0.7);
// change alpha parameter for the colors
gradient->SetColorAlpha(0, 0.2 + gRandom->Rndm() * 0.8);
gradient->SetColorAlpha(1, 0.2 + gRandom->Rndm() * 0.8);
} else {
::Error("CreateRandomGradientFill", "failed to find new gradient color with index %d", indx);
}
return indx;
}
//______________________________________________________________________
bool add_ellipse(const Double_t xC, const Double_t yC, const Double_t r)
{
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(bool gl = true)
{
gRandom->SetSeed(4357);
gStyle->SetCanvasPreferGL(gl);
auto cnv = new TCanvas("radialgradients", "radial gradients", 800, 800);
if (!cnv->UseGL() && !cnv->IsWeb())
::Warning("radialgradients",
"This macro requires either OpenGL or Web canvas to correctly handle gradient colors");
for (unsigned i = 0; i < 100; ++i)
add_ellipse(gRandom->Rndm(), gRandom->Rndm(), 0.5 * gRandom->Rndm());
}
ROOT::R::TRInterface & r
Definition Object.C:4
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
short Color_t
Color number (short).
Definition RtypesCore.h:99
Error("WriteTObject","The current directory (%s) is not associated with a file. The object (%s) has not been written.", GetName(), objname)
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
#define gROOT
Definition TROOT.h:417
externTRandom * gRandom
Definition TRandom.h:62
externTStyle * gStyle
Definition TStyle.h:442
Color * colors
Definition X3DBuffer.c:21
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:40
The Canvas class.
Definition TCanvas.h:23
static Int_t GetRadialGradient(Double_t r, const std::vector< Int_t > &colors, const std::vector< Double_t > &positions={})
void Draw(Option_t *option="") override
Default Draw method for all objects.
const Int_t n
Definition legend1.C:16
Author
Timur Pocheptsov

Definition in file radialgradients.C.