Gnash  0.8.10
LinearRGB.h
Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00003 //   Free Software Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #ifndef GNASH_AGG_LINEAR_INTERPOLATOR_H
00020 #define GNASH_AGG_LINEAR_INTERPOLATOR_H
00021 
00022 #include <cmath>
00023 
00024 namespace gnash {
00025 
00027 double
00028 linearToSRGB(double s)
00029 {
00030     const double a = 0.055;
00031     if (s <= 0.0031308) return 12.92 * s;
00032     return (1 + a) * std::pow(s, 1 / 2.4) - a;
00033 }
00034 
00035 template<typename T>
00036 T
00037 cdiff(T a, T b, double ratio)
00038 {
00039     const int diff = b - a;
00040     const double d = linearToSRGB((diff < 0) ? 1 - ratio : ratio);
00041     if (diff < 0) {
00042         return b - d * diff;
00043     }
00044     return a + d * diff;
00045 }
00046 
00048 //
00052 template<class ColorT>
00053 struct linear_rgb_interpolator
00054 {
00055 public:
00056     typedef ColorT color_type;
00057 
00058     linear_rgb_interpolator(const color_type& c1, const color_type& c2, 
00059         size_t len)
00060         :
00061         _c1(c1),
00062         _c2(c2),
00063         _len(len),
00064         _count(0)
00065     {}
00066 
00067     void operator++() {
00068         ++_count;
00069     }
00070 
00071     color_type color() const {
00072         const double ratio = static_cast<double>(_count) / _len;
00073         return color_type(
00074                 cdiff(_c1.r, _c2.r, ratio),
00075                 cdiff(_c1.g, _c2.g, ratio),
00076                 cdiff(_c1.b, _c2.b, ratio),
00077                 _c1.a + (_c2.a - _c1.a) * ratio);
00078     }
00079 
00080 private:
00081     color_type _c1;
00082     color_type _c2;
00083     size_t _len;
00084     size_t _count;
00085 };
00086 
00087 }
00088 
00089 #endif