From 8170593d56b196da3234b26967f8f7938a16225b Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 10 Oct 2023 10:49:13 -0400 Subject: [PATCH] Add Color struct --- src/fart/color.d | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/fart/color.d diff --git a/src/fart/color.d b/src/fart/color.d new file mode 100644 index 0000000..822e3c4 --- /dev/null +++ b/src/fart/color.d @@ -0,0 +1,43 @@ +module fart.color; + +/** + * Structure to represent a RGBA color value. + */ +struct Color +{ + /** Red color component. */ + double r; + + /** Green color component. */ + double g; + + /** Blue color component. */ + double b; + + /** Alpha color component. */ + double a; + + /** + * Convert color to a 32-bit integer RGBA value. + * + * Each color component uses one byte. + */ + public ubyte[] rgba32() const + { + return [toubyte(r), toubyte(g), toubyte(b), toubyte(a)]; + } + + /** + * Scale a floating point color component value to an unsigned 8-bit byte + * value. + * + * @param v + * Floating point color component value. + * + * @return Unsigned 8-bit byte value for the color component. + */ + private static ubyte toubyte(double v) + { + return cast(ubyte)(0xFF * v); + } +}