Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 or Web-based canvas
6///
7/// \macro_image(nobatch)
8/// \macro_code
9///
10/// \authors Timur Pocheptsov, Sergey Linev
11
12//Includes for ACLiC:
13#include "TColorGradient.h"
14#include "TEllipse.h"
15#include "TRandom.h"
16#include "TCanvas.h"
17#include "TStyle.h"
18#include "TError.h"
19
20//______________________________________________________________________
21Color_t CreateRandomGradientFill()
22{
23 std::vector<Int_t> colors;
24
25 for (int n = 0; n < 2; ++n)
26 colors.emplace_back(gRandom->Integer(10) + 2);
27
28 auto indx = TColor::GetRadialGradient(0.5, colors);
29
30 // example how to modify gradient
31 auto gradient = dynamic_cast<TRadialGradient *> (gROOT->GetColor(indx));
32 if (gradient) {
33 // change center and radius
34 gradient->SetRadialGradient({0.3, 0.3}, 0.7);
35 // change alpha parameter for the colors
36
37 gradient->SetColorAlpha(0, 0.2 + gRandom->Rndm() * 0.8);
38 gradient->SetColorAlpha(1, 0.2 + gRandom->Rndm() * 0.8);
39 } else {
40 ::Error("CreateRandomGradientFill", "failed to find new gradient color with index %d", indx);
41 }
42
43 return indx;
44}
45
46//______________________________________________________________________
47bool add_ellipse(const Double_t xC, const Double_t yC, const Double_t r)
48{
49 const Color_t newColor = CreateRandomGradientFill();
50 if (newColor == -1) {
51 ::Error("add_ellipse", "failed to find a new color index for a gradient fill");
52 return false;
53 }
54
55 TEllipse * const newEllipse = new TEllipse(xC, yC, r, r);
56 newEllipse->SetFillColor(newColor);
57 newEllipse->Draw();
58
59 return true;
60}
61
62//______________________________________________________________________
63void radialgradients(bool gl = true)
64{
65 gRandom->SetSeed(4357);
66
68
69 auto cnv = new TCanvas("radialgradients", "radial gradients", 800, 800);
70 if (!cnv->UseGL() && !cnv->IsWeb())
71 ::Warning("radialgradients", "This macro requires either OpenGL or Web canvas to correctly handle gradient colors");
72
73 for (unsigned i = 0; i < 100; ++i)
74 add_ellipse(gRandom->Rndm(), gRandom->Rndm(), 0.5 * gRandom->Rndm());
75}
short Color_t
Definition RtypesCore.h:92
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
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
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
#define gROOT
Definition TROOT.h:406
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
R__EXTERN TStyle * gStyle
Definition TStyle.h:433
Color * colors
Definition X3DBuffer.c:21
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
The Canvas class.
Definition TCanvas.h:23
static Int_t GetRadialGradient(Double_t r, const std::vector< Int_t > &colors, const std::vector< Double_t > &positions={})
Static function: Returns the radial gradient color number corresponding to specified parameters.
Definition TColor.cxx:2192
Draw Ellipses.
Definition TEllipse.h:23
void Draw(Option_t *option="") override
Draw this ellipse with its current attributes.
Definition TEllipse.cxx:169
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
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
Definition TRandom.cxx:361
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition TStyle.h:339
const Int_t n
Definition legend1.C:16