// @(#)root/base:$Id$
//Author: Timur Pocheptsov   20/03/2012

/*************************************************************************
 * Copyright (C) 1995-2012, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TColorGradient
#define ROOT_TColorGradient


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TColorGradient                                                       //
//                                                                      //
// TColorGradient extends basic TColor.                                 //
// Actually, this is not a simple color, but linear or radial gradient  //
// for a filled area. By inheriting from TColor, gradients can be       //
// placed inside gROOT's list of colors and use it in all TAttXXX       //
// descendants without modifying any existing code.                     //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include <vector>

#ifndef ROOT_Rtypes
#include "Rtypes.h"
#endif

#ifndef ROOT_TColor
#include "TColor.h"
#endif


class TColorGradient : public TColor {
public:
   typedef std::vector<Color_t>::size_type SizeType_t;

   //TODO: Replace with enum class as soon as we have C++11 enabled by default.
   //CoordinateMode: both linear and radial gradients require some points - the
   //start and end points.
   //We can use either pad's rectangle as a coordinate system
   //or an object's bounding rect.
   enum ECoordinateMode {
      kPadMode,//NDC, in a pad's rectangle (pad is 0,0 - 1,1).
      kObjectBoundingMode //NDC in an object's bounding rect (this rect is 0,0 - 1, 1).
   };
   
   struct Point {
      Double_t fX;
      Double_t fY;
      
      Point()
         : fX(0.), fY(0.)
      {
      }
      
      Point(Double_t x, Double_t y)
         : fX(x), fY(y)
      {
      }
   };

private:
   //Positions of color nodes in a gradient, in NDC.
   std::vector<Double_t> fColorPositions;
   std::vector<Double_t> fColors;//RGBA values.

   //'default value' is kObjectBoundingMode.
   ECoordinateMode fCoordinateMode;

protected:
   TColorGradient();

   TColorGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
                  const Color_t *colorIndices, ECoordinateMode mode = kObjectBoundingMode);
   TColorGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
                  const Double_t *colors, ECoordinateMode mode = kObjectBoundingMode);

public:
   void ResetColor(UInt_t nPoints, const Double_t *points,
                   const Color_t *colorIndices);
   void ResetColor(UInt_t nPoints, const Double_t *points,
                   const Double_t *colorIndices);

   void SetCoordinateMode(ECoordinateMode mode);
   ECoordinateMode GetCoordinateMode()const;

   SizeType_t GetNumberOfSteps()const;
   const Double_t *GetColorPositions()const;
   const Double_t *GetColors()const;
   
private:
   void RegisterColor(Color_t colorIndex);
   
   ClassDef(TColorGradient, 1) //Gradient fill.
};

class TLinearGradient : public TColorGradient {
public:
   //With C++11 we'll use inherited constructors!!!
   TLinearGradient();
   TLinearGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
                   const Color_t *colorIndices, ECoordinateMode mode = kObjectBoundingMode);
   TLinearGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
                   const Double_t *colors, ECoordinateMode mode = kObjectBoundingMode);
   
   //points are always in NDC (and also affected by fCoordinateMode).
   void SetStartEnd(const Point &p1, const Point &p2);
   const Point &GetStart()const;
   const Point &GetEnd()const;

private:
   Point fStart;
   Point fEnd;
   
   ClassDef(TLinearGradient, 1)//Linear gradient fill.
};

//
//Radial gradient. Can be either "simple": you specify a center
//and radius in NDC coordinates (see comments about linear gradient
//and coordinate modes above), or "extended": you have two centers
//(start,end) and two radiuses (R1, R2) and interpolation between them;
//still start/end and radiuses are in NDC.
//

class TRadialGradient : public TColorGradient {
public:
   enum EGradientType {
      kSimple,
      kExtended
   };


   //With C++11 we'll use inherited constructors!!!
   TRadialGradient();
   TRadialGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
                   const Color_t *colorIndices, ECoordinateMode mode = kObjectBoundingMode);
   TRadialGradient(Color_t newColor, UInt_t nPoints, const Double_t *points,
                   const Double_t *colors, ECoordinateMode mode = kObjectBoundingMode);


   EGradientType GetGradientType()const;


   //Extended gradient.
   void SetStartEndR1R2(const Point &p1, Double_t r1,
                        const Point &p2, Double_t r2);
   const Point &GetStart()const;
   Double_t GetR1()const;
   const Point &GetEnd()const;
   Double_t GetR2()const;

   //Simple radial gradient: the same as extended with
   //start == end, r1 = 0, r2 = radius.
   void SetRadialGradient(const Point &center, Double_t radius);
   const Point &GetCenter()const;
   Double_t GetRadius()const;

private:
   Point fStart;
   Double_t fR1 = 0.;
   Point fEnd;
   Double_t fR2 = 0.;
   
   EGradientType fType = kSimple;
   
   ClassDef(TRadialGradient, 1)//Radial gradient fill.
};


#endif
 TColorGradient.h:1
 TColorGradient.h:2
 TColorGradient.h:3
 TColorGradient.h:4
 TColorGradient.h:5
 TColorGradient.h:6
 TColorGradient.h:7
 TColorGradient.h:8
 TColorGradient.h:9
 TColorGradient.h:10
 TColorGradient.h:11
 TColorGradient.h:12
 TColorGradient.h:13
 TColorGradient.h:14
 TColorGradient.h:15
 TColorGradient.h:16
 TColorGradient.h:17
 TColorGradient.h:18
 TColorGradient.h:19
 TColorGradient.h:20
 TColorGradient.h:21
 TColorGradient.h:22
 TColorGradient.h:23
 TColorGradient.h:24
 TColorGradient.h:25
 TColorGradient.h:26
 TColorGradient.h:27
 TColorGradient.h:28
 TColorGradient.h:29
 TColorGradient.h:30
 TColorGradient.h:31
 TColorGradient.h:32
 TColorGradient.h:33
 TColorGradient.h:34
 TColorGradient.h:35
 TColorGradient.h:36
 TColorGradient.h:37
 TColorGradient.h:38
 TColorGradient.h:39
 TColorGradient.h:40
 TColorGradient.h:41
 TColorGradient.h:42
 TColorGradient.h:43
 TColorGradient.h:44
 TColorGradient.h:45
 TColorGradient.h:46
 TColorGradient.h:47
 TColorGradient.h:48
 TColorGradient.h:49
 TColorGradient.h:50
 TColorGradient.h:51
 TColorGradient.h:52
 TColorGradient.h:53
 TColorGradient.h:54
 TColorGradient.h:55
 TColorGradient.h:56
 TColorGradient.h:57
 TColorGradient.h:58
 TColorGradient.h:59
 TColorGradient.h:60
 TColorGradient.h:61
 TColorGradient.h:62
 TColorGradient.h:63
 TColorGradient.h:64
 TColorGradient.h:65
 TColorGradient.h:66
 TColorGradient.h:67
 TColorGradient.h:68
 TColorGradient.h:69
 TColorGradient.h:70
 TColorGradient.h:71
 TColorGradient.h:72
 TColorGradient.h:73
 TColorGradient.h:74
 TColorGradient.h:75
 TColorGradient.h:76
 TColorGradient.h:77
 TColorGradient.h:78
 TColorGradient.h:79
 TColorGradient.h:80
 TColorGradient.h:81
 TColorGradient.h:82
 TColorGradient.h:83
 TColorGradient.h:84
 TColorGradient.h:85
 TColorGradient.h:86
 TColorGradient.h:87
 TColorGradient.h:88
 TColorGradient.h:89
 TColorGradient.h:90
 TColorGradient.h:91
 TColorGradient.h:92
 TColorGradient.h:93
 TColorGradient.h:94
 TColorGradient.h:95
 TColorGradient.h:96
 TColorGradient.h:97
 TColorGradient.h:98
 TColorGradient.h:99
 TColorGradient.h:100
 TColorGradient.h:101
 TColorGradient.h:102
 TColorGradient.h:103
 TColorGradient.h:104
 TColorGradient.h:105
 TColorGradient.h:106
 TColorGradient.h:107
 TColorGradient.h:108
 TColorGradient.h:109
 TColorGradient.h:110
 TColorGradient.h:111
 TColorGradient.h:112
 TColorGradient.h:113
 TColorGradient.h:114
 TColorGradient.h:115
 TColorGradient.h:116
 TColorGradient.h:117
 TColorGradient.h:118
 TColorGradient.h:119
 TColorGradient.h:120
 TColorGradient.h:121
 TColorGradient.h:122
 TColorGradient.h:123
 TColorGradient.h:124
 TColorGradient.h:125
 TColorGradient.h:126
 TColorGradient.h:127
 TColorGradient.h:128
 TColorGradient.h:129
 TColorGradient.h:130
 TColorGradient.h:131
 TColorGradient.h:132
 TColorGradient.h:133
 TColorGradient.h:134
 TColorGradient.h:135
 TColorGradient.h:136
 TColorGradient.h:137
 TColorGradient.h:138
 TColorGradient.h:139
 TColorGradient.h:140
 TColorGradient.h:141
 TColorGradient.h:142
 TColorGradient.h:143
 TColorGradient.h:144
 TColorGradient.h:145
 TColorGradient.h:146
 TColorGradient.h:147
 TColorGradient.h:148
 TColorGradient.h:149
 TColorGradient.h:150
 TColorGradient.h:151
 TColorGradient.h:152
 TColorGradient.h:153
 TColorGradient.h:154
 TColorGradient.h:155
 TColorGradient.h:156
 TColorGradient.h:157
 TColorGradient.h:158
 TColorGradient.h:159
 TColorGradient.h:160
 TColorGradient.h:161
 TColorGradient.h:162
 TColorGradient.h:163
 TColorGradient.h:164
 TColorGradient.h:165
 TColorGradient.h:166
 TColorGradient.h:167
 TColorGradient.h:168
 TColorGradient.h:169
 TColorGradient.h:170
 TColorGradient.h:171
 TColorGradient.h:172
 TColorGradient.h:173
 TColorGradient.h:174
 TColorGradient.h:175
 TColorGradient.h:176
 TColorGradient.h:177