Created
August 27, 2013 06:48
-
-
Save isoiphone/6350369 to your computer and use it in GitHub Desktop.
A useful RGBA 8888 colour class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// somewhere | |
const Color4b Color4b::White = Color4b(0xFF,0xFF,0xFF,0xFF); | |
const Color4b Color4b::Black = Color4b(0x00,0x00,0x00,0xFF); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class Color4b { | |
public: | |
Color4b() | |
: r(0xFF) | |
, g(0x00) | |
, b(0xFF) | |
, a(0xFF) | |
{} | |
Color4b(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a=0xFF) | |
: r(_r) | |
, g(_g) | |
, b(_b) | |
, a(_a) | |
{} | |
static Color4b fromHEX(const char* pszHex) { | |
if (!pszHex || strlen(pszHex) != 6) { | |
return Color4b(); | |
} | |
const uint32_t num = strtoll(pszHex, NULL, 16); | |
return Color4b(((num&0xFF0000)>>16), ((num&0x00FF00)>>8), (num&0x0000FF)); | |
} | |
static Color4b fromHSV(float h, float s, float v) { | |
const int hi = (int)(h / 60.0)%6; | |
const float f = h/60.0 - floor(h/60.0); | |
const float p = v * (1.0 - s); | |
const float q = v * (1.0 - (f*s)); | |
const float t = v * (1.0 - ((1.0 - f) * s)); | |
switch (hi) { | |
case 0: | |
return Color4b(v,t,p); | |
case 1: | |
return Color4b(q,v,p); | |
case 2: | |
return Color4b(p,v,t); | |
case 3: | |
return Color4b(p,q,v); | |
case 4: | |
return Color4b(t,p,v); | |
case 5: | |
return Color4b(v,p,q); | |
} | |
return Color4b(); | |
} | |
inline uint8_t* v() { return &r; } | |
uint8_t r,g,b,a; | |
static const Color4b White; | |
static const Color4b Black; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment