Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
grad.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gl
3/// This macro demonstrates how to create and use linear gradients to fill
4/// a histogram or a pad.
5///
6/// \macro_image(nobatch)
7/// \macro_code
8///
9/// \authors Timur Pocheptsov, Sergey Linev
10
11//Includes for ACLiC (cling does not need them).
12#include "TColorGradient.h"
13#include "TCanvas.h"
14#include "TColor.h"
15#include "TStyle.h"
16#include "TError.h"
17#include "TH1F.h"
18
19//______________________________________________________________________
20void grad(bool use_gl = true)
21{
22 //Make sure we enabled OpenGL support in a canvas.
24
25 // Test if canvas supports OpenGL:
26 TCanvas *cnv = new TCanvas("grad", "gradient demo 1", 100, 100, 600, 600);
27 if (!cnv->UseGL() && !cnv->IsWeb())
28 ::Warning("grad", "This macro requires either OpenGL or Web canvas to correctly handle gradient colors");
29
30 // Create custom linear gradients.
31 // Linear gradient is defined by:
32 // 1) Direction in which gradient is changing (defined as angle in grad)
33 // 2) colors (to interpolate between them), at least two of them
34 // 3) alpha parameter for the colors (if not specified - used from TColor directly)
35 // 4) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
36
37 auto fcol1 = TColor::GetColor((Float_t) 0.25, 0.25, 0.25, 0.55); // special frame color 1
38 auto fcol2 = TColor::GetColor((Float_t) 1., 1., 1., 0.05); // special frame color 2
39
40 auto frameGradient = TColor::GetLinearGradient(0., {fcol1, fcol2, fcol2, fcol1}, {0., 0.2, 0.8, 1.});
41
42 // This gradient is a mixture of two standard colors.
43 auto padGradient = TColor::GetLinearGradient(0., {30, 38});
44
45 // Another gradient built from three standard colors.
46 auto histGradient = TColor::GetLinearGradient(45., {kYellow, kOrange, kRed});
47
48 // Example of radial gradient, for stats box works properly only in web canvas
49 // Here first argument is radius [0..1] and then list of colors
50 auto statGradient = TColor::GetRadialGradient(0.5, {kBlue, kGreen});
51
52
53 gStyle->SetStatColor(statGradient);
54
55 cnv->SetFillColor(padGradient);
56 cnv->SetFrameFillColor(frameGradient);
57
58 TH1F * const hist = new TH1F("a1", "b1", 20, -3., 3.);
59 hist->SetFillColor(histGradient);
60 hist->FillRandom("gaus", 10000);
61 hist->Draw();
62}
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:433
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:1494
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:2092
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:2169
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:1839
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition TH1.cxx:3515
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3062
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition TStyle.h:339
void SetStatColor(Color_t color=19)
Definition TStyle.h:387