ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
gradients.C
Go to the documentation of this file.
1 //Author: Timur Pocheptsov, 19/03/2014
2 //This macro requires OS X and ROOT
3 //compiled with --enable-cocoa to run.
4 
5 //Features:
6 //1. Radial and linear gradients
7 //2. Transparent/semitransparent colours.
8 //3. Shadows.
9 
10 
11 //Includes for ACLiC:
12 #include "TColorGradient.h"
13 #include "TVirtualX.h"
14 #include "TCanvas.h"
15 #include "TError.h"
16 #include "TText.h"
17 #include "TPie.h"
18 
19 //Cocoa aux. functions.
20 #include "customcolor.h"
21 
22 void gradients()
23 {
24  //Find free colour indices in the ROOT's palette for:
25  //1. A radial gradient for TPie;
26  //2. A linear gradient for TCanvas
27  //3. A fully transparent fill color for a nested pad.
28 
29  Color_t colorIndices[3] = {};
30  if (ROOT::CocoaTutorials::FindFreeCustomColorIndices(colorIndices) != 3) {
31  ::Error("grad", "failed to create new custom colors");
32  return;
33  }
34 
35  //Better names:
36  const Color_t &radialFill = colorIndices[0];
37  const Color_t &linearFill = colorIndices[1];
38  const Color_t &transparentFill = colorIndices[2];
39 
40  //Create a canvas to check if we have a right back-end which supports gradients:
41  TCanvas * const c = new TCanvas("cpie","Gradient colours demo", 700, 700);
42  //Before we allocated any new colour or created any object:
43  if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
44  ::Error("gradients", "This macro requires OS X and ROOT built with --enable-cocoa");
45  delete c;
46  return;
47  }
48 
49  //Linear gradient is defined by: 1) colors (to interpolate between them),
50  //2) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
51  //3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
52  //and this rect is either: bounding rect of your polygon/object to fill
53  //(gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
54  //or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
55  //kObjectBoundingMode is the default one.
56 
57 
58  //Colour positions in the gradient's palette (here I place colors at the
59  //ends of 0-1):
60  const Double_t locations[] = {0., 1.};
61  //Linear gradient fill (with an axis angle == 45):
62  const Double_t rgbaData1[] = {0.2, 0.2, 0.2, 1.,/*gray*/
63  0.8, 1., 0.9, 1. /*pale green*/};
64  TLinearGradient * const gradientFill1 = new TLinearGradient(linearFill, 2, locations, rgbaData1);
65  //45 degrees:
66  gradientFill1->SetStartEnd(TColorGradient::Point(0., 0.), TColorGradient::Point(1., 1.));
67  //Set as a background color in the canvas:
68  c->SetFillColor(linearFill);
69 
70  //Draw a text in the canvas (the object above the text will be
71  //semi-transparent):
72  TText * const t = new TText(0.05, 0.7, "Can you see the text?");
73  t->Draw();
74 
75  //We create a nested pad on top to render a TPie in,
76  //this way we still have a text (below) + TPie with
77  //a fancy colour on top.
78  TPad * const pad = new TPad("p", "p", 0., 0., 1., 1.);
79 
80  //TPad itself is fully transparent:
81  new TColor(transparentFill, 1., 1., 1., "transparent_fill_color", 0.);
82  pad->SetFillColor(transparentFill);
83  //Add our pad into the canvas:
84  pad->Draw();
85  pad->cd();
86 
87  //Radial gradient fill for a TPie object:
88  const Double_t rgbaData2[] = {/*opaque orange at the start:*/1., 0.8, 0., 1.,
89  /*transparent red at the end:*/1., 0.2, 0., 0.8};
90 
91  //
92  //With Quartz/Cocoa we support the "extended" radial gradient:
93  //you can specify two centers and two radiuses - the start and
94  //the end of your radial gradient (+ colors/positions as with a linear
95  //gradient).
96  //
97 
98  TRadialGradient * const gradientFill2 = new TRadialGradient(radialFill, 2,
99  locations, rgbaData2);
100  //Parameters for a gradient fill:
101  //the gradient is 'pad-related' - we calculate everything in a pad's
102  //space and consider it as a NDC (so pad's rect is (0,0), (1,1)).
104  //Centers for both circles are the same point.
105  gradientFill2->SetStartEndR1R2(TColorGradient::Point(0.5, 0.5), 0.1,
106  TColorGradient::Point(0.5, 0.5), 0.4);
107 
108  const UInt_t nSlices = 5;
109  //Values for a TPie (non-const, that's how TPie's ctor is declared):
110  Double_t values[nSlices] = {0.8, 1.2, 1.2, 0.8, 1.};
111  Int_t colors[nSlices] = {radialFill, radialFill, radialFill,
112  radialFill, radialFill};
113 
114  TPie * const pie = new TPie("pie", "TPie:", nSlices, values, colors);
115  //One slice is slightly shifted:
116  pie->SetEntryRadiusOffset(2, 0.05);
117  //Move labels to the center (to fit the pad's space):
118  pie->SetLabelsOffset(-0.08);
119  //
120  pie->SetRadius(0.4);
121  pie->Draw("rsc");
122 }
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition: customcolor.h:44
return c
std::vector< double > values
Definition: TwoHistoFit2D.C:32
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:254
Define a radial color gradient.
void SetStartEndR1R2(const Point &p1, Double_t r1, const Point &p2, Double_t r2)
Set start and end R1 and R2.
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:514
void gradients()
Definition: gradients.C:22
void SetEntryRadiusOffset(Int_t, Double_t)
Set the distance, in the direction of the radius of the slice.
Definition: TPie.cxx:1268
void SetCoordinateMode(ECoordinateMode mode)
Set coordinate mode.
Base class for several text objects.
Definition: TText.h:42
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1192
void Error(const char *location, const char *msgfmt,...)
short Color_t
Definition: RtypesCore.h:79
TThread * t[5]
Definition: threadsh1.C:13
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
unsigned int UInt_t
Definition: RtypesCore.h:42
The most important graphics class in the ROOT system.
Definition: TPad.h:46
tuple pad
Definition: first.py:38
#define gVirtualX
Definition: TVirtualX.h:362
Define a linear color gradient.
Color * colors
Definition: X3DBuffer.c:19
The Canvas class.
Definition: TCanvas.h:48
void SetRadius(Double_t)
Set the pie chart's radius' value.
Definition: TPie.cxx:1367
double Double_t
Definition: RtypesCore.h:55
Draw a Pie Chart,.
Definition: TPie.h:31
The color creation and management class.
Definition: TColor.h:47
void SetLabelsOffset(Float_t)
Set the distance between the label end the external line of the TPie.
Definition: TPie.cxx:1351
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
virtual void Draw(Option_t *option="l")
Draw the pie chart.
Definition: TPie.cxx:274