TEveRGBAPalette.cxx

Go to the documentation of this file.
00001 // @(#)root/eve:$Id: TEveRGBAPalette.cxx 33864 2010-06-14 09:47:19Z matevz $
00002 // Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "TEveRGBAPalette.h"
00013 
00014 #include "TColor.h"
00015 #include "TStyle.h"
00016 #include "TMath.h"
00017 
00018 //______________________________________________________________________________
00019 //
00020 // A generic, speed-optimised mapping from value to RGBA color
00021 // supporting different wrapping and range truncation modes.
00022 //
00023 // Flag fFixColorRange: specifies how the palette is mapped to signal values:
00024 //  true  - LowLimit -> HighLimit
00025 //  false - MinValue -> MaxValue
00026 
00027 
00028 ClassImp(TEveRGBAPalette);
00029 
00030 //______________________________________________________________________________
00031 TEveRGBAPalette::TEveRGBAPalette() :
00032    TObject(), TQObject(),
00033    TEveRefCnt(),
00034 
00035    fLowLimit(0), fHighLimit(0), fMinVal(0), fMaxVal(0),
00036 
00037    fInterpolate     (kTRUE),
00038    fShowDefValue    (kTRUE),
00039    fFixColorRange   (kFALSE),
00040    fUnderflowAction (kLA_Cut),
00041    fOverflowAction  (kLA_Clip),
00042 
00043    fDefaultColor(-1),
00044    fUnderColor  (-1),
00045    fOverColor   (-1),
00046 
00047    fNBins(0), fCAMin(0), fCAMax(0), fColorArray(0)
00048 {
00049    // Constructor.
00050 
00051    SetLimits(0, 1024);
00052    SetMinMax(0,  512);
00053 
00054    SetDefaultColor(0);
00055    SetUnderColor(1);
00056    SetOverColor(2);
00057 }
00058 
00059 //______________________________________________________________________________
00060 TEveRGBAPalette::TEveRGBAPalette(Int_t min, Int_t max, Bool_t interp,
00061                                  Bool_t showdef, Bool_t fixcolrng) :
00062    TObject(), TQObject(),
00063    TEveRefCnt(),
00064 
00065    fLowLimit(0), fHighLimit(0), fMinVal(0), fMaxVal(0),
00066 
00067    fInterpolate     (interp),
00068    fShowDefValue    (showdef),
00069    fFixColorRange   (fixcolrng),
00070    fUnderflowAction (kLA_Cut),
00071    fOverflowAction  (kLA_Clip),
00072 
00073    fDefaultColor(-1),
00074    fUnderColor  (-1),
00075    fOverColor   (-1),
00076 
00077    fNBins(0), fCAMin(0), fCAMax(0), fColorArray(0)
00078 {
00079    // Constructor.
00080 
00081    SetLimits(min, max);
00082    SetMinMax(min, max);
00083 
00084    SetDefaultColor(0);
00085    SetUnderColor(1);
00086    SetOverColor(2);
00087 }
00088 
00089 //______________________________________________________________________________
00090 TEveRGBAPalette::~TEveRGBAPalette()
00091 {
00092    // Destructor.
00093 
00094    delete [] fColorArray;
00095 }
00096 
00097 /******************************************************************************/
00098 
00099 //______________________________________________________________________________
00100 void TEveRGBAPalette::SetupColor(Int_t val, UChar_t* pixel) const
00101 {
00102    // Set RGBA color 'pixel' for signal-value 'val'.
00103 
00104    using namespace TMath;
00105    Float_t div  = Max(1, fCAMax - fCAMin);
00106    Int_t   nCol = gStyle->GetNumberOfColors();
00107 
00108    Float_t f;
00109    if      (val >= fCAMax) f = nCol - 1;
00110    else if (val <= fCAMin) f = 0;
00111    else                     f = (val - fCAMin)/div*(nCol - 1);
00112 
00113    if (fInterpolate) {
00114       Int_t  bin = (Int_t) f;
00115       Float_t f1 = f - bin, f2 = 1.0f - f1;
00116       TEveUtil::ColorFromIdx(f1, gStyle->GetColorPalette(bin),
00117                              f2, gStyle->GetColorPalette(Min(bin + 1, nCol - 1)),
00118                              pixel);
00119    } else {
00120       TEveUtil::ColorFromIdx(gStyle->GetColorPalette((Int_t) Nint(f)), pixel);
00121    }
00122 }
00123 
00124 //______________________________________________________________________________
00125 void TEveRGBAPalette::SetupColorArray() const
00126 {
00127    // Construct internal color array that maps signal value to RGBA color.
00128 
00129    if (fColorArray)
00130       delete [] fColorArray;
00131 
00132    if (fFixColorRange) {
00133       fCAMin = fLowLimit; fCAMax = fHighLimit;
00134    } else {
00135       fCAMin = fMinVal;   fCAMax = fMaxVal;
00136    }
00137    fNBins = fCAMax - fCAMin + 1;
00138 
00139    fColorArray = new UChar_t [4 * fNBins];
00140    UChar_t* p = fColorArray;
00141    for(Int_t v = fCAMin; v <= fCAMax; ++v, p+=4)
00142       SetupColor(v, p);
00143 }
00144 
00145 //______________________________________________________________________________
00146 void TEveRGBAPalette::ClearColorArray()
00147 {
00148    // Clear internal color array.
00149 
00150    if (fColorArray) {
00151       delete [] fColorArray;
00152       fColorArray = 0;
00153       fNBins = fCAMin = fCAMax = 0;
00154    }
00155 }
00156 
00157 /******************************************************************************/
00158 
00159 //______________________________________________________________________________
00160 void TEveRGBAPalette::SetLimits(Int_t low, Int_t high)
00161 {
00162    // Set low/high limits on signal value. Current min/max values are
00163    // clamped into the new limits.
00164 
00165    fLowLimit  = low;
00166    fHighLimit = high;
00167 
00168    if (fMaxVal < fLowLimit)  SetMax(fLowLimit);
00169    if (fMinVal < fLowLimit)  SetMin(fLowLimit);
00170    if (fMinVal > fHighLimit) SetMin(fHighLimit);
00171    if (fMaxVal > fHighLimit) SetMax(fHighLimit);
00172 
00173    ClearColorArray();
00174 }
00175 
00176 //______________________________________________________________________________
00177 void TEveRGBAPalette::SetLimitsScaleMinMax(Int_t low, Int_t high)
00178 {
00179    // Set low/high limits and rescale current min/max values.
00180 
00181    Float_t rng_old = fHighLimit - fLowLimit;
00182    Float_t rng_new = high - low;
00183 
00184    fMinVal = TMath::Nint(low + (fMinVal - fLowLimit)*rng_new/rng_old);
00185    fMaxVal = TMath::Nint(low + (fMaxVal - fLowLimit)*rng_new/rng_old);
00186    fLowLimit  = low;
00187    fHighLimit = high;
00188 
00189    ClearColorArray();
00190 }
00191 
00192 //______________________________________________________________________________
00193 void TEveRGBAPalette::SetMin(Int_t min)
00194 {
00195    // Set current min value.
00196 
00197    fMinVal = TMath::Min(min, fMaxVal);
00198    ClearColorArray();
00199 }
00200 
00201 //______________________________________________________________________________
00202 void TEveRGBAPalette::SetMax(Int_t max)
00203 {
00204    // Set current max value.
00205 
00206    fMaxVal = TMath::Max(max, fMinVal);
00207    ClearColorArray();
00208 }
00209 
00210 //______________________________________________________________________________
00211 void TEveRGBAPalette::SetMinMax(Int_t min, Int_t max)
00212 {
00213    // Set current min/max values.
00214 
00215    fMinVal = min;
00216    fMaxVal = max;
00217    ClearColorArray();
00218 }
00219 
00220 /******************************************************************************/
00221 
00222 //______________________________________________________________________________
00223 void TEveRGBAPalette::SetInterpolate(Bool_t b)
00224 {
00225    // Set interpolation flag. This determines how colors from ROOT's
00226    // palette are mapped into RGBA values for given signal.
00227 
00228    fInterpolate = b;
00229    ClearColorArray();
00230 }
00231 
00232 //______________________________________________________________________________
00233 void TEveRGBAPalette::SetFixColorRange(Bool_t v)
00234 {
00235    // Set flag specifying how the palette is mapped to signal values:
00236    //  true  - LowLimit -> HighLimit
00237    //  false - MinValue -> MaxValue
00238 
00239    fFixColorRange = v;
00240    ClearColorArray();
00241 }
00242 
00243 /******************************************************************************/
00244 
00245 //______________________________________________________________________________
00246 void TEveRGBAPalette::SetDefaultColor(Color_t ci)
00247 {
00248    // Set default color.
00249 
00250    fDefaultColor = ci;
00251    TEveUtil::ColorFromIdx(ci, fDefaultRGBA, kTRUE);
00252 }
00253 
00254 //______________________________________________________________________________
00255 void TEveRGBAPalette::SetDefaultColorPixel(Pixel_t pix)
00256 {
00257    // Set default color.
00258 
00259    SetDefaultColor(Color_t(TColor::GetColor(pix)));
00260 }
00261 
00262 //______________________________________________________________________________
00263 void TEveRGBAPalette::SetDefaultColorRGBA(UChar_t r, UChar_t g, UChar_t b, UChar_t a)
00264 {
00265    // Set default color.
00266 
00267    fDefaultColor = Color_t(TColor::GetColor(r, g, b));
00268    fDefaultRGBA[0] = r;
00269    fDefaultRGBA[1] = g;
00270    fDefaultRGBA[2] = b;
00271    fDefaultRGBA[3] = a;
00272 }
00273 
00274 /******************************************************************************/
00275 
00276 //______________________________________________________________________________
00277 void TEveRGBAPalette::SetUnderColor(Color_t ci)
00278 {
00279    // Set underflow color.
00280 
00281    fUnderColor = ci;
00282    TEveUtil::ColorFromIdx(ci, fUnderRGBA, kTRUE);
00283 }
00284 
00285 //______________________________________________________________________________
00286 void TEveRGBAPalette::SetUnderColorPixel(Pixel_t pix)
00287 {
00288    // Set underflow color.
00289 
00290    SetUnderColor(Color_t(TColor::GetColor(pix)));
00291 }
00292 
00293 //______________________________________________________________________________
00294 void TEveRGBAPalette::SetUnderColorRGBA(UChar_t r, UChar_t g, UChar_t b, UChar_t a)
00295 {
00296    // Set underflow color.
00297 
00298    fUnderColor = Color_t(TColor::GetColor(r, g, b));
00299    fUnderRGBA[0] = r;
00300    fUnderRGBA[1] = g;
00301    fUnderRGBA[2] = b;
00302    fUnderRGBA[3] = a;
00303 }
00304 
00305 /******************************************************************************/
00306 
00307 //______________________________________________________________________________
00308 void TEveRGBAPalette::SetOverColor(Color_t ci)
00309 {
00310    // Set overflow color.
00311 
00312    fOverColor = ci;
00313    TEveUtil::ColorFromIdx(ci, fOverRGBA, kTRUE);
00314 }
00315 
00316 //______________________________________________________________________________
00317 void TEveRGBAPalette::SetOverColorPixel(Pixel_t pix)
00318 {
00319    // Set overflow color.
00320 
00321    SetOverColor(Color_t(TColor::GetColor(pix)));
00322 }
00323 
00324 //______________________________________________________________________________
00325 void TEveRGBAPalette::SetOverColorRGBA(UChar_t r, UChar_t g, UChar_t b, UChar_t a)
00326 {
00327    // Set overflow color.
00328 
00329    fOverColor = Color_t(TColor::GetColor(r, g, b));
00330    fOverRGBA[0] = r;
00331    fOverRGBA[1] = g;
00332    fOverRGBA[2] = b;
00333    fOverRGBA[3] = a;
00334 }
00335 
00336 //______________________________________________________________________________
00337 void TEveRGBAPalette::MinMaxValChanged()
00338 {
00339    // Emit the "MinMaxValChanged()" signal.
00340    // This is NOT called automatically from SetMin/Max functions but
00341    // it IS called from TEveRGBAPaletteEditor after it changes the
00342    // min/max values.
00343 
00344    Emit("MinMaxValChanged()");
00345 }

Generated on Tue Jul 5 14:16:13 2011 for ROOT_528-00b_version by  doxygen 1.5.1