ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
radialgradients.C
Go to the documentation of this file.
1 //Author: Timur Pocheptsov, 25/03/2014.
2 //This tutorial demonstrates how to use radial gradients,
3 //custom colors, transparency.
4 //Requires ROOT built for OS X with --enable-cocoa.
5 
6 
7 //Includes for ACLiC:
8 #include <cassert>
9 #include <cstdlib>
10 
11 #include "TColorGradient.h"
12 #include "TVirtualX.h"
13 #include "TEllipse.h"
14 #include "TRandom.h"
15 #include "TCanvas.h"
16 #include "TError.h"
17 
18 //Cocoa aux. functions.
19 #include "customcolor.h"
20 
21 namespace {
22 
23 //Just some colors (rgba) to build our
24 //fancy gradients from.
25 const Double_t basicColors[][4] =
26 {
27 {1., 0., 0., 1.},
28 {1., 0.3, 0., 1.},
29 {0., 0., 1., 1.},
30 {1., 1., 0., 1.},
31 {1., 0., 1., 1.},
32 {0., 1., 1., 1.},
33 {0., 1., 0., 1.},
34 {0., 0.5, 0., 1.},
35 //transparent colors:
36 {1., 0., 0., 0.5},
37 {1., 0.3, 0., 0.5},
38 {0., 0., 1., 0.5},
39 {1., 1., 0., 0.5},
40 {1., 0., 1., 0.5},
41 {0., 1., 1., 0.5},
42 {0., 1., 0., 0.5},
43 {0., 0.5, 0., 0.5},
44 //and even more transparent:
45 {1., 0., 0., 0.2},
46 {1., 0.3, 0., 0.2},
47 {0., 0., 1., 0.2},
48 {1., 1., 0., 0.2},
49 {1., 0., 1., 0.2},
50 {0., 1., 1., 0.2},
51 {0., 1., 0., 0.2},
52 {0., 0.5, 0., 0.2}
53 };
54 
55 const unsigned nBasicColors = sizeof basicColors / sizeof basicColors[0];
56 
57 //______________________________________________________________________
58 Color_t CreateRandomGradientFill()
59 {
60  Color_t idx[1] = {};
62  return -1;
63 
64  const Double_t * const fromRGBA = basicColors[(std::rand() % (nBasicColors / 2))];
65  //With odd number of colors the last one is never selected :)
66  const Double_t * const toRGBA = basicColors[nBasicColors / 2 + (std::rand() % (nBasicColors / 2))];
67 
68  const Double_t locations[] = {0., 1.};
69  const Double_t rgbas[] = {fromRGBA[0], fromRGBA[1], fromRGBA[2], fromRGBA[3],
70  toRGBA[0], toRGBA[1], toRGBA[2], toRGBA[3]};
71 
72  TRadialGradient * const grad = new TRadialGradient(idx[0], 2, locations, rgbas);
73  grad->SetRadialGradient(TColorGradient::Point(0.5, 0.5), 0.5);
74 
75  return idx[0];
76 }
77 
78 //______________________________________________________________________
79 bool add_ellipse(const Double_t xC, const Double_t yC, const Double_t r)
80 {
81  assert(gPad != nullptr && "add_ellipse, no pad to add ellipse");
82 
83  const Color_t newColor = CreateRandomGradientFill();
84  if (newColor == -1) {
85  ::Error("add_ellipse", "failed to find a new color index for a gradient fill");
86  return false;
87  }
88 
89  TEllipse * const newEllipse = new TEllipse(xC, yC, r, r);
90  newEllipse->SetFillColor(newColor);
91  newEllipse->Draw();
92 
93  return true;
94 }
95 
96 }
97 
98 //______________________________________________________________________
100 {
101  gRandom->SetSeed(4357);//;)
102 
103  TCanvas * const cnv = new TCanvas("radial gradients", "radial gradients", 800, 800);
104  if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
105  ::Error("radialgradients",
106  "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 }
void radialgradients()
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition: customcolor.h:44
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
#define assert(cond)
Definition: unittest.h:542
void SetRadialGradient(const Point &center, Double_t radius)
Set radial gradient.
Define a radial color gradient.
virtual void SetSeed(UInt_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 void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
#define gVirtualX
Definition: TVirtualX.h:362
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
Draw Ellipses.
Definition: TEllipse.h:44
#define gPad
Definition: TVirtualPad.h:288
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
void Modified(Bool_t flag=1)
Definition: TPad.h:407