00001
00007 #ifndef __COLORLOOKUP_H__
00008 #define __COLORLOOKUP_H__
00009
00010 #include <cassert>
00011 #include <algorithm>
00012 #include <vector>
00013
00014 #include "pixel.h"
00015
00016 namespace Plot {
00017
00018
00019
00020
00021
00022
00023
00024
00026 template<class PixelType=Plot::RGBPixel<> >
00027 class ColorLookup {
00028 public:
00029 virtual ~ColorLookup() { }
00030
00032 virtual PixelType operator() (const Bounded::Magnitude mag) const=0;
00033 };
00034
00035
00037 template<class PixelType=Plot::RGBPixel<> >
00038 class SingleColorLookup : public ColorLookup<PixelType> {
00039 public:
00040 virtual ~SingleColorLookup() { }
00041 SingleColorLookup(Bounded::Magnitude hue=1.0, Bounded::Magnitude sat=0.0, Bounded::Magnitude val=1.0)
00042 : h(hue), s(sat), v(val) { }
00043
00044 PixelType operator() (const Bounded::Magnitude) const {
00045 return PixelType(HSVPixel<>(h,s,v));
00046 }
00047 private:
00048 Bounded::Magnitude h,s,v;
00049 };
00050
00051
00052
00054 template<class PixelType=Plot::RGBPixel<> >
00055 class HueColorLookup : public ColorLookup<PixelType> {
00056 public:
00057 virtual ~HueColorLookup() { }
00058 HueColorLookup(Bounded::Magnitude sat=1.0, Bounded::Magnitude val=1.0)
00059 : s(sat), v(val) { }
00060
00061 PixelType operator() (const Bounded::Magnitude mag) const {
00062 return PixelType(HSVPixel<>(mag,s,v));
00063 }
00064 private:
00065 Bounded::Magnitude s,v;
00066 };
00067
00068
00069
00071 template<class PixelType=Plot::RGBPixel<> >
00072 class SaturationColorLookup : public ColorLookup<PixelType> {
00073 public:
00074 virtual ~SaturationColorLookup() { }
00075 SaturationColorLookup(Bounded::Magnitude hue=0.0, Bounded::Magnitude val=1.0)
00076 : h(hue), v(val) { }
00077
00078 PixelType operator() (const Bounded::Magnitude mag) const {
00079 return PixelType(HSVPixel<>(h,mag,v));
00080 }
00081 private:
00082 Bounded::Magnitude h,v;
00083 };
00084
00085
00086
00088 template<class PixelType=Plot::RGBPixel<> >
00089 class ValueColorLookup : public ColorLookup<PixelType> {
00090 public:
00091 virtual ~ValueColorLookup() { }
00092 ValueColorLookup(Bounded::Magnitude hue=0.0, Bounded::Magnitude sat=0.0)
00093 : h(hue), s(sat) { }
00094
00095 PixelType operator() (const Bounded::Magnitude mag) const {
00096 return PixelType(HSVPixel<>(h,s,mag));
00097 }
00098 private:
00099 Bounded::Magnitude h,s;
00100 };
00101
00102
00103
00106 template<class PixelType=Plot::RGBPixel<> >
00107 class ColorLookupTable : public ColorLookup<PixelType> {
00108 public:
00109 ColorLookupTable(size_t numcolors) : colors(numcolors) { }
00110 virtual ~ColorLookupTable() { }
00111
00112 PixelType operator() (const Bounded::Magnitude mag) const
00113 { return colors[Generic::crop(0,int(colors.size()-1),int(mag*(colors.size()-1)))]; }
00114
00115 public:
00116 std::vector<PixelType> colors;
00117 };
00118
00119
00120
00122 template<class PixelType=Plot::RGBPixel<> >
00123 class RYWColorLookup : public ColorLookupTable<PixelType> {
00124 public:
00125 virtual ~RYWColorLookup() { }
00126
00128 RYWColorLookup(size_t numcolors=252) : ColorLookupTable<PixelType>(numcolors) {
00129 const int thirdofrange = int(colors.size()/3);
00130 const double m = 1.0/double(thirdofrange);
00131
00132 for(size_t i=0; i<colors.size(); i++) {
00133 double r=0;
00134 double g=0;
00135 double b=0;
00136
00137 if (i < thirdofrange)
00138 r = i*m;
00139 else{
00140 r = 1.0;
00141 if (i < 2*thirdofrange)
00142 g = (i-thirdofrange) * m;
00143 else{
00144 g = 1.0;
00145 b = (i-2*thirdofrange)*m;
00146 }
00147 }
00148 colors[i]=PixelType(r,g,b);
00149 }
00150 }
00151 };
00152
00153
00154
00155 }
00156 #endif