Color
A simple RGBA color utility class.
The guikit::Color class is a utility class that stores a color as four 8-bit channels (red, green, blue, alpha) and provides helpers to set and read them.
This doc assumes using namespace guikit is set and #include <guikit/color.hpp> is included.
Otherwise, use guikit::Color instead of Color.
Constructors
Color()
Creates a fully transparent color (0, 0, 0, 0).
Color();Color(uint32_t color)
Creates a color from a single 32-bit value. The bytes are interpreted as RRGGBBAA.
Color(uint32_t color);Example:
Color(0xFF0000FF); // Bright red, opaqueColor(uint8_t r, uint8_t g, uint8_t b)
Creates an opaque color from RGB channels (a = 255).
Color(uint8_t r, uint8_t g, uint8_t b);Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Creates a color from RGBA channels.
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a);Example:
Color(255, 0, 0, 255); // Bright redColor(std::string color)
Parses a hex string like "#RRGGBB" or "#RRGGBBAA". A leading # is optional. For RRGGBB, alpha defaults to 255.
Color(std::string color);Example:
Color("#22c55e"); // Green
Color("#ff000080"); // Semi-transparent redAvailable methods
set(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Sets all four channels at once and returns the color for chaining.
Color &set(uint8_t r, uint8_t g, uint8_t b, uint8_t a);Example:
Color(255, 0, 0, 255).set(0, 255, 0, 255); // Changing red to greensetR(uint8_t r)
Sets the red channel and returns the color for chaining.
Color &setR(uint8_t r);Example:
Color(0, 0, 0, 255).setR(255);setG(uint8_t g)
Sets the green channel and returns the color for chaining.
Color &setG(uint8_t g);Example:
Color(0, 0, 0, 255).setG(255);setB(uint8_t b)
Sets the blue channel and returns the color for chaining.
Color &setB(uint8_t b);Example:
Color(0, 0, 0, 255).setB(255);setA(uint8_t a)
Sets the alpha (opacity) channel and returns the color for chaining.
Color &setA(uint8_t a);Example:
Color(0, 0, 0, 255).setA(128);getRGBA()
Returns the color packed into a single uint32_t in RRGGBBAA order.
uint32_t getRGBA();Example:
uint32_t rgba = Color(255, 0, 0, 255).getRGBA();getABGR()
Returns the color packed into a single uint32_t in AABBGGRR order.
uint32_t getABGR();Example:
uint32_t abgr = Color(255, 0, 0, 255).getABGR();getSDLColor()
Returns the color as an SDL_Color struct, ready to use with SDL rendering APIs.
SDL_Color getSDLColor();Example:
SDL_Color sdlColor = Color(255, 0, 0, 255).getSDLColor();Full Example
Here is a full example of how to use the Color class:
#include <guikit/color.hpp>
#include <cstdint>
#include <iostream>
using namespace guikit;
int main() {
// initialize using hex (opaque red)
Color myColor(0xFF0000FF);
// change value to semi-transparent purple using chaining
myColor.setB(255).setA(128);
// access components
std::cout << "Red: " << (int)myColor.r << std::endl;
std::cout << "Green: " << (int)myColor.g << std::endl;
std::cout << "Blue: " << (int)myColor.b << std::endl;
std::cout << "Alpha: " << (int)myColor.a << std::endl;
// parse from a hex string
Color parsed("#22c55e");
std::cout << "Parsed green: " << (int)parsed.g << std::endl;
// access raw color
std::cout << "Raw RGBA value: 0x" << std::hex << myColor.getRGBA() << std::endl;
return 0;
}