Logo ROOT  
Reference Guide

Detailed Description

Gradient fill with transparency and "SAME" option.

Requires OS X and ROOT configured with –enable-cocoa.

//Includes for ACLiC (cling does not need them).
#include "TColorGradient.h"
#include "TCanvas.h"
#include "TError.h"
#include "TColor.h"
#include "TStyle.h"
#include "TH1F.h"
//Aux. functions for tutorials/gl.
#include "customcolorgl.h"
void grad2()
{
//1. 'Allocate' four indices for our custom colors.
//We can use hard-coded indices like 1001, 1002, 1003 ... but
//I prefer to find free indices in the ROOT's color table
//to avoid possible conflicts with other tutorials.
Color_t freeIndices[4] = {};
::Error("grad2", "can not allocate new custom colors");
return;
}
//'Aliases' (instead of freeIndices[someIndex])
const Color_t customRed = freeIndices[0], grad1 = freeIndices[1];
const Color_t customGreen = freeIndices[2], grad2 = freeIndices[3];
//Make sure canvas supports OpenGL.
//2. Check that we have a canvas with an OpenGL support.
TCanvas * const cnv = new TCanvas("gradiend demo 2", "gradient demo 2", 100, 100, 800, 600);
if (!cnv->UseGL()) {
::Error("grad2", "This macro requires OpenGL");
delete cnv;
return;
}
//3. Custom colors:
// a) Custom semi-transparent red.
new TColor(customRed, 1., 0., 0., "red", 0.5);
// b) Gradient (from our semi-transparent red to ROOT's kOrange).
// Linear gradient is defined by: 1) colors (to interpolate between them),
// 2) coordinates for these colors along the gradient axis [0., 1.] (must be sorted!).
// 3) Start and end points for a gradient, you specify them in some NDC rect ([0,0 - 1,1]),
// and this rect is either: bounding rect of your polygon/object to fill
// (gradient->SetCoordinateMode(TColorGradient::kObjectBoundingMode))
// or bounding rect of a pad (gradient->SetCoordinateMode(TColorGradient::kPadMode)).
// kObjectBoundingMode is the default one.
const Double_t locations[] = {0., 1.};
const Color_t idx1[] = {customRed, kOrange};
TLinearGradient * const gradFill1 = new TLinearGradient(grad1, 2, locations, idx1);
typedef TColorGradient::Point Point;
//Starting and ending points for a gradient fill (it's a vertical gradient):
gradFill1->SetStartEnd(Point(0., 0.), Point(0., 1.));
// c) Custom semi-transparent green.
new TColor(customGreen, 0., 1., 0., "green", 0.5);
// d) Gradient from ROOT's kBlue to our custom green.
const Color_t idx2[] = {customGreen, kBlue};
TLinearGradient * const gradFill2 = new TLinearGradient(grad2, 2, locations, idx2);
//Vertical gradient fill.
gradFill2->SetStartEnd(Point(0., 0.), Point(0., 1.));
TH1F * const hist = new TH1F("a2", "b2", 10, -2., 3.);
TH1F * const hist2 = new TH1F("c3", "d3", 10, -3., 3.);
hist->FillRandom("landau", 100000);
hist2->FillRandom("gaus", 100000);
hist->SetFillColor(grad1);
hist2->SetFillColor(grad2);
hist2->Draw();
hist->Draw("SAME");
}
Author
Timur Pocheptsov

Definition in file grad2.C.

TStyle::SetCanvasPreferGL
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition: TStyle.h:325
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TH1F.h
ROOT::GLTutorials::FindFreeCustomColorIndices
unsigned FindFreeCustomColorIndices(T(&indices)[N])
Definition: customcolorgl.h:40
TColorGradient.h
TStyle.h
kOrange
@ kOrange
Definition: Rtypes.h:67
TColor.h
TColorGradient::Point
Definition: TColorGradient.h:49
TCanvas::UseGL
Bool_t UseGL() const
Definition: TCanvas.h:223
TH1::FillRandom
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3502
TCanvas.h
Color_t
short Color_t
Definition: RtypesCore.h:83
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TColor
The color creation and management class.
Definition: TColor.h:19
TLinearGradient
Define a linear color gradient.
Definition: TColorGradient.h:99
TLinearGradient::SetStartEnd
void SetStartEnd(const Point &p1, const Point &p2)
Set end and start.
Definition: TColorGradient.cxx:218
Double_t
double Double_t
Definition: RtypesCore.h:59
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:573
TAttFill::SetFillColor
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
kBlue
@ kBlue
Definition: Rtypes.h:66
customcolorgl.h
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3050
Error
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
TError.h