-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColor.cpp
100 lines (75 loc) · 1.8 KB
/
Color.cpp
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
90
91
92
//
// Created by Tizian Rettig on 13.01.2021.
//
#include <cmath>
#include <sstream>
#include "Color.h"
#define BYTE_BOUNDARY 255
#define GAMMA 2.2
Color::Color() {
set(0, 0, 0);
}
Color::Color(double r, double g, double b) {
set(r, g, b);
}
void Color::set(double r, double g, double b) {
m_r = r;
m_g = g;
m_b = b;
}
void Color::set(const Color& c) {
set( c.getDoubleR(), c.getDoubleG(), c.getDoubleB());
}
Color Color::operator+(const Color& c) const {
return {
m_r + c.getDoubleR(),
m_g + c.getDoubleG(),
m_b + c.getDoubleB()
};
}
Color Color::operator*(const float& x) const {
return {
m_r * x,
m_g * x,
m_b * x
};
}
Color Color::operator*(const double& x) const {
return {
m_r * x,
m_g * x,
m_b * x
};
}
Color Color::mult(const Color& x) const {
return {
m_r * x.getDoubleR(),
m_g * x.getDoubleG(),
m_b * x.getDoubleB()
};
}
inline double clamp(double x) { return x<0 ? 0 : x>1 ? 1 : x; }
inline int toInt(double x) { return int(pow(clamp(x), 1/GAMMA) * BYTE_BOUNDARY + .5); }
double Color::getDoubleR() const {
return m_r;
}
double Color::getDoubleG() const {
return m_g;
}
double Color::getDoubleB() const {
return m_b;
}
std::byte Color::getR() const {
return (std::byte) toInt(m_r);
}
std::byte Color::getG() const {
return (std::byte) toInt(m_g);
}
std::byte Color::getB() const {
return (std::byte) toInt(m_b);
}
std::string Color::toString() const {
std::stringstream ss;
ss << "d(" << getDoubleR() << ", " << getDoubleG() << ", " << getDoubleB() << ")" << "b(" << toInt(m_r) << ", " << toInt(m_g) << ", " << toInt(m_b) << ")" << std::endl;
return ss.str();
}