ROOT logo

From $ROOTSYS/tutorials/gl/grad2.C

//Author: Timur Pocheptsov, 19/03/2014
//Requires OpenGL: either set OpenGL.CanvasPreferGL to 1
//in the $ROOTSYS/etc/system.rootrc,
//or use gStyle->SetCanvasPreferGL(kTRUE).

//Features:
//1. Linear gradients
//2. Semitransparent colors.

//Includes for ACLiC:
#include "TColorGradient.h"
#include "TCanvas.h"
#include "TError.h"
#include "TStyle.h"
#include "TH1F.h"

#include "customcolors.h"

typedef TColorGradient::Point point_type;

void grad2()
{
   Color_t customIdx[4] = {};
   if (FindFreeCustomColorIndices(4, customIdx) != 4) {
      ::Error("grad2", "failed to allocate custom colors");
      return;
   }

   gStyle->SetCanvasPreferGL(kTRUE);
   
   TCanvas * const cnv = new TCanvas("gradient test 2", "gradient_test2", 100, 100, 800, 600);
   if (!cnv->UseGL()) {
      ::Error("grad2", "This macro requires OpenGL");
      delete cnv;
      return;
   }

   TH1F * const hist = new TH1F("hg2a", "hg2a", 10, -2., 3.);
   TH1F * const hist2 = new TH1F("hg2b", "hg2b", 10, -3., 3.);
   hist->FillRandom("landau", 100000);
   hist2->FillRandom("gaus", 100000);

   new TColor(customIdx[0], 1., 0., 0., "red", 0.5);

   const Double_t locations[] = {0., 1.};
   const Color_t idx1[] = {customIdx[0], kOrange};
   //Gradient from ROOT's kOrange and my own semi-transparent red color.
   TLinearGradient * const grad1 = new TLinearGradient(customIdx[1], 2, locations, idx1);
   const point_type start(0., 0.);
   const point_type end(0., 1.);
   grad1->SetStartEnd(start, end);
   
   hist->SetFillColor(customIdx[1]);
   
   new TColor(customIdx[2], 0., 1., 0., "green", 0.5);
   const Color_t idx2[] = {customIdx[2], kBlue};
   //Gradient from ROOT's kBlue and my own semi-transparent green color.
   TLinearGradient * const grad2 = new TLinearGradient(customIdx[3], 2, locations, idx2);
   grad2->SetStartEnd(start, end);
   hist2->SetFillColor(customIdx[3]);
   
   hist2->Draw();
   hist->Draw("SAME");
}
 grad2.C:1
 grad2.C:2
 grad2.C:3
 grad2.C:4
 grad2.C:5
 grad2.C:6
 grad2.C:7
 grad2.C:8
 grad2.C:9
 grad2.C:10
 grad2.C:11
 grad2.C:12
 grad2.C:13
 grad2.C:14
 grad2.C:15
 grad2.C:16
 grad2.C:17
 grad2.C:18
 grad2.C:19
 grad2.C:20
 grad2.C:21
 grad2.C:22
 grad2.C:23
 grad2.C:24
 grad2.C:25
 grad2.C:26
 grad2.C:27
 grad2.C:28
 grad2.C:29
 grad2.C:30
 grad2.C:31
 grad2.C:32
 grad2.C:33
 grad2.C:34
 grad2.C:35
 grad2.C:36
 grad2.C:37
 grad2.C:38
 grad2.C:39
 grad2.C:40
 grad2.C:41
 grad2.C:42
 grad2.C:43
 grad2.C:44
 grad2.C:45
 grad2.C:46
 grad2.C:47
 grad2.C:48
 grad2.C:49
 grad2.C:50
 grad2.C:51
 grad2.C:52
 grad2.C:53
 grad2.C:54
 grad2.C:55
 grad2.C:56
 grad2.C:57
 grad2.C:58
 grad2.C:59
 grad2.C:60
 grad2.C:61
 grad2.C:62
 grad2.C:63
 grad2.C:64
 grad2.C:65