Nori  24
color.h
1 /*
2  This file is part of Nori, a simple educational ray tracer
3 
4  Copyright (c) 2015 by Wenzel Jakob
5 
6  Nori is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Nori 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, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #if !defined(__NORI_COLOR_H)
20 #define __NORI_COLOR_H
21 
22 #include <nori/common.h>
23 
24 NORI_NAMESPACE_BEGIN
25 
29 struct Color3f : public Eigen::Array3f {
30 public:
31  typedef Eigen::Array3f Base;
32 
34  Color3f(float value = 0.f) : Base(value, value, value) { }
35 
37  Color3f(float r, float g, float b) : Base(r, g, b) { }
38 
40  template <typename Derived> Color3f(const Eigen::ArrayBase<Derived>& p)
41  : Base(p) { }
42 
44  template <typename Derived> Color3f &operator=(const Eigen::ArrayBase<Derived>& p) {
45  this->Base::operator=(p);
46  return *this;
47  }
48 
50  float &r() { return x(); }
52  const float &r() const { return x(); }
54  float &g() { return y(); }
56  const float &g() const { return y(); }
58  float &b() { return z(); }
60  const float &b() const { return z(); }
61 
63  Color3f clamp() const { return Color3f(std::max(r(), 0.0f),
64  std::max(g(), 0.0f), std::max(b(), 0.0f)); }
65 
67  bool isValid() const;
68 
70  Color3f toLinearRGB() const;
71 
73  Color3f toSRGB() const;
74 
76  float getLuminance() const;
77 
79  std::string toString() const {
80  return tfm::format("[%f, %f, %f]", coeff(0), coeff(1), coeff(2));
81  }
82 };
83 
89 struct Color4f : public Eigen::Array4f {
90 public:
91  typedef Eigen::Array4f Base;
92 
94  Color4f() : Base(0.0f, 0.0f, 0.0f, 0.0f) { }
95 
97  Color4f(const Color3f &c) : Base(c.r(), c.g(), c.b(), 1.0f) { }
98 
100  Color4f(float r, float g, float b, float w) : Base(r, g, b, w) { }
101 
103  template <typename Derived> Color4f(const Eigen::ArrayBase<Derived>& p)
104  : Base(p) { }
105 
107  template <typename Derived> Color4f &operator=(const Eigen::ArrayBase<Derived>& p) {
108  this->Base::operator=(p);
109  return *this;
110  }
111 
114  if (w() != 0)
115  return head<3>() / w();
116  else
117  return Color3f(0.0f);
118  }
119 
121  std::string toString() const {
122  return tfm::format("[%f, %f, %f, %f]", coeff(0), coeff(1), coeff(2), coeff(3));
123  }
124 };
125 
126 NORI_NAMESPACE_END
127 
128 #endif /* __NORI_COLOR_H */
129 
Represents a linear RGB color value.
Definition: color.h:29
bool isValid() const
Check if the color vector contains a NaN/Inf/negative value.
Definition: common.cpp:224
std::string toString() const
Return a human-readable string summary.
Definition: color.h:79
float & g()
Return a reference to the green channel.
Definition: color.h:54
const float & r() const
Return a reference to the red channel (const version)
Definition: color.h:52
const float & b() const
Return a reference to the blue channel (const version)
Definition: color.h:60
float getLuminance() const
Return the associated luminance.
Definition: common.cpp:233
Color3f(float value=0.f)
Initialize the color vector with a uniform value.
Definition: color.h:34
Color3f toLinearRGB() const
Convert from sRGB to linear RGB.
Definition: common.cpp:208
float & b()
Return a reference to the blue channel.
Definition: color.h:58
Color3f(const Eigen::ArrayBase< Derived > &p)
Construct a color vector from ArrayBase (needed to play nice with Eigen)
Definition: color.h:40
Color3f toSRGB() const
Convert from linear RGB to sRGB.
Definition: common.cpp:192
const float & g() const
Return a reference to the green channel (const version)
Definition: color.h:56
Color3f clamp() const
Clamp to the positive range.
Definition: color.h:63
Color3f & operator=(const Eigen::ArrayBase< Derived > &p)
Assign a color vector from ArrayBase (needed to play nice with Eigen)
Definition: color.h:44
Color3f(float r, float g, float b)
Initialize the color vector with specific per-channel values.
Definition: color.h:37
float & r()
Return a reference to the red channel.
Definition: color.h:50
Represents a linear RGB color and a weight.
Definition: color.h:89
Color4f(const Eigen::ArrayBase< Derived > &p)
Construct a color vector from ArrayBase (needed to play nice with Eigen)
Definition: color.h:103
Color4f()
Create an zero value.
Definition: color.h:94
Color4f(const Color3f &c)
Create from a 3-channel color.
Definition: color.h:97
std::string toString() const
Return a human-readable string summary.
Definition: color.h:121
Color4f & operator=(const Eigen::ArrayBase< Derived > &p)
Assign a color vector from ArrayBase (needed to play nice with Eigen)
Definition: color.h:107
Color4f(float r, float g, float b, float w)
Initialize the color vector with specific per-channel values.
Definition: color.h:100
Color3f divideByFilterWeight() const
Divide by the filter weight and convert into a Color3f value.
Definition: color.h:113