raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Color.hpp
1#ifndef RAYLIB_CPP_INCLUDE_COLOR_HPP_
2#define RAYLIB_CPP_INCLUDE_COLOR_HPP_
3
4#include <string>
5#include <string_view>
6
7#include "./Vector4.hpp"
8#include "./RadiansDegrees.hpp"
9#include "./raylib-cpp-utils.hpp"
10#include "./raylib.hpp"
11
12namespace raylib {
16class Color : public ::Color {
17public:
18 Color(const ::Color& color) : ::Color{color.r, color.g, color.b, color.a} {}
19
20 Color(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255)
21 : ::Color{red, green, blue, alpha} {};
22
26 Color() : ::Color{0, 0, 0, 255} {};
27
31 Color(::Vector3 hsv) { set(::ColorFromHSV(hsv.x, hsv.y, hsv.z)); }
32
36 static ::Color FromHSV(float hue, float saturation, float value) { return ::ColorFromHSV(hue, saturation, value); }
37
41 explicit Color(unsigned int hexValue) : ::Color(::GetColor(hexValue)) { }
42
43 Color(void* srcPtr, int format) : ::Color(::GetPixelColor(srcPtr, format)) { }
44
48 [[nodiscard]] int ToInt() const { return ::ColorToInt(*this); }
49
53 explicit operator int() const { return ::ColorToInt(*this); }
54
55 [[nodiscard]] std::string ToString() const { return TextFormat("Color(%d, %d, %d, %d)", r, g, b, a); }
56
57 explicit operator std::string() const { return ToString(); }
58
62 [[nodiscard]] Color Fade(float alpha) const { return ::Fade(*this, alpha); }
63
67 [[nodiscard]] Vector4 Normalize() const { return ::ColorNormalize(*this); }
68
72 explicit Color(::Vector4 normalized) : Color(::ColorFromNormalized(normalized)) { }
73
77 [[nodiscard]] Vector3 ToHSV() const { return ::ColorToHSV(*this); }
78
79 GETTERSETTER(unsigned char, R, r)
80 GETTERSETTER(unsigned char, G, g)
81 GETTERSETTER(unsigned char, B, b)
82 GETTERSETTER(unsigned char, A, a)
83
84 Color& operator=(const ::Color& color) {
85 set(color);
86 return *this;
87 }
88
93 ::ClearBackground(*this);
94 return *this;
95 }
96
97 void DrawPixel(int x, int y) const { ::DrawPixel(x, y, *this); }
98
102 void DrawPixel(::Vector2 pos) const { ::DrawPixelV(pos, *this); }
103
107 void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY) const {
108 ::DrawLine(startPosX, startPosY, endPosX, endPosY, *this);
109 }
110
114 void DrawLine(::Vector2 startPos, ::Vector2 endPos) const { ::DrawLineV(startPos, endPos, *this); }
115
119 void DrawLine(::Vector2 startPos, ::Vector2 endPos, float thick) const {
120 ::DrawLineEx(startPos, endPos, thick, *this);
121 }
122
123 void DrawLineBezier(::Vector2 startPos, ::Vector2 endPos, float thick = 1.0f) const {
124 ::DrawLineBezier(startPos, endPos, thick, *this);
125 }
126
127 void DrawLineStrip(::Vector2* points, int numPoints) const { ::DrawLineStrip(points, numPoints, *this); }
128
129 void DrawText(const char* text, int posX = 0, int posY = 0, int fontSize = 10.0f) const {
130 ::DrawText(text, posX, posY, fontSize, *this);
131 }
132
133 void DrawText(const std::string_view text, int posX = 0, int posY = 0, int fontSize = 10.0f) const {
134 ::DrawText(text.data(), posX, posY, fontSize, *this);
135 }
136
137 void DrawText(const ::Font& font, const char* text, ::Vector2 position, float fontSize, float spacing) const {
138 ::DrawTextEx(font, text, position, fontSize, spacing, *this);
139 }
140
141 void DrawText(const ::Font& font, const std::string_view text, ::Vector2 position,
142 float fontSize, float spacing) const {
143 ::DrawTextEx(font, text.data(), position, fontSize, spacing, *this);
144 }
145
146 void DrawText(
147 const ::Font& font,
148 const char* text,
149 ::Vector2 position,
150 ::Vector2 origin,
151 Degree rotation,
152 float fontSize,
153 float spacing) const {
154 ::DrawTextPro(font, text, position, origin, rotation, fontSize, spacing, *this);
155 }
156
157 void DrawText(
158 const ::Font& font,
159 const std::string_view text,
160 ::Vector2 position,
161 ::Vector2 origin,
162 Degree rotation,
163 float fontSize,
164 float spacing) const {
165 ::DrawTextPro(font, text.data(), position, origin, rotation, fontSize, spacing, *this);
166 }
167
168 void DrawRectangle(int posX, int posY, int width, int height) const {
169 ::DrawRectangle(posX, posY, width, height, *this);
170 }
171
172 void DrawRectangle(::Vector2 position, ::Vector2 size) const { ::DrawRectangleV(position, size, *this); }
173
174 void DrawRectangle(::Rectangle rec) const { ::DrawRectangleRec(rec, *this); }
175
176 void DrawRectangle(::Rectangle rec, ::Vector2 origin, Degree rotation) const {
177 ::DrawRectanglePro(rec, origin, rotation, *this);
178 }
179
180 void DrawRectangleLines(int posX, int posY, int width, int height) const {
181 ::DrawRectangleLines(posX, posY, width, height, *this);
182 }
183
184 void DrawRectangleLines(::Rectangle rec, float lineThick) const { ::DrawRectangleLinesEx(rec, lineThick, *this); }
185
186 bool IsEqual(::Color color) {
187 return ::ColorIsEqual(*this, color);
188 }
189
190 bool operator==(const ::Color& other) const { return ::ColorIsEqual(*this, other); }
191 bool operator!=(const ::Color& other) const { return !::ColorIsEqual(*this, other); }
192
196 Color Tint(::Color tint) { return ::ColorTint(*this, tint); }
197
201 Color Brightness(float factor) { return ::ColorBrightness(*this, factor); }
202
206 Color Contrast(float contrast) { return ::ColorContrast(*this, contrast); }
207
211 [[nodiscard]] Color Alpha(float alpha) const { return ::ColorAlpha(*this, alpha); }
212
213 Color Lerp(::Color color2, float factor) {
214 return ::ColorLerp(*this, color2, factor);
215 }
216
220 [[nodiscard]] Color AlphaBlend(::Color dst, ::Color tint) const { return ::ColorAlphaBlend(dst, *this, tint); }
221
222 static Color LightGray() { return LIGHTGRAY; }
223 static Color Gray() { return GRAY; }
224 static Color DarkGray() { return DARKGRAY; }
225 static Color Yellow() { return YELLOW; }
226 static Color Gold() { return GOLD; }
227 static Color Orange() { return ORANGE; }
228 static Color Pink() { return PINK; }
229 static Color Red() { return RED; }
230 static Color Maroon() { return MAROON; }
231 static Color Green() { return GREEN; }
232 static Color Lime() { return LIME; }
233 static Color DarkGreen() { return DARKGREEN; }
234 static Color SkyBlue() { return SKYBLUE; }
235 static Color Blue() { return BLUE; }
236 static Color DarkBlue() { return DARKBLUE; }
237 static Color Purple() { return PURPLE; }
238 static Color Violet() { return VIOLET; }
239 static Color DarkPurple() { return DARKPURPLE; }
240 static Color Beige() { return BEIGE; }
241 static Color Brown() { return BROWN; }
242 static Color DarkBrown() { return DARKBROWN; }
243 static Color White() { return WHITE; }
244 static Color Black() { return BLACK; }
245 static Color Blank() { return BLANK; }
246 static Color Magenta() { return MAGENTA; }
247 static Color RayWhite() { return RAYWHITE; }
248protected:
249 void set(const ::Color& color) {
250 r = color.r;
251 g = color.g;
252 b = color.b;
253 a = color.a;
254 }
255};
256
257} // namespace raylib
258
259using RColor = raylib::Color;
260
261#endif // RAYLIB_CPP_INCLUDE_COLOR_HPP_
Color type, RGBA (32bit)
Definition: Color.hpp:16
Color AlphaBlend(::Color dst, ::Color tint) const
Returns src alpha-blended into dst color with tint.
Definition: Color.hpp:220
Color(::Vector3 hsv)
Returns a Color from HSV values.
Definition: Color.hpp:31
Color Contrast(float contrast)
Get color with contrast correction, contrast values between -1.0f and 1.0f.
Definition: Color.hpp:206
Color Tint(::Color tint)
Get color multiplied with another color.
Definition: Color.hpp:196
void DrawLine(::Vector2 startPos, ::Vector2 endPos) const
Draw a line using Vector points.
Definition: Color.hpp:114
void DrawPixel(::Vector2 pos) const
Draw a pixel.
Definition: Color.hpp:102
::Color FromHSV(float hue, float saturation, float value)
Returns a Color from HSV values.
Definition: Color.hpp:36
Vector4 Normalize() const
Returns Color normalized as float [0..1].
Definition: Color.hpp:67
Color Fade(float alpha) const
Returns color with alpha applied, alpha goes from 0.0f to 1.0f.
Definition: Color.hpp:62
int ToInt() const
Returns hexadecimal value for a Color.
Definition: Color.hpp:48
Color(::Vector4 normalized)
Returns Color from normalized values [0..1].
Definition: Color.hpp:72
void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY) const
Draw a line.
Definition: Color.hpp:107
Vector3 ToHSV() const
Returns HSV values for a Color.
Definition: Color.hpp:77
Color()
Black.
Definition: Color.hpp:26
void DrawLine(::Vector2 startPos, ::Vector2 endPos, float thick) const
Draw a line using Vector points, with a given thickness.
Definition: Color.hpp:119
Color Alpha(float alpha) const
Returns color with alpha applied, alpha goes from 0.0f to 1.0f.
Definition: Color.hpp:211
Color & ClearBackground()
Set background color (framebuffer clear color)
Definition: Color.hpp:92
Color(unsigned int hexValue)
Get Color structure from hexadecimal value.
Definition: Color.hpp:41
Color Brightness(float factor)
Get color with brightness correction, brightness factor goes from -1.0f to 1.0f.
Definition: Color.hpp:201
Vector3 type.
Definition: Vector3.hpp:20
Vector4 type.
Definition: Vector4.hpp:21
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
static void DrawTextPro(const Font &font, const char *text, Vector2 position, Vector2 origin, Degree rotation, float fontSize, float spacing, ::Color tint)
Draw text using Font and pro parameters (rotation)
Definition: Functions.hpp:283
static void DrawTextEx(const Font &font, char *text, Vector2 position, float fontSize, float spacing, ::Color tint)
Draw text using font and additional parameters.
Definition: Functions.hpp:268