ROOT logo
// $Id: RGBAPalette.cxx 2694 2012-03-11 04:55:04Z matevz $

#include "RGBAPalette.h"
#include "RGBAPalette.c7"

#include <TColor.h>
#include <TMath.h>
#include <TROOT.h>
#include <TPRegexp.h>
#include <TStyle.h>
#include <TSystem.h>

#include <fstream>

//______________________________________________________________________
//
// Linear palette of RGBA colors for fast access via.
//
// Internal representation is std::vector<ZColorMark>. The float marks
// always go from 0 -> 1.
//
// This is expanded into mColorArray in steps from mMinInt to mMaxInt.
// ColorFromValue(Int_t val) makes direct access into this array.
// ColorFromValue(Float_t val) remaps the val

ClassImp(RGBAPalette);

void RGBAPalette::_init()
{
  mMinInt = 0; mMaxInt = 100;
  mMinFlt = 0; mMaxFlt = 1;

  bInterpolate     = true;    mShowDefValue    = true;
  mUnderflowAction = LA_Cut;  mOverflowAction  = LA_Clip;

  mDefaultColor.gray(0.5);    mCutColor.gray(0);
  mUnderColor.gray(0.9);      mOverColor.gray(0.1);

  mNBins = 0;
}

RGBAPalette::RGBAPalette(const Text_t* n, const Text_t* t) :
  ZGlass(n, t)
{
  _init();
}

RGBAPalette::~RGBAPalette()
{}

void RGBAPalette::ClearColorArray()
{
  mNBins = 0;
  mColorArray.clear();
}

/**************************************************************************/

void RGBAPalette::EmitRecolDCUORay()
{
  mDefaultColor.to_ubyte(mDefaultRGBA);
  mCutColor.to_ubyte(mCutRGBA);
  mUnderColor.to_ubyte(mUnderRGBA);
  mOverColor.to_ubyte(mOverRGBA);
}

/**************************************************************************/

void RGBAPalette::SetMinInt(Int_t min)
{
  mMinInt = min;
  if (mMaxInt <= min) mMaxInt = min + 1;
  ClearColorArray();
  Stamp(FID());
}

void RGBAPalette::SetMaxInt(Int_t max)
{
  mMaxInt = max;
  if (mMinInt >= max) mMinInt = max - 1;
  ClearColorArray();
  Stamp(FID());
}

/**************************************************************************/

void RGBAPalette::SetupColorArray() const
{
  mNBins = TMath::Max(mMaxInt-mMinInt+1, 1);
  mColorArray.resize(4 * mNBins);

  Float_t div = 1.0f / (mNBins - 1);

  UChar_t* p = &mColorArray[0];
  mColorMarks.front().to_ubyte(p); p += 4;
  vector<ZColorMark>::const_iterator a = mColorMarks.begin(), b = a;
  for(Int_t i=1; i<mNBins - 1; ++i, p+=4)
  {
    Float_t f = i*div;
    while (b->mark() < f) a = b++;
    Float_t bfrac = (f - a->mark()) / (b->mark() - a->mark());
    if (bInterpolate)
    {
      ZColor c = (1 - bfrac)*(*a) + bfrac*(*b);
      c.to_ubyte(p);
    }
    else
    {
      if (bfrac <= 0.5f) a->to_ubyte(p);
      else               b->to_ubyte(p);
    }
  }
  mColorMarks.back().to_ubyte(p);
}

void RGBAPalette::SetMarksFromgStyle()
{
  static const Exc_t _eh("RGBAPalette::SetMarksFromgStyle ");

  Int_t nc = gStyle->GetNumberOfColors();
  if (nc < 2)
    throw _eh + "number of colors < 2.";
  mColorMarks.resize(nc);

  Float_t m   = 0;
  Float_t div = 1.0f / (nc - 1);
  UChar_t c[4];
  for (Int_t i=0; i<nc; ++i, m+=div)
  {
    ColorFromIdx(gStyle->GetColorPalette(i), c, true);
    mColorMarks[i].rgba_from_ubyte(c);
    mColorMarks[i].mark(m);
  }
  mColorMarks.back().mark(1);

  ClearColorArray();
  StampReqTring();
}

void RGBAPalette::SetMarksFromPOVFile(const Text_t* file_name)
{
  // Import gradient from POV like definition as exported by gimp.

  static const Exc_t _eh("RGBAPalette::SetMarksFromPOVFile ");

  ifstream ifs(file_name, ifstream::in);
  if (ifs.fail())
    throw _eh + "failed opening file '" + file_name + "'.";

  mColorMarks.clear();

  TPMERegexp line_re("^\\s*\\[.*\\]\\s*$");
  TPMERegexp num_re("[\\d\\.]+", "g");

  while (!ifs.eof())
  {
    TString text; text.ReadLine(ifs);

    if (line_re.Match(text))
    {
      Float_t n[5];
      Int_t   i = 0;
      num_re.ResetGlobalState();
      while (num_re.Match(text) && i < 5)
      {
	n[i++] = num_re[0].Atof();
      }
      if (i == 5)
      {
	mColorMarks.push_back(ZColorMark(n[0], n[1], n[2], n[3], 1.0f - n[4]));
      }
    }
  }
  printf("%s%zu marks from '%s'.\n", _eh.Data(), mColorMarks.size(), file_name);

  ifs.close();

  ClearColorArray();
  StampReqTring();
}

void RGBAPalette::PrintMarks() const
{
  static const Exc_t _eh("RGBAPalette::PrintMarks ");

  Int_t n = mColorMarks.size();
  printf("%s [%s], n_marks=%d\n", _eh.Data(), Identify().Data(), n);
  for (Int_t i=0; i<n; ++i)
  {
    const ZColorMark& m = mColorMarks[i];
    printf("%2d %5.3f  rgba:%5.3f/%5.3f/%5.3f/%5.3f\n",
           i, m.m(), m.r(), m.g(), m.b(), m.a());
  }
}

void RGBAPalette::PrintArray() const
{
  static const Exc_t _eh("RGBAPalette::PrintArray ");

  if (mColorArray.empty())  SetupColorArray();
  printf("%s [%s], n_bins=%d\n", _eh.Data(), Identify().Data(), mNBins);
  UChar_t* p = &mColorArray[0];
  for (Int_t i=0; i<mNBins; ++i, p+=4)
  {
    printf("%3d rgba:%02hhx/%02hhx/%02hhx/%02hhx\n",
           i, p[0], p[1], p[2], p[3]);
  }
}

/**************************************************************************/
/**************************************************************************/

// Color from ROOT TColor

void RGBAPalette::ColorFromIdx(Short_t ci, UChar_t* col, Bool_t alpha)
{
  if (ci < 0)
  {
    col[0] = col[1] = col[2] = col[3] = 0;
    return;
  }
  TColor* c = gROOT->GetColor(ci);
  if(c)
  {
    col[0] = (UChar_t)(255*c->GetRed());
    col[1] = (UChar_t)(255*c->GetGreen());
    col[2] = (UChar_t)(255*c->GetBlue());
    if (alpha) col[3] = 255;
  }
}

