Gnash  0.8.11dev
LinearRGB.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #ifndef GNASH_AGG_LINEAR_INTERPOLATOR_H
20 #define GNASH_AGG_LINEAR_INTERPOLATOR_H
21 
22 #include <cmath>
23 
24 namespace gnash {
25 
27 double
28 linearToSRGB(double s)
29 {
30  const double a = 0.055;
31  if (s <= 0.0031308) return 12.92 * s;
32  return (1 + a) * std::pow(s, 1 / 2.4) - a;
33 }
34 
35 template<typename T>
36 T
37 cdiff(T a, T b, double ratio)
38 {
39  const int diff = b - a;
40  const double d = linearToSRGB((diff < 0) ? 1 - ratio : ratio);
41  if (diff < 0) {
42  return b - d * diff;
43  }
44  return a + d * diff;
45 }
46 
48 //
52 template<class ColorT>
54 {
55 public:
56  typedef ColorT color_type;
57 
59  size_t len)
60  :
61  _c1(c1),
62  _c2(c2),
63  _len(len),
64  _count(0)
65  {}
66 
67  void operator++() {
68  ++_count;
69  }
70 
71  color_type color() const {
72  const double ratio = static_cast<double>(_count) / _len;
73  return color_type(
74  cdiff(_c1.r, _c2.r, ratio),
75  cdiff(_c1.g, _c2.g, ratio),
76  cdiff(_c1.b, _c2.b, ratio),
77  _c1.a + (_c2.a - _c1.a) * ratio);
78  }
79 
80 private:
81  color_type _c1;
82  color_type _c2;
83  size_t _len;
84  size_t _count;
85 };
86 
87 }
88 
89 #endif
Definition: GnashKey.h:147
Definition: GnashKey.h:150
void operator++()
Definition: LinearRGB.h:67
SWFStream & s
Definition: DefineBitsTag.cpp:73
ColorT color_type
Definition: LinearRGB.h:56
color_type color() const
Definition: LinearRGB.h:71
Interpolate in the linear RGB colorspace.
Definition: LinearRGB.h:53
T cdiff(T a, T b, double ratio)
Definition: LinearRGB.h:37
Definition: GnashKey.h:148
linear_rgb_interpolator(const color_type &c1, const color_type &c2, size_t len)
Definition: LinearRGB.h:58
Definition: GnashKey.h:132
double linearToSRGB(double s)
Convert linear RGB colorspace to sRGB.
Definition: LinearRGB.h:28