ROOT logo

From $ROOTSYS/tutorials/gl/grad.C

//Author: Timur Pocheptsov, 19/03/2014
//This macro shows how to create and use linear gradients to fill
//a histogram or a pad.
//Requires OpenGL: you either have to use gStyle->SetCanvasPreferGL(kTRUE)
//or set OpenGL.CanvasPreferGL to 1 in a $ROOTSYS/etc/system.rootrc.

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

//Aux. functions:
#include "customcolors.h"

typedef TColorGradient::Point point_type;

//______________________________________________________________________
Color_t create_pad_frame_gradient()
{
   //We create a gradient with 4 steps - from dark (and semi-transparent)
   //gray to almost transparent (95%) white and to white and to dark gray again.
   Color_t idx[3] = {};
   if (FindFreeCustomColorIndices(3, idx) != 3)
      return -1;

   const Double_t locations[] = {0., 0.2, 0.8, 1.};
   
   new TColor(idx[0], 0.25, 0.25, 0.25, "special pad color1", 0.55);
   new TColor(idx[1], 1., 1., 1., "special pad color2", 0.05);
   Color_t colorIndices[4] = {idx[0], idx[1], idx[1], idx[0]};
   TLinearGradient * const grad = new TLinearGradient(idx[2], 4, locations, colorIndices);
   const point_type start(0., 0.);
   const point_type end(1., 0.);
   grad->SetStartEnd(start, end);
   
   return idx[2];
}

//______________________________________________________________________
Color_t create_pad_gradient()
{
   //We create two-steps gradient from ROOT's standard colors (38 and 30).
   const Color_t idx = FindFreeCustomColorIndex(1000);//Start lookup from 1000.
   if (idx == -1)
      return -1;

   const Double_t locations[] = {0., 1.};
   const Color_t colorIndices[2] = {30, 38};
   
   TLinearGradient * const grad = new TLinearGradient(idx, 2, locations, colorIndices);
   const point_type start(0., 0.);
   const point_type end(0., 1.);
   grad->SetStartEnd(start, end);
   
   return idx;
}

//______________________________________________________________________
void grad()
{
   const Color_t frameColor = create_pad_frame_gradient();
   if (frameColor == -1) {
      ::Error("grad", "failed to allocate a custom color");
      return;
   }
   
   const Color_t padColor = create_pad_gradient();
   if (padColor == -1) {
      ::Error("grad", "failed to allocate a custom color");
      return;//:( no way to cleanup palette now.
   }

   const Color_t histFill = FindFreeCustomColorIndex(padColor + 1);//Start lookup from the next.
   if (histFill == -1) {
      ::Error("grad", "failed to allocate a custom color");
      return;
   }

   gStyle->SetCanvasPreferGL(kTRUE);

   TCanvas * const cnv = new TCanvas("gradient test", "gradient test", 100, 100, 600, 600);
   if (!cnv->UseGL()) {
      ::Error("grad", "This macro requires OpenGL");
      delete cnv;
      return;
   }
   
   cnv->SetFillColor(padColor);
   cnv->SetFrameFillColor(frameColor);

   //Gradient to fill a histogramm
   const Color_t colorIndices[3] = {kYellow, kOrange, kRed};
   const Double_t lengths[3] = {0., 0.5, 1.};
   TLinearGradient * const grad = new TLinearGradient(histFill, 3, lengths, colorIndices);
   grad->SetStartEnd(point_type(0., 0.), point_type(0., 1.));
   
   TH1F * const hist = new TH1F("h11", "h11", 20, -3., 3.);
   hist->SetFillColor(histFill);
   hist->FillRandom("gaus", 100000);
   hist->Draw();

   cnv->Update();
}
 grad.C:1
 grad.C:2
 grad.C:3
 grad.C:4
 grad.C:5
 grad.C:6
 grad.C:7
 grad.C:8
 grad.C:9
 grad.C:10
 grad.C:11
 grad.C:12
 grad.C:13
 grad.C:14
 grad.C:15
 grad.C:16
 grad.C:17
 grad.C:18
 grad.C:19
 grad.C:20
 grad.C:21
 grad.C:22
 grad.C:23
 grad.C:24
 grad.C:25
 grad.C:26
 grad.C:27
 grad.C:28
 grad.C:29
 grad.C:30
 grad.C:31
 grad.C:32
 grad.C:33
 grad.C:34
 grad.C:35
 grad.C:36
 grad.C:37
 grad.C:38
 grad.C:39
 grad.C:40
 grad.C:41
 grad.C:42
 grad.C:43
 grad.C:44
 grad.C:45
 grad.C:46
 grad.C:47
 grad.C:48
 grad.C:49
 grad.C:50
 grad.C:51
 grad.C:52
 grad.C:53
 grad.C:54
 grad.C:55
 grad.C:56
 grad.C:57
 grad.C:58
 grad.C:59
 grad.C:60
 grad.C:61
 grad.C:62
 grad.C:63
 grad.C:64
 grad.C:65
 grad.C:66
 grad.C:67
 grad.C:68
 grad.C:69
 grad.C:70
 grad.C:71
 grad.C:72
 grad.C:73
 grad.C:74
 grad.C:75
 grad.C:76
 grad.C:77
 grad.C:78
 grad.C:79
 grad.C:80
 grad.C:81
 grad.C:82
 grad.C:83
 grad.C:84
 grad.C:85
 grad.C:86
 grad.C:87
 grad.C:88
 grad.C:89
 grad.C:90
 grad.C:91
 grad.C:92
 grad.C:93
 grad.C:94
 grad.C:95
 grad.C:96
 grad.C:97
 grad.C:98
 grad.C:99
 grad.C:100
 grad.C:101
 grad.C:102
 grad.C:103
 grad.C:104
 grad.C:105
 grad.C:106