Skip to content

Instantly share code, notes, and snippets.

@isoiphone
Created August 27, 2013 06:48
Show Gist options
  • Save isoiphone/6350369 to your computer and use it in GitHub Desktop.
Save isoiphone/6350369 to your computer and use it in GitHub Desktop.
A useful RGBA 8888 colour class.
// somewhere
const Color4b Color4b::White = Color4b(0xFF,0xFF,0xFF,0xFF);
const Color4b Color4b::Black = Color4b(0x00,0x00,0x00,0xFF);
#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