Basic Usage - gchudnov/cpp-colors GitHub Wiki
The color
data type denotes a color in the RGB color space.
// A: 255, R: 0, G: 0, B: 0
color c0;
// A, R, G, B
color c1(255, 0, 0, 0);
// R, G, B; A = 255
color c2(0, 0, 0);
// create from a constant
color c3(x11_color_constant::black);
// create from a packed value
color c4(0xFF000000);
Color transparency is controlled by the alpha channel.
- Max. value (0xFF) means fully opaque.
- Min. value (0x00) means fully transparent.
Colors channels can be represented as floating point numbers via colorF data type
// A: 255, R: 0, G: 0, B: 0
color c0;
// 0xff000000
uint32_t bgra32_value = c0.value(); // default format bgra32: pixel_format::color
// 0x00008000
uint32_t bgra5551_value = c0.value<pixel_format::bgra5551>();
More details on color value formatting can be found here
color c1(0xFF000000);
color c2(0x00010101);
// 0xFF010101
color c3 = c1 + c2;
color c1(0xFF020304);
color c2(0x00010101);
// 0xFF010203
color c3 = c1 - c2;
color c1(0x02020202);
color c2(0x01020304);
// 0x02040608
color c3 = c1 * c2;
color c4(0x02020202);
color c5(0x04080808);
// 0x02040404
color c6 = c5 / c4;
color c1(0x01020304);
color c2(0x02040608);
// 0x02040608
color c3 = c1 * 2;
// 0x01020304
color c4 = c2 / 2;
color c1(0xFF112233);
uint8_t a = get<0>(c1); // 0xFF
uint8_t r = get<1>(c1); // 0x11
uint8_t g = get<2>(c1); // 0x22
uint8_t b = get<3>(c1); // 0x33
Trying to access an invalid index, causes an error at the compile-time.
A color value can be represented in a hexadecimal notation #RRGGBB
or #AARRGGBB
color c1(0x01020304);
// without alpha channel
std::string s1 = to_hex_str<char>(c1); // "#020304"
std::wstring ws1 = to_hex_str<wchar_t>(c1); // L"#020304"
// with alpha channel
std::string s2 = to_ahex_str<char>(c1); // "#01020304"
std::wstring ws2 = to_ahex_str<wchar_t>(c1); // L"#01020304"
A color value can be represented in a functional notation rgb(R,G,B)
or argb(A,R,G,B)
color c1(0x01020304);
std::string s3 = to_rgb_str<char>(c1); // "rgb(2,3,4)"
std::wstring ws3 = to_rgb_str<wchar_t>(c1); // L"rgb(2,3,4)"
std::string s4 = to_argb_str<char>(c1); // "argb(1,2,3,4)"
std::wstring ws4 = to_argb_str<wchar_t>(c1); // L"argb(1,2,3,4)"
When written to a stream, a color saved in the functional notation argb(A,R,G,B)
color c1(0x01020304);
std::ostringstream oss;
oss << c1; // "argb(1,2,3,4)"
Using boost::lexical_cast
color c1(0x01020304);
std::string clr_str = boost::lexical_cast<std::string>(c1); // "argb(1,2,3,4)"
A string representation of a color can be parsed to a color
data type.
color c2(255, 32, 64, 128);
// ARGB
assert(c2 == boost::lexical_cast<color>("#ff204080"));
// RGB
assert(c2 == boost::lexical_cast<color>("#204080"));
// ARGB
assert(c2 == boost::lexical_cast<color>("argb(255,32,64,128)"));
// RGB
assert(c2 == boost::lexical_cast<color>("rgb(32,64,128)"));
// with spaces
assert(c2 == boost::lexical_cast<color>("rgb(32, 64, 128)"));
stringstream ss("argb(255, 10, 20, 30)");
color c;
ss >> c; // A:255, R:10, G:20, B:30