-
Notifications
You must be signed in to change notification settings - Fork 0
/
Color.cpp
68 lines (67 loc) · 2.39 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
#include "Color.hpp"
#include <sstream> // to use "stoi"
#include <iomanip> // to use "setfill" and "setw"
namespace prog {
Color::Color() {
// default constructor with color = black (0, 0, 0)
r = 0;
g = 0;
b = 0;
}
Color::Color(const Color& other) {
// copy constructor
r = other.r;
g = other.g;
b = other.b;
}
Color::Color(rgb_value red, rgb_value green, rgb_value blue) {
// constructor supplied (r, g, b) values
this->r = red;
this->g = green;
this->b = blue;
}
rgb_value Color::red() const {
return r; //get red component
}
rgb_value Color::green() const {
return g; //get green component
}
rgb_value Color::blue() const {
return b; //get blue component
}
rgb_value& Color::red() {
return r; // mutator for red component
}
rgb_value& Color::green() {
return g; // mutator for green component
}
rgb_value& Color::blue() {
return b; // mutator for blue component
}
Color::Color(const string& hexcode) {
// use "stoi" to turn hexcode string to integer values rgb
// pass "nullptr" because we don't need to store the values, we'll just use them temporarily
// 16 indicates the base for interpreting the string as a hexadecimal number
this->r = stoi(hexcode.substr(1, 2), nullptr, 16);
this->g = stoi(hexcode.substr(3, 2), nullptr, 16);
this->b = stoi(hexcode.substr(5, 2), nullptr, 16);
}
string Color::rgb_to_hexcode(const Color& c) {
// stringstream to build hexcode by converting the rgb values to a string
stringstream hexcode;
// perform bitwise & operation between the rgb value and "0xFF"(11111111) to get least significant 8 bits that represent a rgb value
hexcode << "#" << setfill('0') << setw(2) << hex << uppercase << (c.red() & 0xFF)
<< setfill('0') << setw(2) << hex << uppercase << (c.green() & 0xFF)
<< setfill('0') << setw(2) << hex << uppercase << (c.blue() & 0xFF);
return hexcode.str();
}
bool Color::operator<(const Color& other) const {
if (r < other.r)
return true;
else if (r == other.r && g < other.g)
return true;
else if (r == other.r && g == other.g && b < other.b)
return true;
return false;
}
}