-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixel.cpp
More file actions
67 lines (53 loc) · 1.64 KB
/
pixel.cpp
File metadata and controls
67 lines (53 loc) · 1.64 KB
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
#include "pixel.hpp"
#include <algorithm>
#include "constants.hpp"
const static double EPS = 0.001;
Pixel::Pixel(double red, double green, double blue) : red(red), green(green), blue(blue) {
}
Pixel::Pixel(uint8_t red, uint8_t green, uint8_t blue)
: red(static_cast<double>(red) / BYTE_TO_DOUBLE_COEF),
green(static_cast<double>(green) / BYTE_TO_DOUBLE_COEF),
blue(static_cast<double>(blue) / BYTE_TO_DOUBLE_COEF) {
}
Pixel Pixel::operator+(const Pixel& other) const {
return {red + other.red, green + other.green, blue + other.blue};
}
Pixel& Pixel::operator+=(const Pixel& other) {
return *this = *this + other;
}
Pixel Pixel::operator-() const {
return {1 - red, 1 - green, 1 - blue};
}
Pixel Pixel::operator*(double k) const {
return {red * k, green * k, blue * k};
}
Pixel& Pixel::operator*=(double k) {
return *this = *this * k;
}
Pixel::Pixel(double gray_shade) {
red = green = blue = gray_shade;
}
bool Pixel::operator==(const Pixel& other) const {
if (std::abs(red - other.red) >= EPS) {
return false;
}
if (std::abs(green - other.green) >= EPS) {
return false;
}
if (std::abs(blue - other.blue) >= EPS) {
return false;
}
return true;
}
bool Pixel::operator>(const Pixel& other) const {
return (red + green + blue) > (other.red + other.green + other.blue);
}
std::ostream& operator<<(std::ostream& out, const Pixel& p) {
return out << "(" << p.red << " " << p.green << " " << p.blue << ")";
}
Pixel Pixel::operator/(double k) const {
return {red / k, green / k, blue / k};
}
Pixel& Pixel::operator/=(double k) {
return *this = *this / k;
}