-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixel.cpp
52 lines (42 loc) · 1.32 KB
/
pixel.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
#include "pixel.hpp"
#include <cmath>
#include <cstdint>
Pixel::Pixel()
: red_(static_cast<Pixel::Channel>(0)),
green_(static_cast<Pixel::Channel>(0)),
blue_(static_cast<Pixel::Channel>(0)) {
}
Pixel::Pixel(Pixel::Channel red, Pixel::Channel green, Pixel::Channel blue) {
SetRed(red);
SetGreen(green);
SetBlue(blue);
}
Pixel::Pixel(uint8_t red, uint8_t green, uint8_t blue)
: red_(static_cast<Pixel::Channel>(red) / UINT8_MAX),
green_(static_cast<Pixel::Channel>(green) / UINT8_MAX),
blue_(static_cast<Pixel::Channel>(blue) / UINT8_MAX) {
}
void Pixel::SetRed(Pixel::Channel red) {
red_ = std::clamp(red, Channel{0.0}, Channel{1.0});
}
void Pixel::SetGreen(Pixel::Channel green) {
green_ = std::clamp(green, Channel{0.0}, Channel{1.0});
}
void Pixel::SetBlue(Pixel::Channel blue) {
blue_ = std::clamp(blue, Channel{0.0}, Channel{1.0});
}
Pixel::Channel Pixel::GetRed() const {
return red_;
}
Pixel::Channel Pixel::GetGreen() const {
return green_;
}
Pixel::Channel Pixel::GetBlue() const {
return blue_;
}
uint8_t Pixel::ChannelToUInt8T(Pixel::Channel brightness) {
return static_cast<uint8_t>(std::round(brightness * UINT8_MAX));
}
std::ostream& operator<<(std::ostream& os, const Pixel& pixel) {
return os << pixel.red_ << ' ' << pixel.green_ << ' ' << pixel.blue_;
}