-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.cpp
More file actions
128 lines (111 loc) · 2.86 KB
/
Color.cpp
File metadata and controls
128 lines (111 loc) · 2.86 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
/** @file Color.cpp */
#include "Color.h"
SDL_Surface * Color::format_surface = NULL;
Color Color::black;
Color Color::white;
Color Color::red;
Color Color::green;
Color Color::blue;
Color Color::yellow;
Color Color::magenta;
Color Color::cyan;
void Color::initialize()
{
format_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 1, KQ_COLOR_DEPTH, 0, 0, 0, 0);
black = Color( 0, 0, 0);
white = Color(255, 255, 255);
red = Color(255, 0, 0);
green = Color( 0, 255, 0);
blue = Color( 0, 0, 255);
yellow = Color(255, 255, 0);
magenta = Color(255, 0, 255);
cyan = Color( 0, 255, 255);
}
/**
* @brief Uninitializes the color system.
*/
void Color::quit()
{
SDL_FreeSurface(format_surface);
format_surface = NULL;
}
/**
* @brief Creates a default color with unspecified RGB values.
*/
Color::Color()
{
internal_color.r = 0;
internal_color.g = 0;
internal_color.b = 0;
internal_value = 0;
}
/**
* @brief Copy constructor.
* @param other another color
*/
Color::Color(const Color &other): internal_value(other.internal_value), internal_color(other.internal_color)
{
}
/**
* @brief Creates a color with the specified RGB values.
* @param r the red component (from 0 to 255)
* @param g the green component (from 0 to 255)
* @param b the blue component (from 0 to 255)
*/
Color::Color(int r, int g, int b) {
internal_color.r = r;
internal_color.g = g;
internal_color.b = b;
internal_value = SDL_MapRGB(format_surface->format, r, g, b);
}
/**
* @brief Creates a color from a 32-bit value.
*
* This constructor must be used only by low-level classes.
*
* @param internal_value The 32-bit value of the color to create.
*/
Color::Color(uint32_t internal_value):
internal_value(internal_value) {
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
internal_color.r = (internal_value & 0x00ff0000) / 0x10000;
internal_color.g = (internal_value & 0x0000ff00) / 0x100;
internal_color.b = (internal_value & 0x000000ff);
#else
internal_color.r = (internal_value & 0x000000ff);
internal_color.g = (internal_value & 0x0000ff00) / 0x100;
internal_color.b = (internal_value & 0x00ff0000) / 0x10000;
#endif
}
/**
* @brief Returns the 32-bit value representing this color.
*
* This function must be used only by low-level classes.
*
* @return the 32-bit value of this color
*/
uint32_t Color::get_internal_value() const
{
return internal_value;
}
/**
* @brief Returns the internal color encapsulated by this object.
*
* This function must be used only by low-level classes.
*
* @return the SDL color encapsulated
*/
SDL_Color* Color::get_internal_color() {
return &internal_color;
}
/**
* @brief Returns the red, green and blue values of this color.
* @param r Red component to write.
* @param g Green component to write.
* @param b Blue component to write.
*/
void Color::get_components(int& r, int& g, int& b) const {
r = internal_color.r;
g = internal_color.g;
b = internal_color.b;
}