Color
Setters and getters for color operations.
The guikit::Color class is a utility class that can be used as helper to set colors.
the color is stored as a single uint32_t ( 32 bit unsigned integer )
This doc assumes using namespace guikit is set and #include <guikit/color.hpp> is included.
Otherwise, use guikit::Color instead of Color.
Constructors
- By listing individual color components
// red // green // blue // opacity
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a);Example:
// Bright red
Color(255,0,0,255);- By using single hexadecimal literal to specify the color
Color(uint32_t color);// Bright Red
Color(0xFF0000FF);Available methods
set()
change the values of the color object.
Color& set(uint8_t r, uint8_t g, uint8_t b, uint8_t a);Example:
// Changing red to green
Color(255,0,0,255).set(0,255,0,255);setR(uint8_t r)
set the red component of the color object
Color& setR(uint8_t r);Example:
Color(0,0,0,255).setR(255);setG(uint8_t g)
set the green component of the color object
Color& setG(uint8_t g);Example:
Color(0,0,0,255).setG(255);setB(uint8_t b)
set the blue component of the color object
Color& setB(uint8_t b);Example:
Color(0,0,0,255).setB(255);setA(uint8_t a)
set the alpha (opacity) component of the color object
Color& setA(uint8_t a);Example:
Color(0,0,0,255).setA(128); getR()
get the red component of the color object
uint8_t getR();Example:
uint8_t r = Color(255,0,0,255).getR();getG()
get the green component of the color object
uint8_t getG();Example:
uint8_t g = Color(0,255,0,255).getG();getB()
get the blue component of the color object
uint8_t getB();Example:
uint8_t b = Color(0,0,255,255).getB();getA()
get the alpha (opacity) component of the color object
uint8_t getA();Example:
uint8_t a = Color(0,0,0,128).getA();Full Example
Here is a full example of how to use the Color class:
#include <guikit/color.hpp>
#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.getR() << std::endl;
std::cout << "Green: " << (int)myColor.getG() << std::endl;
std::cout << "Blue: " << (int)myColor.getB() << std::endl;
std::cout << "Alpha: " << (int)myColor.getA() << std::endl;
// access raw color
std::cout << "Raw value: 0x" << std::hex << myColor.color << std::endl;
return 0;
}