-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixel.h
More file actions
268 lines (228 loc) · 8.25 KB
/
Copy pathPixel.h
File metadata and controls
268 lines (228 loc) · 8.25 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// Created by Hà Tường Nguyên on 5/22/24.
//
#ifndef COMPUTERGRAPHIC_PIXEL_H
#define COMPUTERGRAPHIC_PIXEL_H
#include "../Utility/color.h"
#include <cstdint>
#include <ostream>
namespace graphic {
class Screen;
/**
* @brief Represents a pixel with red, green, blue, and alpha components.
* This class is the main components of the graphic::Screen class
*
* The Pixel class provides methods for setting and retrieving the color
* components of a pixel. It supports various constructors for different
* use cases, including setting all components to the same value (grey),
* setting individual RGB values, and including an alpha channel.
*/
class Pixel {
friend Screen;
/**
* @brief Overloads the << operator to print Pixel information.
*
* This friend function allows a Pixel object's RGB values to be printed
* in the format (r, g, b).
*
* @param _cout The output stream object.
* @param pixel The Pixel object to be printed.
* @return The output stream object with the Pixel's RGB values.
*/
friend std::ostream& operator<<(std::ostream& _cout, const Pixel& pixel);
private:
uint8_t r, g, b, a;
public:
[[maybe_unused]] Pixel();
/**
* @brief Sets the RGB color of a Pixel.
*
* This constructor initializes a Pixel with the specified color value.
* The same color value is assigned to the red, green, and blue components
* of the Pixel, resulting in a shade of grey.
*
* @param color The integer value to set the red, green, and blue components
* of the Pixel. This value should be between 0 (black) and 255 (white).
*/
[[maybe_unused]] explicit Pixel(int color);
/**
* @brief Sets the RGB color of a Pixel.
*
* This constructor initializes a Pixel with the specified red, green, and blue values.
*
* @param _r The red component value.
* @param _g The green component value.
* @param _b The blue component value.
*/
[[maybe_unused]] explicit Pixel(int _r, int _g, int _b);
/**
* @brief Sets the RGBA color of a Pixel.
*
* This constructor initializes a Pixel with the specified red, green, blue, and alpha values.
*
* @param _r The red component value.
* @param _g The green component value.
* @param _b The blue component value.
* @param _a The alpha component value.
*/
[[maybe_unused]] explicit Pixel(int _r, int _g, int _b, int _a);
~Pixel() = default;
Pixel& operator=(const Pixel& other);
/**
* @brief Sets the RGB color of the Pixel.
*
* @param _r The red component value.
* @param _g The green component value.
* @param _b The blue component value.
*/
void set(int _r, int _g, int _b);
/**
* @brief Sets the RGBA color of the Pixel.
*
* @param _r The red component value.
* @param _g The green component value.
* @param _b The blue component value.
* @param _a The alpha component value.
*/
void set(int _r, int _g, int _b, int _a);
/**
* @brief Sets the Pixel to a shade of grey.
*
* @param gray_value The grey value to set the red, green, and blue components.
*/
void set(int gray_value);
/**
* @brief Sets the red component of the Pixel.
*
* @param _r The red component value.
*/
void set_r(int _r);
/**
* @brief Sets the green component of the Pixel.
*
* @param _g The green component value.
*/
void set_g(int _g);
/**
* @brief Sets the blue component of the Pixel.
*
* @param _b The blue component value.
*/
void set_b(int _b);
/**
* @brief Sets the alpha component of the Pixel.
*
* @param _a The alpha component value.
*/
void set_alpha(int _a);
/**
* @brief Converts the Pixel to a shade of grey.
*/
void to_gray();
/**
* @brief Gets the red component of the Pixel.
*
* @return The red component value.
*/
[[nodiscard]] [[maybe_unused]] uint8_t R() const;
/**
* @brief Gets the green component of the Pixel.
*
* @return The green component value.
*/
[[nodiscard]] [[maybe_unused]] uint8_t G() const;
/**
* @brief Gets the blue component of the Pixel.
*
* @return The blue component value.
*/
[[nodiscard]] [[maybe_unused]] uint8_t B() const;
/**
* @brief Gets the alpha component of the Pixel.
*
* @return The alpha component value.
*/
[[nodiscard]] [[maybe_unused]] uint8_t A() const;
/**
* @brief Gets the grey value of the Pixel.
*
* @return The grey value.
*/
[[nodiscard]] [[maybe_unused]] int gray() const;
/**
* @brief Creates a red Pixel.
*
* @param _a The alpha component value (default is 255).
* @return A red Pixel with the specified alpha value.
*/
[[maybe_unused]] static Pixel red(int _a = 255);
/**
* @brief Creates a green Pixel.
*
* @param _a The alpha component value (default is 255).
* @return A green Pixel with the specified alpha value.
*/
[[maybe_unused]] static Pixel green(int _a = 255);
/**
* @brief Creates a yellow Pixel.
*
* @param _a The alpha component value (default is 255).
* @return A yellow Pixel with the specified alpha value.
*/
[[maybe_unused]] static Pixel yellow(int _a = 255);
/**
* @brief Creates a blue Pixel.
*
* @param _a The alpha component value (default is 255).
* @return A blue Pixel with the specified alpha value.
*/
[[maybe_unused]] static Pixel blue(int _a = 255);
/**
* @brief Creates a magenta Pixel.
*
* @param _a The alpha component value (default is 255).
* @return A magenta Pixel with the specified alpha value.
*/
[[maybe_unused]] static Pixel magenta(int _a = 255);
/**
* @brief Creates a cyan Pixel.
*
* @param _a The alpha component value (default is 255).
* @return A cyan Pixel with the specified alpha value.
*/
[[maybe_unused]] static Pixel cyan(int _a = 255);
/**
* @brief Creates a white Pixel.
*
* @param _a The alpha component value (default is 255).
* @return A white Pixel with the specified alpha value.
*/
[[maybe_unused]] static Pixel white(int _a = 255);
/**
* @brief A struct for comparing Pixels based on their grayscale values.
*/
struct Compare {
struct Less {
/**
* @brief Compares two Pixels and returns true if the first is less than the second.
*
* @param lhs The first Pixel to compare.
* @param rhs The second Pixel to compare.
* @return True if the grayscale value of lhs is less than that of rhs, otherwise false.
*/
bool operator()(Pixel& lhs, Pixel& rhs) { return lhs.gray() < rhs.gray(); }
};
struct Greater {
/**
* @brief Compares two Pixels and returns true if the first is greater than the second.
*
* @param lhs The first Pixel to compare.
* @param rhs The second Pixel to compare.
* @return True if the grayscale value of lhs is greater than that of rhs, otherwise false.
*/
bool operator()(Pixel& lhs, Pixel& rhs) { return lhs.gray() > rhs.gray(); }
};
};
};
}
#endif //COMPUTERGRAPHIC_PIXEL_H