Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
transparentpad.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gl
3/// This macro demonstrates semi-transparent pads.
4///
5/// One uses fill styles between 4000 and 4100 to configure objects transparency
6/// On OpenGL or Mac/Cocoa or Web-based canvas pads will be drawn with transparent colors.
7/// On X11 pixmap transformation performed to partially emulate pads transparency
8/// Also demonstrated usage of transparent fill styles for stats box
9///
10/// \macro_image(nobatch)
11/// \macro_code
12///
13/// \authors Timur Pocheptsov, Sergey Linev
14
15#include "TCanvas.h"
16#include "TStyle.h"
17#include "TError.h"
18#include "TColor.h"
19#include "TH1F.h"
20
21void transparentpad(bool gl = true)
22{
24
25 // prevent filling of TFrame with solid color
27
28 // 1. Create canvas and check if it support transparent colors
29 auto c1 = new TCanvas("c1", "transparent pad demo", 10, 10, 900, 500);
30 if (!c1->UseGL() && !c1->IsWeb() && !gVirtualX->InheritsFrom("TGCocoa"))
31 ::Warning("transparentpad",
32 "To have real transparency in a canvas graphics, you need either OpenGL or Mac/Cocoa or Web rendering enabled");
33
34 // 2. Some arbitrary histograms.
35 auto h1 = new TH1F("TH1F 1", "TH1F 1", 100, -1.5, 1.5);
36 h1->FillRandom("gaus");
37
38 auto h2 = new TH1F("TH1F 2", "TH1F 2", 100, -1.5, 0.);
39 h2->FillRandom("gaus");
40
41 auto h3 = new TH1F("TH1F 3", "TH1F 3", 100, 0.5, 2.);
42 h3->FillRandom("landau");
43
44 // 3. Now overlapping transparent pads.
45 auto pad1 = new TPad("transparent pad 1", "transparent pad 1", 0.1, 0.1, 0.7, 0.7);
46 pad1->SetFillColor(kPink);
47 pad1->SetFillStyle(4040); // transparent pink, here's the magic!
48 c1->Add(pad1);
49 pad1->Add(h1, "lego2");
50
51 auto pad2 = new TPad("transparent pad 2", "transparent pad 2", 0.2, 0.2, 0.8, 0.8);
52 pad2->SetFillColor(kGreen);
53 pad2->SetFillStyle(4035); // transparent green, here's the magic!
54 c1->Add(pad2);
55 pad2->Add(h2);
56
57 auto pad3 = new TPad("transparent pad 3", "transparent pad 3", 0.3, 0.3, 0.9, 0.9);
58 pad3->SetFillColor(kBlue);
59 pad3->SetFillStyle(4030); // transparent blue, here's the magic!
60 c1->Add(pad3);
61 pad3->Add(h3);
62
63 c1->Update();
64
65 auto stats2 = dynamic_cast<TPaveStats *>(h2->FindObject("stats"));
66 if (stats2) {
67 stats2->SetFillColor(kYellow);
68 stats2->SetFillStyle(4050); // semi-transparent stats box, only with GL
69 }
70
71 auto stats3 = dynamic_cast<TPaveStats *>(h3->FindObject("stats"));
72 if (stats3) {
73 stats3->SetFillColor(kYellow);
74 stats3->SetFillStyle(4050); // semi-transparent stats box, only with GL
75 }
76
77 // SVG or PDF or PS image always support transparent colors
78 // c1->SaveAs("transparentpad.svg");
79
80 c1->Modified();
81}
@ kPink
Definition Rtypes.h:68
@ kGreen
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:67
@ kYellow
Definition Rtypes.h:67
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:252
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
#define gVirtualX
Definition TVirtualX.h:367
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3575
The most important graphics class in the ROOT system.
Definition TPad.h:28
The histogram statistics painter class.
Definition TPaveStats.h:18
void SetFrameFillStyle(Style_t styl=0)
Definition TStyle.h:378
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition TStyle.h:345
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5