From $ROOTSYS/tutorials/gl/grad.C

//Author: Timur Pocheptsov, 19/03/2014.
//This macro demonstrates how to create and use linear gradients to fill
//a histogram or a pad.
//To use this macro you need OpenGL enabled in pad:
//either set OpenGL.CanvasPreferGL to 1 in $ROOTSYS/etc/system.rootrc;
//or call gStyle->SetCanvasPreferGL(kTRUE); before canvas created.

//Includes for ACLiC (cling does not need them).
#include "TColorGradient.h"
#include "TCanvas.h"
#include "TColor.h"
#include "TStyle.h"
#include "TError.h"
#include "TH1F.h"

//Aux. functions for tutorials/gl.
#include "customcolorgl.h"

//______________________________________________________________________
void grad()
{
   //1. Try to 'allocate' five 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 colorIndices[5] = {};
   if (ROOT::GLTutorials::FindFreeCustomColorIndices(colorIndices) != 5) {
      ::Error("grad", "failed to create new custom colors");
      return;
   }

   //Make sure we enabled OpenGL support in a canvas.
   gStyle->SetCanvasPreferGL(kTRUE);

   //2. Test if canvas supports OpenGL:
   TCanvas * const cnv = new TCanvas("gradient demo 1", "gradient demo 1", 100, 100, 600, 600);
   if (!cnv->UseGL()) {
      ::Error("grad", "This macro requires OpenGL");
      delete cnv;
      return;
   }

   typedef TColorGradient::Point Point;

   //3. Create custom colors:
   //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 Color_t &frameGradient = colorIndices[2];//This gradient is a mixture of colorIndices[0] and colorIndices[1]
   //Fill color for a pad frame:
   {
      new TColor(colorIndices[0], 0.25, 0.25, 0.25, "special pad color1", 0.55);
      new TColor(colorIndices[1], 1., 1., 1., "special pad color2", 0.05);

      //dark-white-white-dark:
      const Double_t locations[] = {0., 0.2, 0.8, 1.};
      const Color_t gradientIndices[4] = {colorIndices[0], colorIndices[1], colorIndices[1], colorIndices[0]};

      //Gradient for a pad's frame.
      TLinearGradient * const gradFill1 = new TLinearGradient(frameGradient, 4, locations, gradientIndices);
      //Horizontal:
      gradFill1->SetStartEnd(Point(0., 0.), Point(1., 0.));
   }

   //This gradient is a mixture of two standard colors.
   const Color_t &padGradient = colorIndices[3];
   //Fill color for a pad:
   {
      const Double_t locations[] = {0., 1.};
      const Color_t gradientIndices[4] = {30, 38};//We create a gradient from system colors.

      //Gradient for a pad.
      TLinearGradient * const gradFill2 = new TLinearGradient(padGradient, 2, locations, gradientIndices);
      //Vertical:
      gradFill2->SetStartEnd(Point(0., 0.), Point(0., 1.));
   }

   //Another gradient built from three standard colors.
   const Color_t &histGradient = colorIndices[4];
   //Fill color for a histogram:
   {
      const Color_t gradientIndices[3] = {kYellow, kOrange, kRed};
      const Double_t locations[3] = {0., 0.5, 1.};

      //Gradient for a histogram.
      TLinearGradient * const gradFill3 = new TLinearGradient(histGradient, 3, locations, gradientIndices);
      //Vertical:
      gradFill3->SetStartEnd(Point(0., 0.), Point(0., 1.));
   }

   cnv->SetFillColor(padGradient);
   cnv->SetFrameFillColor(frameGradient);

   TH1F * const hist = new TH1F("a1", "b1", 20, -3., 3.);
   hist->SetFillColor(histGradient);
   hist->FillRandom("gaus", 100000);
   hist->Draw();
}
 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