Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ColorScale.cxx

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #include "EventDisplay/ColorScale.h"
00009 #include <iostream>
00010 #include <cmath>
00011 #include "TStyle.h"
00012 #include "TColor.h"
00013 using namespace evd;
00014 
00028 ColorScale::ColorScale(double xlo, double xhi,
00029                        int which, int scale, int n,
00030                        double h1, double h2, double v1, double v2) :
00031   fXlo(xlo),
00032   fXhi(xhi),
00033   fScale(scale)
00034 {
00035   switch(which) {
00036   case kColdToHot:    this->MakeHSVScale(n, 150.0, 0.0,   0.2, 0.5);  break;
00037   case kHeatedObject: this->MakeHSVScale(n, 60.0,  0.0,   0.1, 0.9);  break;
00038   case kReds:         this->MakeHSVScale(n, 30.0,  0.0,   0.1, 0.9);  break;
00039   case kBlues:        this->MakeHSVScale(n, 180.0, 270.0, 0.1, 0.9);  break;
00040   case kGreens:       this->MakeHSVScale(n, 90.0,  120.0, 0.1, 0.9);  break;
00041   case kGeographic:   this->MakeHSVScale(n, 270.0, 30.0,  0.9, 0.05); break;
00042   case kCustom:       this->MakeHSVScale(n, h1, h2, v1, v2);          break;
00043   case kRainbow:
00044     // Default to a rainbow.
00045   default:
00046     this->MakeHSVScale(16,270,0,0.1,0.6);
00047     break;
00048   }
00049 }
00050 
00051 //......................................................................
00058 int ColorScale::GetColor(double x) const
00059 {
00060   double f=0.0;
00061   if (fScale == kLinear) {
00062     f = (x-fXlo)/(fXhi-fXlo);
00063   }
00064   else if (fScale == kLog) {
00065     f = (log(x)-log(fXlo))/(log(fXhi)-log(fXlo));
00066   }
00067   else if (fScale == kSqrt) {
00068     f = (sqrt(x)-sqrt(fXlo))/(sqrt(fXhi)-sqrt(fXlo));
00069   }
00070 
00071   int indx = (int)floor(f*(float)fNcolor);
00072   if (indx<0)        indx = 0;
00073   if (indx>=fNcolor) indx = fNcolor-1;
00074   return fColors[indx];
00075 }
00076 int ColorScale::operator()(double x) const { 
00077   return this->GetColor(x); 
00078 }
00079 
00080 //.....................................................................
00091 void ColorScale::HSVtoRGB(double h,  double s,  double v,
00092                           double* r, double* g, double* b) const
00093 {
00094   // A-chromatic, return grey scale values
00095   if (s==0.0) { *r = *g = *b = v; return; }
00096 
00097   int i;
00098   double f, p, q, t;
00099   double hh = h;
00100   while (hh<  0.0) hh += 360.0;
00101   while (hh>360.0) hh -= 360.0;
00102   hh /= 60;
00103   i = (int)floor(hh);
00104   f = hh - i;
00105   p = v * (1 - s);
00106   q = v * (1 - s*f);
00107   t = v * (1 - s*(1-f) );
00108   switch( i ) {
00109   case 0:  *r = v; *g = t; *b = p; break;
00110   case 1:  *r = q; *g = v; *b = p; break;
00111   case 2:  *r = p; *g = v; *b = t; break;
00112   case 3:  *r = p; *g = q; *b = v; break;
00113   case 4:  *r = t; *g = p; *b = v; break;
00114   default: *r = v; *g = p; *b = q; break;
00115   }
00116 }
00117 
00118 //......................................................................
00119 
00132 void ColorScale::MakeHSVScale(int n,
00133                               double h1, double h2,
00134                               double vs1, double vs2) 
00135 {
00136   int i;
00137   double r, g, b;
00138   double h;
00139   double vs, v, s;
00140   
00141   if (n>128) n = 128;
00142   fNcolor = n;
00143 
00144   for (i=0; i<fNcolor; ++i) {
00145     h  = h1  + (h2-h1)*(float)i/(float)(fNcolor-1);
00146     vs = vs1 + (vs2-vs1)*(float)i/(float)(fNcolor-1);
00147     vs = -1.0 + 2.0*vs;
00148     if (vs<0.0) { v = 1.0; s = 1.0+vs; }
00149     else { v = 1.0-vs; s = 1.0; }
00150     this->HSVtoRGB(h, s, v, &r, &g, &b);
00151     r *= 255;
00152     g *= 255;
00153     b *= 255;
00154     fColors[i] = TColor::GetColor((int)r,(int)g,(int)b);
00155   }
00156 }
00157 
00158 //......................................................................
00162 void ColorScale::SetPalette()
00163 {
00164   gStyle->SetPalette(fNcolor, fColors);
00165 }
00166 
00168 

Generated on Mon Dec 1 02:35:17 2008 for NOvA Offline by  doxygen 1.3.9.1