void RGBAPalette::ColorFromIdx(Float_t f1, Short_t c1, Float_t f2, Short_t c2,
                               UChar_t* col, Bool_t alpha)
{
  TColor* t1 = gROOT->GetColor(c1);
  TColor* t2 = gROOT->GetColor(c2);
  if(t1 && t2)
  {
    col[0] = (UChar_t)(255*(f1*t1->GetRed()   + f2*t2->GetRed()));
    col[1] = (UChar_t)(255*(f1*t1->GetGreen() + f2*t2->GetGreen()));
    col[2] = (UChar_t)(255*(f1*t1->GetBlue()  + f2*t2->GetBlue()));
    if (alpha) col[3] = 255;
  }
}
 RGBAPalette.cxx:1
 RGBAPalette.cxx:2
 RGBAPalette.cxx:3
 RGBAPalette.cxx:4
 RGBAPalette.cxx:5
 RGBAPalette.cxx:6
 RGBAPalette.cxx:7
 RGBAPalette.cxx:8
 RGBAPalette.cxx:9
 RGBAPalette.cxx:10
 RGBAPalette.cxx:11
 RGBAPalette.cxx:12
 RGBAPalette.cxx:13
 RGBAPalette.cxx:14
 RGBAPalette.cxx:15
 RGBAPalette.cxx:16
 RGBAPalette.cxx:17
 RGBAPalette.cxx:18
 RGBAPalette.cxx:19
 RGBAPalette.cxx:20
 RGBAPalette.cxx:21
 RGBAPalette.cxx:22
 RGBAPalette.cxx:23
 RGBAPalette.cxx:24
 RGBAPalette.cxx:25
 RGBAPalette.cxx:26
 RGBAPalette.cxx:27
 RGBAPalette.cxx:28
 RGBAPalette.cxx:29
 RGBAPalette.cxx:30
 RGBAPalette.cxx:31
 RGBAPalette.cxx:32
 RGBAPalette.cxx:33
 RGBAPalette.cxx:34
 RGBAPalette.cxx:35
 RGBAPalette.cxx:36
 RGBAPalette.cxx:37
 RGBAPalette.cxx:38
 RGBAPalette.cxx:39
 RGBAPalette.cxx:40
 RGBAPalette.cxx:41
 RGBAPalette.cxx:42
 RGBAPalette.cxx:43
 RGBAPalette.cxx:44
 RGBAPalette.cxx:45
 RGBAPalette.cxx:46
 RGBAPalette.cxx:47
 RGBAPalette.cxx:48
 RGBAPalette.cxx:49
 RGBAPalette.cxx:50
 RGBAPalette.cxx:51
 RGBAPalette.cxx:52
 RGBAPalette.cxx:53
 RGBAPalette.cxx:54
 RGBAPalette.cxx:55
 RGBAPalette.cxx:56
 RGBAPalette.cxx:57
 RGBAPalette.cxx:58
 RGBAPalette.cxx:59
 RGBAPalette.cxx:60
 RGBAPalette.cxx:61
 RGBAPalette.cxx:62
 RGBAPalette.cxx:63
 RGBAPalette.cxx:64
 RGBAPalette.cxx:65
 RGBAPalette.cxx:66
 RGBAPalette.cxx:67
 RGBAPalette.cxx:68
 RGBAPalette.cxx:69
 RGBAPalette.cxx:70
 RGBAPalette.cxx:71
 RGBAPalette.cxx:72
 RGBAPalette.cxx:73
 RGBAPalette.cxx:74
 RGBAPalette.cxx:75
 RGBAPalette.cxx:76
 RGBAPalette.cxx:77
 RGBAPalette.cxx:78
 RGBAPalette.cxx:79
 RGBAPalette.cxx:80
 RGBAPalette.cxx:81
 RGBAPalette.cxx:82
 RGBAPalette.cxx:83
 RGBAPalette.cxx:84
 RGBAPalette.cxx:85
 RGBAPalette.cxx:86
 RGBAPalette.cxx:87
 RGBAPalette.cxx:88
 RGBAPalette.cxx:89
 RGBAPalette.cxx:90
 RGBAPalette.cxx:91
 RGBAPalette.cxx:92
 RGBAPalette.cxx:93
 RGBAPalette.cxx:94
 RGBAPalette.cxx:95
 RGBAPalette.cxx:96
 RGBAPalette.cxx:97
 RGBAPalette.cxx:98
 RGBAPalette.cxx:99
 RGBAPalette.cxx:100
 RGBAPalette.cxx:101
 RGBAPalette.cxx:102
 RGBAPalette.cxx:103
 RGBAPalette.cxx:104
 RGBAPalette.cxx:105
 RGBAPalette.cxx:106
 RGBAPalette.cxx:107
 RGBAPalette.cxx:108
 RGBAPalette.cxx:109
 RGBAPalette.cxx:110
 RGBAPalette.cxx:111
 RGBAPalette.cxx:112
 RGBAPalette.cxx:113
 RGBAPalette.cxx:114
 RGBAPalette.cxx:115
 RGBAPalette.cxx:116
 RGBAPalette.cxx:117
 RGBAPalette.cxx:118
 RGBAPalette.cxx:119
 RGBAPalette.cxx:120
 RGBAPalette.cxx:121
 RGBAPalette.cxx:122
 RGBAPalette.cxx:123
 RGBAPalette.cxx:124
 RGBAPalette.cxx:125
 RGBAPalette.cxx:126
 RGBAPalette.cxx:127
 RGBAPalette.cxx:128
 RGBAPalette.cxx:129
 RGBAPalette.cxx:130
 RGBAPalette.cxx:131
 RGBAPalette.cxx:132
 RGBAPalette.cxx:133
 RGBAPalette.cxx:134
 RGBAPalette.cxx:135
 RGBAPalette.cxx:136
 RGBAPalette.cxx:137
 RGBAPalette.cxx:138
 RGBAPalette.cxx:139
 RGBAPalette.cxx:140
 RGBAPalette.cxx:141
 RGBAPalette.cxx:142
 RGBAPalette.cxx:143
 RGBAPalette.cxx:144
 RGBAPalette.cxx:145
 RGBAPalette.cxx:146
 RGBAPalette.cxx:147
 RGBAPalette.cxx:148
 RGBAPalette.cxx:149
 RGBAPalette.cxx:150
 RGBAPalette.cxx:151
 RGBAPalette.cxx:152
 RGBAPalette.cxx:153
 RGBAPalette.cxx:154
 RGBAPalette.cxx:155
 RGBAPalette.cxx:156
 RGBAPalette.cxx:157
 RGBAPalette.cxx:158
 RGBAPalette.cxx:159
 RGBAPalette.cxx:160
 RGBAPalette.cxx:161
 RGBAPalette.cxx:162
 RGBAPalette.cxx:163
 RGBAPalette.cxx:164
 RGBAPalette.cxx:165
 RGBAPalette.cxx:166
 RGBAPalette.cxx:167
 RGBAPalette.cxx:168
 RGBAPalette.cxx:169
 RGBAPalette.cxx:170
 RGBAPalette.cxx:171
 RGBAPalette.cxx:172
 RGBAPalette.cxx:173
 RGBAPalette.cxx:174
 RGBAPalette.cxx:175
 RGBAPalette.cxx:176
 RGBAPalette.cxx:177
 RGBAPalette.cxx:178
 RGBAPalette.cxx:179
 RGBAPalette.cxx:180
 RGBAPalette.cxx:181
 RGBAPalette.cxx:182
 RGBAPalette.cxx:183
 RGBAPalette.cxx:184
 RGBAPalette.cxx:185
 RGBAPalette.cxx:186
 RGBAPalette.cxx:187
 RGBAPalette.cxx:188
 RGBAPalette.cxx:189
 RGBAPalette.cxx:190
 RGBAPalette.cxx:191
 RGBAPalette.cxx:192
 RGBAPalette.cxx:193
 RGBAPalette.cxx:194
 RGBAPalette.cxx:195
 RGBAPalette.cxx:196
 RGBAPalette.cxx:197
 RGBAPalette.cxx:198
 RGBAPalette.cxx:199
 RGBAPalette.cxx:200
 RGBAPalette.cxx:201
 RGBAPalette.cxx:202
 RGBAPalette.cxx:203
 RGBAPalette.cxx:204
 RGBAPalette.cxx:205
 RGBAPalette.cxx:206
 RGBAPalette.cxx:207
 RGBAPalette.cxx:208
 RGBAPalette.cxx:209
 RGBAPalette.cxx:210
 RGBAPalette.cxx:211
 RGBAPalette.cxx:212
 RGBAPalette.cxx:213
 RGBAPalette.cxx:214
 RGBAPalette.cxx:215
 RGBAPalette.cxx:216
 RGBAPalette.cxx:217
 RGBAPalette.cxx:218
 RGBAPalette.cxx:219
 RGBAPalette.cxx:220
 RGBAPalette.cxx:221
 RGBAPalette.cxx:222
 RGBAPalette.cxx:223
 RGBAPalette.cxx:224
 RGBAPalette.cxx:225
 RGBAPalette.cxx:226
 RGBAPalette.cxx:227
 RGBAPalette.cxx:228
 RGBAPalette.cxx:229
 RGBAPalette.cxx:230
 RGBAPalette.cxx:231
 RGBAPalette.cxx:232
 RGBAPalette.cxx:233
 RGBAPalette.cxx:234
 RGBAPalette.cxx:235
 RGBAPalette.cxx:236
 RGBAPalette.cxx:237
 RGBAPalette.cxx:238
 RGBAPalette.cxx:239
 RGBAPalette.cxx:240
 RGBAPalette.cxx:241
 RGBAPalette.cxx:242
 RGBAPalette.cxx:243
 RGBAPalette.cxx:244