Logo ROOT  
Reference Guide
transp.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_cocoa
3 /// This demo shows how to use transparency.
4 /// On MacOS X you can see the transparency in a canvas,
5 /// you can save canvas contents as pdf/png
6 /// (and thus you'll have an image with transparency on every platform).
7 ///
8 /// \macro_code
9 ///
10 /// \author Timur Pocheptsov
11 
12 //Includes for ACLiC (cling does not need them).
13 #include "TVirtualX.h"
14 #include "TCanvas.h"
15 #include "TColor.h"
16 #include "TError.h"
17 #include "TH1F.h"
18 
19 //Aux. functions for tutorials/cocoa.
20 #include "customcolor.h"
21 
22 void transp()
23 {
24  //1. Try to find free indices for our custom colors.
25  //We can use hard-coded indices like 1001, 1002, 1003, ... but
26  //I prefer to find free indices in a ROOT's color table
27  //to avoid possible conflicts with other tutorials.
28  Color_t indices[2] = {};
30  ::Error("transp", "failed to create new custom colors");
31  return;
32  }
33 
34  //2. Now that we have indices, create our custom colors.
35  const Color_t redIndex = indices[0], greeIndex = indices[1];
36 
37  new TColor(redIndex, 1., 0., 0., "red", 0.85);
38  new TColor(greeIndex, 0., 1., 0., "green", 0.5);
39 
40  //3. Test that back-end is TGCocoa.
41  TCanvas * const cnv = new TCanvas("transparency", "transparency demo", 600, 400);
42  //After we created a canvas, gVirtualX in principle should be initialized
43  //and we can check its type:
44  if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
45  Warning("transp", "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 MacOSX version with cocoa enabled");
47  }
48 
49  TH1F * const hist = new TH1F("a5", "b5", 10, -2., 3.);
50  TH1F * const hist2 = new TH1F("c6", "d6", 10, -3., 3.);
51  hist->FillRandom("landau", 100000);
52  hist2->FillRandom("gaus", 100000);
53 
54  hist->SetFillColor(redIndex);
55  hist2->SetFillColor(greeIndex);
56 
57  cnv->cd();
58  hist2->Draw();
59  hist->Draw("SAME");
60 }
TH1F.h
Warning
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition: TError.cxx:231
gVirtualX
#define gVirtualX
Definition: TVirtualX.h:338
TColor.h
TVirtualX.h
TCanvas.h
ROOT::CocoaTutorials::FindFreeCustomColorIndices
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition: customcolor.h:38
Color_t
short Color_t
Definition: RtypesCore.h:83
TColor
Definition: TColor.h:19
TCanvas::cd
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:704
customcolor.h
TH1::FillRandom
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3444
TCanvas
Definition: TCanvas.h:23
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:572
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2997
Error
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
TError.h