Logo ROOT   6.16/01
Reference Guide
transp_text.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_cocoa
3/// This macro is based on labels1.C by Rene Brun.
4/// Updated by Timur Pocheptsov to use transparent text (12/10/2012).
5/// Requires OS X and ROOT configured with --enable-cocoa.
6///
7/// \macro_code
8///
9/// \author Timur Pocheptsov
10
11//Includes for ACLiC (cling does not need them).
12#include "TVirtualX.h"
13#include "TPaveText.h"
14#include "TCanvas.h"
15#include "TRandom.h"
16#include "TError.h"
17#include "TColor.h"
18#include "TH1F.h"
19
20//Aux. functions for tutorials/cocoa.
21#include "customcolor.h"
22
23void transp_text()
24{
25 //1. Try to 'allocate' free indices for our custom colors -
26 //we can use hard-coded indices like 1001, 1002, 1003 ... but
27 //I prefer to find free indices in a ROOT's color table
28 //to avoid possible conflicts with other tutorials.
29 Color_t indices[2] = {};
31 ::Error("transp_text", "failed to create new custom colors");
32 return;
33 }
34
35 //2. Create special transparent colors for both pavetext fill color and text color.
36 const Color_t grayColorIndex = indices[0], blackColorIndex = indices[1];
37 new TColor(grayColorIndex, 0.8, 0.8, 0.8, "transparent_gray", 0.85);
38 new TColor(blackColorIndex, 0., 0., 0., "transparent_black", 0.5);
39
40 //3. Create a TCanvas first to force gVirtualX initialization.
41 TCanvas * const c1 = new TCanvas("transparent text","transparent text demo", 10, 10, 900, 500);
42 //We can check gVirtualX (its type):
43 if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
44 ::Warning("transt_text", "You can see the transparency ONLY in a pdf or png output (\"File\"->\"Save As\" ->...)\n"
45 "To have transparency in a canvas graphics, you need OS X version with cocoa enabled");
46 }
47
48 c1->SetGrid();
49 c1->SetBottomMargin(0.15);
50
51 const Int_t nx = 20;
52 const char *people[nx] = {"Jean","Pierre","Marie","Odile",
53 "Sebastien","Fons","Rene","Nicolas","Xavier","Greg",
54 "Bjarne","Anton","Otto","Eddy","Peter","Pasha",
55 "Philippe","Suzanne","Jeff","Valery"};
56
57 TH1F * const h = new TH1F("h4", "test", nx, 0, nx);
58
59 h->SetFillColor(38);
60 for (Int_t i = 0; i < 5000; ++i)
61 h->Fill(gRandom->Gaus(0.5 * nx, 0.2 * nx));
62
63 h->SetStats(0);
64 for (Int_t i = 1; i <= nx; ++i)
65 h->GetXaxis()->SetBinLabel(i, people[i - 1]);
66
67 h->Draw();
68
69 TPaveText * const pt = new TPaveText(0.3, 0.3, 0.98, 0.98, "brNDC");
70 //Transparent 'rectangle' with transparent text.
71 pt->SetFillColor(grayColorIndex);
72 pt->SetTextColor(blackColorIndex);
73
74 pt->SetTextSize(0.5);
75 pt->SetTextAlign(12);
76
77 pt->AddText("Hello");
78 pt->Draw();
79}
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:41
short Color_t
Definition: RtypesCore.h:79
void Error(const char *location, const char *msgfmt,...)
void Warning(const char *location, const char *msgfmt,...)
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
#define gVirtualX
Definition: TVirtualX.h:345
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition: TAttText.h:41
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:43
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:46
The Canvas class.
Definition: TCanvas.h:31
The color creation and management class.
Definition: TColor.h:19
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:21
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
Definition: TPaveText.cxx:182
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:233
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:256
TPaveText * pt
return c1
Definition: legend1.C:41
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition: customcolor.h:38