Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
grad2.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gl
3/// Gradient fill with transparency and the "SAME" option.
4/// To use this macro you need OpenGL enabled in pad:
5/// either set OpenGL.CanvasPreferGL to 1 in $ROOTSYS/etc/system.rootrc;
6/// or call `gStyle->SetCanvasPreferGL(kTRUE);` before canvas created.
7///
8/// \macro_image(nobatch)
9/// \macro_code
10///
11/// \authors Timur Pocheptsov, Sergey Linev
12
13// Includes for ACLiC (cling does not need them).
14#include "TColorGradient.h"
15#include "TCanvas.h"
16#include "TError.h"
17#include "TColor.h"
18#include "TStyle.h"
19#include "TH1F.h"
20
21void grad2(bool gl = true)
22{
23 // Make sure canvas supports OpenGL.
25
26 // 2. Check that we have a canvas with an OpenGL support.
27 auto cnv = new TCanvas("grad2", "gradient demo 2", 100, 100, 800, 600);
28 if (!cnv->UseGL() && !cnv->IsWeb())
29 ::Warning("grad2", "This macro requires either OpenGL or Web canvas to correctly handle gradient colors");
30
31 // 3. Custom colors:
32 // a) Custom semi-transparent red.
33 auto customRed = TColor::GetColor((Float_t)1., 0., 0., 0.5);
34
35 // Custom semi-transparent green.
36 auto customGreen = TColor::GetColor((Float_t)0., 1., 0., 0.5);
37
38 // 4. Linear gradient colors
39 // b) Gradient (from our semi-transparent red to ROOT's kOrange).
40 // Linear gradient is defined by: 1) angle in grad
41 // 2) colors (to interpolate between them),
42 // If necessary, TLinearGradient object can be retrieved and modified later
43
44 auto grad1 = TColor::GetLinearGradient(90., {customRed, kOrange});
45
46 // Vertical gradient fill.
47 auto grad2 = TColor::GetLinearGradient(90., {customGreen, kBlue});
48
49 auto hist = new TH1F("a2", "b2", 10, -2., 3.);
50 auto hist2 = new TH1F("c3", "d3", 10, -3., 3.);
51 hist->FillRandom("landau", 100000);
52 hist2->FillRandom("gaus", 100000);
53
54 hist->SetFillColor(grad1);
55 hist2->SetFillColor(grad2);
56
57 hist2->Draw();
58 hist->Draw("SAME");
59}
float Float_t
Definition RtypesCore.h:57
@ kOrange
Definition Rtypes.h:67
@ kBlue
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
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:2211
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
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition TStyle.h:341