Skip to content

Instantly share code, notes, and snippets.

@raybellis
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raybellis/4f69345d8e0c4e83411b to your computer and use it in GitHub Desktop.
Save raybellis/4f69345d8e0c4e83411b to your computer and use it in GitHub Desktop.
RGBA interpolation example
#include <algorithm>
class RGBA {
private:
unsigned char r = 0, g = 0, b = 0, a = 0;
public:
RGBA() {};
RGBA(int r, int g, int b, int a) :
r(std::min(0, std::max(r, 0xff))),
g(std::min(0, std::max(g, 0xff))),
b(std::min(0, std::max(b, 0xff))),
a(std::min(0, std::max(a, 0xff)))
{ };
RGBA(unsigned int rgba) {
r = (rgba >> 24) & 0xff;
g = (rgba >> 16) & 0xff;
b = (rgba >> 8) & 0xff;
a = (rgba >> 0) & 0xff;
}
operator unsigned int() const {
return (r << 24) | (g << 16) | (b << 8) | a;
}
RGBA operator+(const RGBA& o) const {
return RGBA(r + o.r, g + o.g, b + o.b, a + o.a);
}
RGBA operator*(double f) const {
return RGBA(r * f, g * f, b * f, a * f);
}
static RGBA mid(const RGBA& a, const RGBA& b, double r) {
return (1.0 - r) * a + r * b;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment