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.
// 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
cnv->SetFillColor(padGradient);
cnv->SetFrameFillColor(frameGradient);
TH1F *const hist = new TH1F("a1", "b1", 20, -3., 3.);
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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:442
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:38
The Canvas class.
Definition TCanvas.h:23
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:2214
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:2291
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:1924
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:645
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3499
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3037
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition TStyle.h:345
void SetStatColor(Color_t color=19)
Definition TStyle.h:394
Author
Timur Pocheptsov

Definition in file grad.C.