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

Detailed Description

This macro shows how to create and use linear gradients to fill a histogram or a pad.

Requires OS X and ROOT configured with –enable-cocoa.

// Includes for ACLiC (cling does not need them).
#include "TColorGradient.h"
#include "TCanvas.h"
#include "TColor.h"
#include "TStyle.h"
#include "TError.h"
#include "TH1F.h"
//______________________________________________________________________
void grad(bool use_gl = true)
{
// Make sure we enabled OpenGL support in a canvas.
// Test if canvas supports OpenGL:
TCanvas *cnv = new TCanvas("grad", "gradient demo 1", 100, 100, 600, 600);
if (!cnv->UseGL() && !cnv->IsWeb())
::Warning("grad", "This macro requires either OpenGL or Web canvas to correctly handle gradient colors");
// Create custom linear gradients.
// Linear gradient is defined by:
// 1) Direction in which gradient is changing (defined as angle in grad)
// 2) colors (to interpolate between them), at least two of them
// 3) alpha parameter for the colors (if not specified - used from TColor directly)
// 4) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
auto fcol1 = TColor::GetColor((Float_t)0.25, 0.25, 0.25, 0.55); // special frame color 1
auto fcol2 = TColor::GetColor((Float_t)1., 1., 1., 0.05); // special frame color 2
auto frameGradient = TColor::GetLinearGradient(0., {fcol1, fcol2, fcol2, fcol1}, {0., 0.2, 0.8, 1.});
// This gradient is a mixture of two standard colors.
auto padGradient = TColor::GetLinearGradient(0., {30, 38});
// Another gradient built from three standard colors.
auto histGradient = TColor::GetLinearGradient(45., {kYellow, kOrange, kRed});
// Example of radial gradient, for stats box works properly only in web canvas
// Here first argument is radius [0..1] and then list of colors
auto statGradient = TColor::GetRadialGradient(0.5, {kBlue, kGreen});
gStyle->SetStatColor(statGradient);
cnv->SetFillColor(padGradient);
cnv->SetFrameFillColor(frameGradient);
TH1F *const hist = new TH1F("a1", "b1", 20, -3., 3.);
hist->SetFillColor(histGradient);
hist->FillRandom("gaus", 10000);
hist->Draw();
}
float Float_t
Definition RtypesCore.h:57
@ kRed
Definition Rtypes.h:66
@ kOrange
Definition Rtypes.h:67
@ kGreen
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
@ kYellow
Definition Rtypes.h:66
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:436
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
void SetFrameFillColor(Color_t color=1)
Definition TAttPad.h:73
The Canvas class.
Definition TCanvas.h:23
Bool_t IsWeb() const override
Is web canvas.
Definition TCanvas.cxx:1496
Bool_t UseGL() const
Definition TCanvas.h:228
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:2211
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:2288
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:1921
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:623
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3530
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3068
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition TStyle.h:341
void SetStatColor(Color_t color=19)
Definition TStyle.h:390
Author
Timur Pocheptsov

Definition in file grad.C.