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] = {{1., 0., 0., 1.},
30 {1., 0.3, 0., 1.},
31 {0., 0., 1., 1.},
32 {1., 1., 0., 1.},
33 {1., 0., 1., 1.},
34 {0., 1., 1., 1.},
35 {0., 1., 0., 1.},
36 {0., 0.5, 0., 1.},
37 // transparent colors:
38 {1., 0., 0., 0.5},
39 {1., 0.3, 0., 0.5},
40 {0., 0., 1., 0.5},
41 {1., 1., 0., 0.5},
42 {1., 0., 1., 0.5},
43 {0., 1., 1., 0.5},
44 {0., 1., 0., 0.5},
45 {0., 0.5, 0., 0.5},
46 // and even more transparent:
47 {1., 0., 0., 0.2},
48 {1., 0.3, 0., 0.2},
49 {0., 0., 1., 0.2},
50 {1., 1., 0., 0.2},
51 {1., 0., 1., 0.2},
52 {0., 1., 1., 0.2},
53 {0., 1., 0., 0.2},
54 {0., 0.5, 0., 0.2}};
55
56const unsigned nBasicColors = sizeof basicColors / sizeof basicColors[0];
57
58//______________________________________________________________________
59Color_t CreateRandomGradientFill()
60{
61 Color_t idx[1] = {};
63 return -1;
64
65 const Double_t *const fromRGBA = basicColors[(std::rand() % (nBasicColors / 2))];
66 // With odd number of colors the last one is never selected :)
67 const Double_t *const toRGBA = basicColors[nBasicColors / 2 + (std::rand() % (nBasicColors / 2))];
68
69 const Double_t locations[] = {0., 1.};
70 const Double_t rgbas[] = {fromRGBA[0], fromRGBA[1], fromRGBA[2], fromRGBA[3],
71 toRGBA[0], toRGBA[1], toRGBA[2], toRGBA[3]};
72
73 TRadialGradient *const grad = new TRadialGradient(idx[0], 2, locations, rgbas);
74 grad->SetRadialGradient(TColorGradient::Point(0.5, 0.5), 0.5);
75
76 return idx[0];
77}
78
79//______________________________________________________________________
80bool add_ellipse(const Double_t xC, const Double_t yC, const Double_t r)
81{
82 assert(gPad != nullptr && "add_ellipse, no pad to add ellipse");
83
84 const Color_t newColor = CreateRandomGradientFill();
85 if (newColor == -1) {
86 ::Error("add_ellipse", "failed to find a new color index for a gradient fill");
87 return false;
88 }
89
90 TEllipse *const newEllipse = new TEllipse(xC, yC, r, r);
91 newEllipse->SetFillColor(newColor);
92 newEllipse->Draw();
93
94 return true;
95}
96
97} // namespace
98
99//______________________________________________________________________
100void radialgradients()
101{
102 gRandom->SetSeed(4357); //;)
103
104 TCanvas *const cnv = new TCanvas("radial gradients", "radial gradients", 800, 800);
105 if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
106 ::Error("radialgradients", "this demo requires OS X and ROOT built with --enable-cocoa");
107 delete cnv;
108 return;
109 }
110
111 for (unsigned i = 0; i < 100; ++i)
112 if (!add_ellipse(gRandom->Rndm(), gRandom->Rndm(), 0.5 * gRandom->Rndm()))
113 break;
114
115 cnv->Modified();
116 cnv->Update();
117}
short Color_t
Definition RtypesCore.h:85
double Double_t
Definition RtypesCore.h:59
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
#define gPad
#define gVirtualX
Definition TVirtualX.h:337
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:38
The Canvas class.
Definition TCanvas.h:23
void Update() override
Update canvas pad buffers.
Definition TCanvas.cxx:2489
Draw Ellipses.
Definition TEllipse.h:23
void Draw(Option_t *option="") override
Draw this ellipse with its current attributes.
Definition TEllipse.cxx:169
void Modified(Bool_t flag=true) override
Mark pad modified Will be repainted when TCanvas::Update() will be called next time.
Definition TPad.cxx:7388
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:615
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition customcolor.h:38