ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
transp.C
Go to the documentation of this file.
1 //Author: Timur Pocheptsov, 25/09/2012
2 //This demo shows how to use transparency.
3 //On MacOS X you can see the transparency in a canvas,
4 //you can save canvas contents as pdf/png
5 //(and thus you'll have an image with transparency on every platform).
6 
7 //Includes for ACLiC (cling does not need them).
8 #include "TVirtualX.h"
9 #include "TCanvas.h"
10 #include "TColor.h"
11 #include "TError.h"
12 #include "TH1F.h"
13 
14 //Aux. functions for tutorials/cocoa.
15 #include "customcolor.h"
16 
17 void transp()
18 {
19  //1. Try to find free indices for our custom colors.
20  //We can use hard-coded indices like 1001, 1002, 1003, ... but
21  //I prefer to find free indices in a ROOT's color table
22  //to avoid possible conflicts with other tutorials.
23  Color_t indices[2] = {};
25  ::Error("transp", "failed to create new custom colors");
26  return;
27  }
28 
29  //2. Now that we have indices, create our custom colors.
30  const Color_t redIndex = indices[0], greeIndex = indices[1];
31 
32  new TColor(redIndex, 1., 0., 0., "red", 0.85);
33  new TColor(greeIndex, 0., 1., 0., "green", 0.5);
34 
35  //3. Test that back-end is TGCocoa.
36  TCanvas * const cnv = new TCanvas("trasnparency", "transparency demo", 600, 400);
37  //After we created a canvas, gVirtualX in principle should be initialized
38  //and we can check its type:
39  if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
40  Warning("transp", "You can see the transparency ONLY in a pdf or png output (\"File\"->\"Save As\" ->...)\n"
41  "To have transparency in a canvas graphics, you need MacOSX version with cocoa enabled");
42  }
43 
44  TH1F * const hist = new TH1F("a5", "b5", 10, -2., 3.);
45  TH1F * const hist2 = new TH1F("c6", "d6", 10, -3., 3.);
46  hist->FillRandom("landau", 100000);
47  hist2->FillRandom("gaus", 100000);
48 
49  hist->SetFillColor(redIndex);
50  hist2->SetFillColor(greeIndex);
51 
52  cnv->cd();
53  hist2->Draw();
54  hist->Draw("SAME");
55 }
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition: customcolor.h:44
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
void Error(const char *location, const char *msgfmt,...)
short Color_t
Definition: RtypesCore.h:79
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3330
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
void Warning(const char *location, const char *msgfmt,...)
#define gVirtualX
Definition: TVirtualX.h:362
The Canvas class.
Definition: TCanvas.h:48
The color creation and management class.
Definition: TColor.h:47
void transp()
Definition: transp.C:17