ROOT
tags/v6-34-10
Reference Guide
Loading...
Searching...
No Matches
gradients.C
Go to the documentation of this file.
1
/// \file
2
/// \ingroup tutorial_cocoa
3
/// This macro requires OS X and ROOT
4
/// compiled with --enable-cocoa to run.
5
///
6
/// Features:
7
/// 1. Radial and linear gradients
8
/// 2. Transparent/semitransparent colours.
9
/// 3. Shadows.
10
///
11
/// \macro_code
12
///
13
/// \author Timur Pocheptsov
14
15
//Includes for ACLiC:
16
#include "
TColorGradient.h
"
17
#include "
TVirtualX.h
"
18
#include "
TCanvas.h
"
19
#include "
TError.h
"
20
#include "
TText.h
"
21
#include "
TPie.h
"
22
23
//Cocoa aux. functions.
24
#include "
customcolor.h
"
25
26
void
gradients()
27
{
28
//Find free colour indices in the ROOT's palette for:
29
//1. A radial gradient for TPie;
30
//2. A linear gradient for TCanvas
31
//3. A fully transparent fill color for a nested pad.
32
33
Color_t
colorIndices
[3] = {};
34
if
(
ROOT::CocoaTutorials::FindFreeCustomColorIndices
(
colorIndices
) != 3) {
35
::Error
(
"grad"
,
"failed to create new custom colors"
);
36
return
;
37
}
38
39
//Better names:
40
const
Color_t
&
radialFill
=
colorIndices
[0];
41
const
Color_t
&
linearFill
=
colorIndices
[1];
42
const
Color_t
&
transparentFill
=
colorIndices
[2];
43
44
//Create a canvas to check if we have a right back-end which supports gradients:
45
TCanvas
*
const
c
=
new
TCanvas
(
"cpie"
,
"Gradient colours demo"
, 700, 700);
46
//Before we allocated any new colour or created any object:
47
if
(
gVirtualX
&& !
gVirtualX
->InheritsFrom(
"TGCocoa"
)) {
48
::Error
(
"gradients"
,
"This macro requires OS X and ROOT built with --enable-cocoa"
);
49
delete
c
;
50
return
;
51
}
52
53
//Linear gradient is defined by: 1) colors (to interpolate between them),
54
//2) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
55
//3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
56
//and this rect is either: bounding rect of your polygon/object to fill
57
//(gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
58
//or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
59
//kObjectBoundingMode is the default one.
60
61
62
//Colour positions in the gradient's palette (here I place colors at the
63
//ends of 0-1):
64
const
Double_t
locations
[] = {0., 1.};
65
//Linear gradient fill (with an axis angle == 45):
66
const
Double_t
rgbaData1
[] = {0.2, 0.2, 0.2, 1.,
/*gray*/
67
0.8, 1., 0.9, 1.
/*pale green*/
};
68
TLinearGradient
*
const
gradientFill1
=
new
TLinearGradient
(
linearFill
, 2,
locations
,
rgbaData1
);
69
//45 degrees:
70
gradientFill1
->SetStartEnd(
TColorGradient::Point
(0., 0.),
TColorGradient::Point
(1., 1.));
71
//Set as a background color in the canvas:
72
c
->SetFillColor(
linearFill
);
73
74
//Draw a text in the canvas (the object above the text will be
75
//semi-transparent):
76
TText
*
const
t =
new
TText
(0.05, 0.7,
"Can you see the text?"
);
77
t->
Draw
();
78
79
//We create a nested pad on top to render a TPie in,
80
//this way we still have a text (below) + TPie with
81
//a fancy colour on top.
82
TPad
*
const
pad
=
new
TPad
(
"p"
,
"p"
, 0., 0., 1., 1.);
83
84
//TPad itself is fully transparent:
85
new
TColor
(
transparentFill
, 1., 1., 1.,
"transparent_fill_color"
, 0.);
86
pad
->SetFillColor(
transparentFill
);
87
//Add our pad into the canvas:
88
pad
->Draw();
89
pad
->cd();
90
91
//Radial gradient fill for a TPie object:
92
const
Double_t
rgbaData2
[] = {
/*opaque orange at the start:*/
1., 0.8, 0., 1.,
93
/*transparent red at the end:*/
1., 0.2, 0., 0.8};
94
95
//
96
//With Quartz/Cocoa we support the "extended" radial gradient:
97
//you can specify two centers and two radiuses - the start and
98
//the end of your radial gradient (+ colors/positions as with a linear
99
//gradient).
100
//
101
102
TRadialGradient
*
const
gradientFill2
=
new
TRadialGradient
(
radialFill
, 2,
103
locations
,
rgbaData2
);
104
//Parameters for a gradient fill:
105
//the gradient is 'pad-related' - we calculate everything in a pad's
106
//space and consider it as a NDC (so pad's rect is (0,0), (1,1)).
107
gradientFill2
->SetCoordinateMode(
TColorGradient::kPadMode
);
108
//Centers for both circles are the same point.
109
gradientFill2
->SetStartEndR1R2(
TColorGradient::Point
(0.5, 0.5), 0.1,
110
TColorGradient::Point
(0.5, 0.5), 0.4);
111
112
const
UInt_t
nSlices
= 5;
113
//Values for a TPie (non-const, that's how TPie's ctor is declared):
114
Double_t
values[
nSlices
] = {0.8, 1.2, 1.2, 0.8, 1.};
115
Int_t
colors
[
nSlices
] = {
radialFill
,
radialFill
,
radialFill
,
116
radialFill
,
radialFill
};
117
118
TPie
*
const
pie
=
new
TPie
(
"pie"
,
"TPie:"
,
nSlices
, values,
colors
);
119
//One slice is slightly shifted:
120
pie
->SetEntryRadiusOffset(2, 0.05);
121
//Move labels to the center (to fit the pad's space):
122
pie
->SetLabelsOffset(-0.08);
123
//
124
pie
->SetRadius(0.4);
125
pie
->Draw(
"rsc"
);
126
}
c
#define c(i)
Definition
RSha256.hxx:101
Int_t
int Int_t
Definition
RtypesCore.h:45
Color_t
short Color_t
Definition
RtypesCore.h:85
UInt_t
unsigned int UInt_t
Definition
RtypesCore.h:46
Double_t
double Double_t
Definition
RtypesCore.h:59
TCanvas.h
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
TColorGradient.h
TError.h
Error
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition
TError.cxx:185
TPie.h
TText.h
TVirtualX.h
gVirtualX
#define gVirtualX
Definition
TVirtualX.h:337
colors
Color * colors
Definition
X3DBuffer.c:21
ROOT::Detail::TRangeCast
Definition
TCollection.h:311
TCanvas
The Canvas class.
Definition
TCanvas.h:23
TColorGradient::kPadMode
@ kPadMode
Definition
TColorGradient.h:42
TColor
The color creation and management class.
Definition
TColor.h:21
TLinearGradient
Definition
TColorGradient.h:97
TObject::Draw
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition
TObject.cxx:280
TPad
The most important graphics class in the ROOT system.
Definition
TPad.h:28
TPie
Draw a Pie Chart,.
Definition
TPie.h:23
TRadialGradient
Definition
TColorGradient.h:124
TText
Base class for several text objects.
Definition
TText.h:22
customcolor.h
ROOT::CocoaTutorials::FindFreeCustomColorIndices
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition
customcolor.h:38
TColorGradient::Point
Definition
TColorGradient.h:46
tutorials
cocoa
gradients.C
ROOT tags/v6-34-10 - Reference Guide Generated on Mon Jun 30 2025 10:56:36 (GVA Time) using Doxygen 1.10.0