Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
transp_text.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_gl
3 /// This macro is based on labels1.C by Rene Brun.
4 /// Updated by Timur Pocheptsov to use transparent text.
5 /// The macro requires OpenGL - either enable by
6 /// `gStyle->SetCanvasPreferGL(kTRUE);` or by setting
7 /// `OpenGL.CanvasPreferGL` to 1 in `$ROOTSYS/etc/system.rootrc`.
8 ///
9 /// \macro_image(nobatch)
10 /// \macro_code
11 ///
12 /// \author Timur Pocheptsov
13 
14 //Includes for ACLiC (cling does not need them).
15 #include "TPaveText.h"
16 #include "TCanvas.h"
17 #include "TRandom.h"
18 #include "TError.h"
19 #include "TColor.h"
20 #include "TStyle.h"
21 #include "TH1F.h"
22 
23 //Aux. functions for tutorials/gl.
24 #include "customcolorgl.h"
25 
26 void transp_text()
27 {
28  //1. Try to 'allocate' free indices for our custom colors -
29  //we can use hard-coded indices like 1001, 1002, 1003 ... but
30  //I prefer to find free indices in a ROOT's color table
31  //to avoid possible conflicts with other tutorials.
32  Int_t indices[2] = {};
33  if (ROOT::GLTutorials::FindFreeCustomColorIndices(indices) != 2) {
34  Error("transp_text", "failed to create new custom colors");
35  return;
36  }
37 
38  //2. Create special transparent colors for both pavetext fill color and text color.
39  const Int_t grayColorIndex = indices[0], blackColorIndex = indices[1];
40  new TColor(grayColorIndex, 0.8, 0.8, 0.8, "transparent_gray", 0.85);
41  new TColor(blackColorIndex, 0., 0., 0., "transparent_black", 0.5);
42 
43  //3. Create a TCanvas.
44  gStyle->SetCanvasPreferGL(kTRUE);
45 
46  TCanvas * const c1 = new TCanvas("transparent text","transparent text demo", 10, 10, 900, 500);
47  if (!c1->UseGL())
48  ::Warning("transp_text", "to use this macro you need OpenGL");
49 
50  c1->SetGrid();
51  c1->SetBottomMargin(0.15);
52 
53  const Int_t nx = 20;
54  const char *people[nx] = {"Jean","Pierre","Marie","Odile",
55  "Sebastien","Fons","Rene","Nicolas","Xavier","Greg",
56  "Bjarne","Anton","Otto","Eddy","Peter","Pasha",
57  "Philippe","Suzanne","Jeff","Valery"};
58 
59  TH1F * const h = new TH1F("h4", "test", nx, 0, nx);
60 
61  h->SetFillColor(38);
62  for (Int_t i = 0; i < 5000; ++i)
63  h->Fill(gRandom->Gaus(0.5 * nx, 0.2 * nx));
64 
65  h->SetStats(0);
66  for (Int_t i = 1; i <= nx; ++i)
67  h->GetXaxis()->SetBinLabel(i, people[i - 1]);
68 
69  h->Draw();
70 
71  TPaveText * const pt = new TPaveText(0.3, 0.3, 0.98, 0.98, "brNDC");
72  //Transparent 'rectangle' with transparent text.
73  pt->SetFillColor(grayColorIndex);
74  pt->SetTextColor(blackColorIndex);
75 
76  pt->SetTextSize(0.5);
77  pt->SetTextAlign(12);
78 
79  pt->AddText("Hello");
80  pt->Draw();
81 }