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 <utility>
23 
24 #include <cmath>
25 
26 namespace gnash {
27 
29 double
30 linearToSRGB(double s)
31 {
32  const double a = 0.055;
33  if (s <= 0.0031308) return 12.92 * s;
34  return (1 + a) * std::pow(s, 1 / 2.4) - a;
35 }
36 
37 template<typename T>
38 T
39 cdiff(T a, T b, double ratio)
40 {
41  const int diff = b - a;
42  const double d = linearToSRGB((diff < 0) ? 1 - ratio : ratio);
43  if (diff < 0) {
44  return b - d * diff;
45  }
46  return a + d * diff;
47 }
48 
50 //
54 template<class ColorT>
56 {
57 public:
58  typedef ColorT color_type;
59 
60  linear_rgb_interpolator(color_type c1, color_type c2,
61  size_t len)
62  :
63  _c1(std::move(c1)),
64  _c2(std::move(c2)),
65  _len(len),
66  _count(0)
67  {}
68 
69  void operator++() {
70  ++_count;
71  }
72 
73  color_type color() const {
74  const double ratio = static_cast<double>(_count) / _len;
75  return color_type(
76  cdiff(_c1.r, _c2.r, ratio),
77  cdiff(_c1.g, _c2.g, ratio),
78  cdiff(_c1.b, _c2.b, ratio),
79  _c1.a + (_c2.a - _c1.a) * ratio);
80  }
81 
82 private:
83  color_type _c1;
84  color_type _c2;
85  size_t _len;
86  size_t _count;
87 };
88 
89 }
90 
91 #endif
Definition: GnashKey.h:147
Definition: GnashKey.h:150
void operator++()
Definition: LinearRGB.h:69
ColorT color_type
Definition: LinearRGB.h:58
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
Interpolate in the linear RGB colorspace.
Definition: LinearRGB.h:55
color_type color() const
Definition: LinearRGB.h:73
T cdiff(T a, T b, double ratio)
Definition: LinearRGB.h:39
Definition: GnashKey.h:148
linear_rgb_interpolator(color_type c1, color_type c2, size_t len)
Definition: LinearRGB.h:60
Definition: GnashKey.h:132
Definition: GnashKey.h:165
double linearToSRGB(double s)
Convert linear RGB colorspace to sRGB.
Definition: LinearRGB.h:30