Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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",
45 "You can see the transparency ONLY in a pdf or png output (\"File\"->\"Save As\" ->...)\n"
46 "To have transparency in a canvas graphics, you need OS X version with cocoa enabled");
47 }
48
49 c1->SetGrid();
50 c1->SetBottomMargin(0.15);
51
52 const Int_t nx = 20;
53 const char *people[nx] = {"Jean", "Pierre", "Marie", "Odile", "Sebastien", "Fons", "Rene",
54 "Nicolas", "Xavier", "Greg", "Bjarne", "Anton", "Otto", "Eddy",
55 "Peter", "Pasha", "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(false);
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:45
short Color_t
Definition RtypesCore.h:85
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
#define gVirtualX
Definition TVirtualX.h:337
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:38
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:44
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:46
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:49
The Canvas class.
Definition TCanvas.h:23
The color creation and management class.
Definition TColor.h:22
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:634
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.
void Draw(Option_t *option="") override
Draw this pavetext with its current attributes.
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:275
TPaveText * pt
return c1
Definition legend1.C:41
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition customcolor.h:38