Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
radialgradients.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_cocoa
3/// This tutorial demonstrates how to use radial gradients,
4/// custom colors, transparency.
5/// Requires ROOT built for OS X with --enable-cocoa.
6///
7/// \macro_code
8///
9/// \author Timur Pocheptsov
10
11//Includes for ACLiC:
12#include <cassert>
13#include <cstdlib>
14
15#include "TColorGradient.h"
16#include "TVirtualX.h"
17#include "TEllipse.h"
18#include "TRandom.h"
19#include "TCanvas.h"
20#include "TError.h"
21
22//Cocoa aux. functions.
23#include "customcolor.h"
24
25namespace {
26
27//Just some colors (rgba) to build our
28//fancy gradients from.
29const Double_t basicColors[][4] =
30{
31{1., 0., 0., 1.},
32{1., 0.3, 0., 1.},
33{0., 0., 1., 1.},
34{1., 1., 0., 1.},
35{1., 0., 1., 1.},
36{0., 1., 1., 1.},
37{0., 1., 0., 1.},
38{0., 0.5, 0., 1.},
39//transparent colors:
40{1., 0., 0., 0.5},
41{1., 0.3, 0., 0.5},
42{0., 0., 1., 0.5},
43{1., 1., 0., 0.5},
44{1., 0., 1., 0.5},
45{0., 1., 1., 0.5},
46{0., 1., 0., 0.5},
47{0., 0.5, 0., 0.5},
48//and even more transparent:
49{1., 0., 0., 0.2},
50{1., 0.3, 0., 0.2},
51{0., 0., 1., 0.2},
52{1., 1., 0., 0.2},
53{1., 0., 1., 0.2},
54{0., 1., 1., 0.2},
55{0., 1., 0., 0.2},
56{0., 0.5, 0., 0.2}
57};
58
59const unsigned nBasicColors = sizeof basicColors / sizeof basicColors[0];
60
61//______________________________________________________________________
62Color_t CreateRandomGradientFill()
63{
64 Color_t idx[1] = {};
66 return -1;
67
68 const Double_t * const fromRGBA = basicColors[(std::rand() % (nBasicColors / 2))];
69 //With odd number of colors the last one is never selected :)
70 const Double_t * const toRGBA = basicColors[nBasicColors / 2 + (std::rand() % (nBasicColors / 2))];
71
72 const Double_t locations[] = {0., 1.};
73 const Double_t rgbas[] = {fromRGBA[0], fromRGBA[1], fromRGBA[2], fromRGBA[3],
74 toRGBA[0], toRGBA[1], toRGBA[2], toRGBA[3]};
75
76 TRadialGradient * const grad = new TRadialGradient(idx[0], 2, locations, rgbas);
77 grad->SetRadialGradient(TColorGradient::Point(0.5, 0.5), 0.5);
78
79 return idx[0];
80}
81
82//______________________________________________________________________
83bool add_ellipse(const Double_t xC, const Double_t yC, const Double_t r)
84{
85 assert(gPad != nullptr && "add_ellipse, no pad to add ellipse");
86
87 const Color_t newColor = CreateRandomGradientFill();
88 if (newColor == -1) {
89 ::Error("add_ellipse", "failed to find a new color index for a gradient fill");
90 return false;
91 }
92
93 TEllipse * const newEllipse = new TEllipse(xC, yC, r, r);
94 newEllipse->SetFillColor(newColor);
95 newEllipse->Draw();
96
97 return true;
98}
99
100}
101
102//______________________________________________________________________
103void radialgradients()
104{
105 gRandom->SetSeed(4357);//;)
106
107 TCanvas * const cnv = new TCanvas("radial gradients", "radial gradients", 800, 800);
108 if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
109 ::Error("radialgradients",
110 "this demo requires OS X and ROOT built with --enable-cocoa");
111 delete cnv;
112 return;
113 }
114
115 for (unsigned i = 0; i < 100; ++i)
116 if (!add_ellipse(gRandom->Rndm(), gRandom->Rndm(), 0.5 * gRandom->Rndm()))
117 break;
118
119 cnv->Modified();
120 cnv->Update();
121}
ROOT::R::TRInterface & r
Definition Object.C:4
double Double_t
Definition RtypesCore.h:59
short Color_t
Definition RtypesCore.h:92
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:187
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
#define gPad
#define gVirtualX
Definition TVirtualX.h:338
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
The Canvas class.
Definition TCanvas.h:23
void Update() override
Update canvas pad buffers.
Definition TCanvas.cxx:2502
Draw Ellipses.
Definition TEllipse.h:23
virtual void Draw(Option_t *option="")
Draw this ellipse with its current attributes.
Definition TEllipse.cxx:168
void Modified(Bool_t flag=1) override
Definition TPad.h:413
Define a radial color gradient.
void SetRadialGradient(const Point &center, Double_t radius)
Set radial gradient.
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition TRandom.cxx:608
virtual Double_t Rndm()
Machine independent random number generator.
Definition TRandom.cxx:552
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition customcolor.h:38