Logo ROOT   6.10/09
Reference Guide
radialgradients.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_gl
3 /// This tutorial demonstrates how to use radial gradients,
4 /// custom colors, transparency.
5 /// Requires OpenGL: either set OpenGL.CanvasPreferGL to 1
6 /// in the $ROOTSYS/etc/system.rootrc,
7 /// or use `gStyle->SetCanvasPreferGL(kTRUE)`.
8 ///
9 /// \macro_image(nobatch)
10 /// \macro_code
11 ///
12 /// \author Timur Pocheptsov
13 
14 //Includes for ACLiC:
15 #include <cassert>
16 #include <cstdlib>
17 
18 #include "TColorGradient.h"
19 #include "TEllipse.h"
20 #include "TRandom.h"
21 #include "TCanvas.h"
22 #include "TStyle.h"
23 
24 #include "TError.h"
25 
26 //Aux. functions.
27 #include "customcolorgl.h"
28 
29 namespace {
30 
31 //Just some colors (rgba) to build our
32 //fancy gradients from.
33 const Double_t basicColors[][4] =
34 {
35 {1., 0., 0., 1.},
36 {1., 0.3, 0., 1.},
37 {0., 0., 1., 1.},
38 {1., 1., 0., 1.},
39 {1., 0., 1., 1.},
40 {0., 1., 1., 1.},
41 {0., 1., 0., 1.},
42 {0., 0.5, 0., 1.},
43 //transparent colors:
44 {1., 0., 0., 0.5},
45 {1., 0.3, 0., 0.5},
46 {0., 0., 1., 0.5},
47 {1., 1., 0., 0.5},
48 {1., 0., 1., 0.5},
49 {0., 1., 1., 0.5},
50 {0., 1., 0., 0.5},
51 {0., 0.5, 0., 0.5},
52 //and even more transparent:
53 {1., 0., 0., 0.2},
54 {1., 0.3, 0., 0.2},
55 {0., 0., 1., 0.2},
56 {1., 1., 0., 0.2},
57 {1., 0., 1., 0.2},
58 {0., 1., 1., 0.2},
59 {0., 1., 0., 0.2},
60 {0., 0.5, 0., 0.2}
61 };
62 
63 const unsigned nBasicColors = sizeof basicColors / sizeof basicColors[0];
64 
65 //______________________________________________________________________
66 Color_t CreateRandomGradientFill()
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  Color_t idx[1] = {};
78  return -1;
79 
80  TRadialGradient * const grad = new TRadialGradient(idx[0], 2, locations, rgbas);
81  //
82  grad->SetRadialGradient(TColorGradient::Point(0.5, 0.5), 0.5);
83 
84  return idx[0];
85 }
86 
87 //______________________________________________________________________
88 bool add_ellipse(const Double_t xC, const Double_t yC, const Double_t r)
89 {
90  assert(gPad != nullptr && "add_ellipse, no pad to add ellipse");
91 
92  const Color_t newColor = CreateRandomGradientFill();
93  if (newColor == -1) {
94  ::Error("add_ellipse", "failed to find a new color index for a gradient fill");
95  return false;
96  }
97 
98  TEllipse * const newEllipse = new TEllipse(xC, yC, r, r);
99  newEllipse->SetFillColor(newColor);
100  newEllipse->Draw();
101 
102  return true;
103 }
104 
105 }
106 
107 //______________________________________________________________________
108 void radialgradients()
109 {
110  gRandom->SetSeed(4357);//;)
111 
113 
114  TCanvas * const cnv = new TCanvas("radial gradients", "radial gradients", 800, 800);
115  if (!cnv->UseGL()) {
116  ::Error("radialgradients", "this demo OpenGL");
117  delete cnv;
118  return;
119  }
120 
121  for (unsigned i = 0; i < 100; ++i)
122  if (!add_ellipse(gRandom->Rndm(), gRandom->Rndm(), 0.5 * gRandom->Rndm()))
123  break;
124 
125  cnv->Modified();
126  cnv->Update();
127 }
unsigned FindFreeCustomColorIndices(T(&indices)[N])
Definition: customcolorgl.h:40
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
void SetRadialGradient(const Point &center, Double_t radius)
Set radial gradient.
Define a radial color gradient.
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
void Error(const char *location, const char *msgfmt,...)
short Color_t
Definition: RtypesCore.h:79
virtual void Draw(Option_t *option="")
Draw this ellipse with its current attributes.
Definition: TEllipse.cxx:167
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:512
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition: TStyle.h:316
TRandom2 r(17)
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
The Canvas class.
Definition: TCanvas.h:31
double Double_t
Definition: RtypesCore.h:55
Draw Ellipses.
Definition: TEllipse.h:24
#define gPad
Definition: TVirtualPad.h:284
Bool_t UseGL() const
Definition: TCanvas.h:226
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2208
const Bool_t kTRUE
Definition: RtypesCore.h:91
void Modified(Bool_t flag=1)
Definition: TPad.h:410