FifeGUI 0.3.0
A C++ GUI library designed for games.
color.hpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#ifndef INCLUDE_FIFECHAN_COLOR_HPP_
6#define INCLUDE_FIFECHAN_COLOR_HPP_
7
8// Standard library includes
9#include <algorithm>
10#include <cstdint>
11#include <iomanip>
12#include <iostream>
13#include <ostream>
14#include <sstream>
15#include <stdexcept>
16#include <string>
17#include <vector>
18
19// Platform config include
20#include "fifechan/platform.hpp"
21
22namespace fcn
23{
57 class FIFEGUI_API Color
58 {
59 public:
64 Color() = default;
65
69 Color(Color const &) = default;
70
74 Color& operator=(Color const &) = default;
75
79 Color(Color&&) = default;
80
84 Color& operator=(Color&&) = default;
85
95 explicit Color(int hexColor);
96
108 explicit Color(std::string const & colorString);
109
122 Color(int r, int g, int b, int a);
123
136 Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
137
141 ~Color() = default;
142
143 //
144 // Arithmetic operators
145 //
146
157 Color operator+(Color const & color) const;
158
169 Color operator-(Color const & color) const;
170
180 Color operator*(float value) const;
181
190 Color operator+(float value) const;
191
200 Color operator-(float value) const;
201
202 //
203 // Object manipulation operators
204 //
205
214 Color& operator+=(Color const & color);
215
224 Color& operator-=(Color const & color);
225
234 Color& operator*=(float value);
235
236 //
237 // Comparison operators
238 //
239
246 bool operator==(Color const & color) const;
247
254 bool operator!=(Color const & color) const;
255
256 //
257 // Color conversions
258 //
259
274 Color lighten(float percentage) const;
275
290 Color darken(float percentage) const;
291
298 Color toGrayScale() const;
299
308 Color blendWith(Color const & other) const;
309
310 //
311 // to string and stream functions
312 //
313
318 std::string toHexString() const;
319
324 std::string toRGBString() const;
325
330 std::string toRGBAString() const;
331
338 friend FIFEGUI_API std::ostream& operator<<(std::ostream& out, Color const & color);
339
341 uint8_t r{0};
342
344 uint8_t g{0};
345
347 uint8_t b{0};
348
350 uint8_t a{255};
351
352 private:
363 void parseHex(std::string const & hex);
364
374 void parseRGB(std::string const & rgbString);
375
384 void parseRGBA(std::string const & rgbaString);
385
397 static std::vector<int> parseColorComponents(std::string const & colors, bool withAlpha = false);
398 };
399} // namespace fcn
400
401#endif // INCLUDE_FIFECHAN_COLOR_HPP_
uint8_t a
Alpha color component (0-255).
Definition color.hpp:350
Color & operator+=(Color const &color)
Adds the RGB values of another color to this color.
Definition color.cpp:216
Color toGrayScale() const
Converts the color to grayscale.
Definition color.cpp:264
Color & operator=(Color &&)=default
Move assignment operator.
std::string toRGBString() const
Returns the color in the form "rgb(r,g,b)".
Definition color.cpp:327
std::string toHexString() const
Returns the color as a hexadecimal string in the form "#RRGGBB".
Definition color.cpp:319
std::string toRGBAString() const
Returns the color in the form "rgba(r,g,b,a)".
Definition color.cpp:334
Color(Color &&)=default
Move constructor.
Color(Color const &)=default
Copy constructor.
Color operator+(Color const &color) const
Adds the RGB values of two colors together.
Definition color.cpp:167
Color lighten(float percentage) const
Lightens the color by a percentage.
Definition color.cpp:244
Color & operator*=(float value)
Multiplies the RGB values of this color with a float value.
Definition color.cpp:234
Color operator*(float value) const
Multiplies the RGB values of a color with a float value.
Definition color.cpp:206
uint8_t b
Blue color component (0-255).
Definition color.hpp:347
Color darken(float percentage) const
Darkens the color by a percentage.
Definition color.cpp:254
bool operator==(Color const &color) const
Compares two colors.
Definition color.cpp:302
uint8_t g
Green color component (0-255).
Definition color.hpp:344
bool operator!=(Color const &color) const
Compares two colors.
Definition color.cpp:307
friend FIFEGUI_API std::ostream & operator<<(std::ostream &out, Color const &color)
Output operator for output.
Definition color.cpp:312
Color operator-(Color const &color) const
Subtracts the RGB values of one color from another.
Definition color.cpp:177
Color & operator=(Color const &)=default
Copy assignment operator.
Color()=default
Default constructor.
Color blendWith(Color const &other) const
Blends the color with another color using alpha blending.
Definition color.cpp:271
uint8_t r
Red color component (0-255).
Definition color.hpp:341
Color & operator-=(Color const &color)
Subtracts the RGB values of another color from this color.
Definition color.cpp:225
~Color()=default
Destructor.
Used replacement tokens by configure_file():