FifeGUI 0.2.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#include <algorithm>
9#include <cstdint>
10#include <iomanip>
11#include <iostream>
12#include <ostream>
13#include <sstream>
14#include <stdexcept>
15#include <string>
16#include <vector>
17
18#include "fifechan/platform.hpp"
19
20namespace fcn
21{
55 class FIFEGUI_API Color
56 {
57 public:
61 Color() = default;
62
72 explicit Color(int hexColor);
73
85 explicit Color(std::string const & colorString);
86
99 Color(int r, int g, int b, int a);
100
113 Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
114
115 //
116 // Arithmetic operators
117 //
118
129 Color operator+(Color const & color) const;
130
141 Color operator-(Color const & color) const;
142
152 Color operator*(float value) const;
153
162 Color operator+(float value) const;
163
172 Color operator-(float value) const;
173
174 //
175 // Object manipulation operators
176 //
177
186 Color& operator+=(Color const & color);
187
196 Color& operator-=(Color const & color);
197
206 Color& operator*=(float value);
207
208 //
209 // Comparison operators
210 //
211
218 bool operator==(Color const & color) const;
219
226 bool operator!=(Color const & color) const;
227
228 //
229 // Color conversions
230 //
231
246 Color lighten(float percentage) const;
247
262 Color darken(float percentage) const;
263
270 Color toGrayScale() const;
271
280 Color blendWith(Color const & other) const;
281
282 //
283 // to string and stream functions
284 //
285
290 std::string toHexString() const;
291
296 std::string toRGBString() const;
297
302 std::string toRGBAString() const;
303
310 friend std::ostream& operator<<(std::ostream& out, Color const & color);
311
313 uint8_t r{0};
314
316 uint8_t g{0};
317
319 uint8_t b{0};
320
322 uint8_t a{255};
323
324 private:
335 void parseHex(std::string const & hex);
336
346 void parseRGB(std::string const & rgbString);
347
356 void parseRGBA(std::string const & rgbaString);
357
369 static std::vector<int> parseColorComponents(std::string const & colors, bool withAlpha = false);
370 };
371} // namespace fcn
372
373#endif // INCLUDE_FIFECHAN_COLOR_HPP_
uint8_t a
Alpha color component (0-255).
Definition color.hpp:322
Color & operator+=(Color const &color)
Adds the RGB values of another color to this color.
Definition color.cpp:210
Color toGrayScale() const
Converts the color to grayscale.
Definition color.cpp:258
std::string toRGBString() const
Returns the color in the form "rgb(r,g,b)".
Definition color.cpp:321
std::string toHexString() const
Returns the color as a hexadecimal string in the form "#RRGGBB".
Definition color.cpp:313
friend std::ostream & operator<<(std::ostream &out, Color const &color)
Output operator for output.
Definition color.cpp:306
std::string toRGBAString() const
Returns the color in the form "rgba(r,g,b,a)".
Definition color.cpp:328
Color operator+(Color const &color) const
Adds the RGB values of two colors together.
Definition color.cpp:161
Color lighten(float percentage) const
Lightens the color by a percentage.
Definition color.cpp:238
Color & operator*=(float value)
Multiplies the RGB values of this color with a float value.
Definition color.cpp:228
Color operator*(float value) const
Multiplies the RGB values of a color with a float value.
Definition color.cpp:200
uint8_t b
Blue color component (0-255).
Definition color.hpp:319
Color darken(float percentage) const
Darkens the color by a percentage.
Definition color.cpp:248
bool operator==(Color const &color) const
Compares two colors.
Definition color.cpp:296
uint8_t g
Green color component (0-255).
Definition color.hpp:316
bool operator!=(Color const &color) const
Compares two colors.
Definition color.cpp:301
Color operator-(Color const &color) const
Subtracts the RGB values of one color from another.
Definition color.cpp:171
Color()=default
Constructor.
Color blendWith(Color const &other) const
Blends the color with another color using alpha blending.
Definition color.cpp:265
uint8_t r
Red color component (0-255).
Definition color.hpp:313
Color & operator-=(Color const &color)
Subtracts the RGB values of another color from this color.
Definition color.cpp:219