-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.h
89 lines (67 loc) · 1.83 KB
/
color.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef COLOR_H_
#define COLOR_H_
#pragma once
#include "dcolor.h"
namespace raytracer {
// RGB ranges [0, 1.0].
class Color {
public:
Color();
Color(double red, double green, double blue);
Color(const Color& c);
Color& operator=(const Color& c);
~Color();
void Clamp();
DColor GetDColor() const;
friend Color operator+(const Color& l, const Color& r);
friend Color operator-(const Color& l, const Color& r);
friend Color operator*(const Color& l, const Color& r);
friend Color operator*(double d, const Color& r);
double r;
double g;
double b;
private:
double Clamp(double d) const;
};
inline Color::Color() : r(0.0), g(0.0), b(0.0) { }
inline Color::Color(double red, double green, double blue)
: r(red), g(green), b(blue) {
Clamp();
}
inline Color::Color(const Color& c) : r(c.r), g(c.g), b(c.b) { }
inline Color& Color::operator=(const Color& c) {
r = c.r;
g = c.g;
b = c.b;
return *this;
}
inline Color::~Color() { }
inline double Color::Clamp(double d) const {
if (d > 1.0) d = 1.0;
else if (d < 0.0) d = 0.0;
return d;
}
inline void Color::Clamp() {
r = Clamp(r);
g = Clamp(g);
b = Clamp(b);
}
inline DColor Color::GetDColor() const {
return DColor(static_cast<unsigned char>(Clamp(r) * 255),
static_cast<unsigned char>(Clamp(g) * 255),
static_cast<unsigned char>(Clamp(b) * 255));
}
inline Color operator+(const Color& l, const Color& r) {
return Color(l.r + r.r, l.g + r.g, l.b + r.b);
}
inline Color operator-(const Color& l, const Color& r) {
return Color(l.r - r.r, l.g - r.g, l.b - r.b);
}
inline Color operator*(const Color& l, const Color& r) {
return Color(l.r * r.r, l.g * r.g, l.b * r.b);
}
inline Color operator*(double d, const Color& r) {
return Color(d * r.r, d * r.g, d * r.b);
}
} // namespace raytracer
#endif // COLOR_H_