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.
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
51
53
54 cnv->SetFillColor(padGradient);
55 cnv->SetFrameFillColor(frameGradient);
56
57 TH1F *const hist = new TH1F("a1", "b1", 20, -3., 3.);
59 hist->FillRandom("gaus", 10000);
60 hist->Draw();
61}
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:436
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:341
void SetStatColor(Color_t color=19)
Definition TStyle.h:390