-
-
Save raybellis/4f69345d8e0c4e83411b to your computer and use it in GitHub Desktop.
RGBA interpolation example
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
#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