Provide red/green/blue/alpha getters for Rust and C++ Color

And fix up the C++ class docs.
This commit is contained in:
Simon Hausmann 2020-09-08 22:31:41 +02:00
parent 09142beac4
commit 52b19606d1
2 changed files with 42 additions and 0 deletions

View file

@ -16,10 +16,15 @@ LICENSE END */
namespace sixtyfps { namespace sixtyfps {
/// Color represents a color in the SixtyFPS run-time, represented using 8-bit channels for
/// red, green, blue and the alpha (opacity).
class Color class Color
{ {
public: public:
/// Default constructs a new color that is entirely transparent.
Color() { inner.red = inner.green = inner.blue = inner.alpha = 0; } Color() { inner.red = inner.green = inner.blue = inner.alpha = 0; }
/// Construct a color from an integer encoded as `0xAARRGGBB`
static Color from_argb_encoded(uint32_t argb_encoded) static Color from_argb_encoded(uint32_t argb_encoded)
{ {
Color col; Color col;
@ -30,18 +35,35 @@ public:
return col; return col;
} }
/// Returns `(alpha, red, green, blue)` encoded as uint32_t.
uint32_t as_argb_encoded() const uint32_t as_argb_encoded() const
{ {
return (uint32_t(inner.red) << 16) | (uint32_t(inner.green) << 8) | uint32_t(inner.blue) return (uint32_t(inner.red) << 16) | (uint32_t(inner.green) << 8) | uint32_t(inner.blue)
| (uint32_t(inner.alpha) << 24); | (uint32_t(inner.alpha) << 24);
} }
/// Returns the red channel of the color as u8 in the range 0..255.
uint8_t red() const { return inner.red; }
/// Returns the green channel of the color as u8 in the range 0..255.
uint8_t green() const { return inner.green; }
/// Returns the blue channel of the color as u8 in the range 0..255.
uint8_t blue() const { return inner.blue; }
/// Returns the alpha channel of the color as u8 in the range 0..255.
uint8_t alpha() const { return inner.alpha; }
/// Returns true if \a lhs has the same values for the individual color channels as \rhs; false
/// otherwise.
friend bool operator==(const Color &lhs, const Color &rhs) friend bool operator==(const Color &lhs, const Color &rhs)
{ {
return lhs.inner.red == rhs.inner.red && lhs.inner.green == rhs.inner.green return lhs.inner.red == rhs.inner.red && lhs.inner.green == rhs.inner.green
&& lhs.inner.blue == rhs.inner.blue && lhs.inner.alpha == rhs.inner.alpha; && lhs.inner.blue == rhs.inner.blue && lhs.inner.alpha == rhs.inner.alpha;
} }
/// Returns true if \a lhs has any different values for the individual color channels as \rhs;
/// false otherwise.
friend bool operator!=(const Color &lhs, const Color &rhs) { return !(lhs == rhs); } friend bool operator!=(const Color &lhs, const Color &rhs) { return !(lhs == rhs); }
private: private:

View file

@ -124,6 +124,26 @@ impl Color {
| (self.blue as u32) | (self.blue as u32)
| ((self.alpha as u32) << 24) | ((self.alpha as u32) << 24)
} }
/// Returns the red channel of the color as u8 in the range 0..255.
pub fn red(self) -> u8 {
self.red
}
/// Returns the green channel of the color as u8 in the range 0..255.
pub fn green(self) -> u8 {
self.green
}
/// Returns the blue channel of the color as u8 in the range 0..255.
pub fn blue(self) -> u8 {
self.blue
}
/// Returns the alpha channel of the color as u8 in the range 0..255.
pub fn alpha(self) -> u8 {
self.alpha
}
} }
impl InterpolatedPropertyValue for Color { impl InterpolatedPropertyValue for Color {