From e05691bc9cb7bdedac7143923d7fe81ecdec5956 Mon Sep 17 00:00:00 2001 From: Alessandro Muntoni Date: Tue, 14 Nov 2023 17:02:40 +0100 Subject: [PATCH] add some utility functions to color class --- include/vclib/io/mesh/stl/load.h | 6 +- include/vclib/io/mesh/stl/save.h | 4 +- include/vclib/space/color.h | 111 +++++++++++++++++++------------ 3 files changed, 72 insertions(+), 49 deletions(-) diff --git a/include/vclib/io/mesh/stl/load.h b/include/vclib/io/mesh/stl/load.h index f191ce436..11bb1c7d5 100644 --- a/include/vclib/io/mesh/stl/load.h +++ b/include/vclib/io/mesh/stl/load.h @@ -85,7 +85,7 @@ bool isStlColored(std::ifstream& fp, bool& magicsMode) fp.seekg(fdataSize, std::ios::cur); unsigned short attr = io::readShort(fp); Color c; - c.setUnsignedR5G5B5(attr); + c.setR5g5b5(attr); if (c != Color::White) colored = true; } @@ -159,9 +159,9 @@ void readStlBin( if (isPerFaceColorAvailable(m) && colored) { Color c; if (magicsMode) - c.setUnsignedR5G5B5(attr); + c.setR5g5b5(attr); else - c.setUnsignedB5G5R5(attr); + c.setB5g5r5(attr); f.color() = c; } } diff --git a/include/vclib/io/mesh/stl/save.h b/include/vclib/io/mesh/stl/save.h index 0c0cacdc3..0a278e871 100644 --- a/include/vclib/io/mesh/stl/save.h +++ b/include/vclib/io/mesh/stl/save.h @@ -152,9 +152,9 @@ void saveStl( if constexpr (HasPerFaceColor) { if (meshInfo.hasFaceColors()) { if (magicsMode) - attributes = 32768 | f.color().unsignedR5G5B5(); + attributes = 32768 | f.color().r5g5b5(); else - attributes = 32768 | f.color().unsignedB5G5R5(); + attributes = 32768 | f.color().b5g5r5(); } } diff --git a/include/vclib/space/color.h b/include/vclib/space/color.h index fd48cb325..e9dd1054e 100644 --- a/include/vclib/space/color.h +++ b/include/vclib/space/color.h @@ -244,12 +244,22 @@ class Color : public Point4 */ float hsvSaturationF() const { return (float) hsvSaturation() / 255; } + uint32_t abgr() const + { + return *reinterpret_cast(Point::p.data()); + } + + uint32_t rgba() const + { + return (alpha() << 24) | (blue() << 16) | (green() << 8) | red(); + } + /** * @brief Converts the color to an unsigned short in R5G5B5 format. * * @return an unsigned short containing the converted color. */ - unsigned short unsignedR5G5B5() const + unsigned short r5g5b5() const { unsigned short r = x() / 8; unsigned short g = y() / 8; @@ -263,7 +273,7 @@ class Color : public Point4 * * @return an unsigned short containing the converted color. */ - unsigned short unsignedB5G5R5() const + unsigned short b5g5r5() const { unsigned short r = x() / 8; unsigned short g = y() / 8; @@ -314,6 +324,61 @@ class Color : public Point4 w() = alpha; } + void setAbgr(uint32_t val) + { + *reinterpret_cast(Point::p.data()) = val; + } + + void setRgba(uint32_t val) + { + w() = val % 256; + z() = (val >> 8) % 256; + y() = (val >> 16) % 256; + x() = (val >> 24) % 256; + } + + /** + * Set the color values from an unsigned 5-5-5 RGB value. + * + * The input value is interpreted as follows: + * - The 5 least significant bits represent the red component. + * - The next 5 bits represent the green component. + * - The 5 most significant bits represent the blue component. + * + * Each color component is scaled from 0 to 255 by multiplying the value + * by 8. + * + * @param[in] val: The unsigned 5-5-5 RGB value to set. + */ + void setR5g5b5(unsigned short val) + { + x() = val % 32 * 8; + y() = ((val / 32) % 32) * 8; + z() = ((val / 1024) % 32) * 8; + w() = 255; + } + + /** + * Set the color values from an unsigned 5-5-5 BGR value. + * + * The input value is interpreted as follows: + * - The 5 least significant bits represent the blue component. + * - The next 5 bits represent the green component. + * - The 5 most significant bits represent the red component. + * + * Each color component is scaled from 0 to 255 by multiplying the value + * by 8. + * + * @param[in] val: The unsigned 5-5-5 BGR value to set. + */ + void setB5g5r5(unsigned short val) + { + z() = val % 32 * 8; + y() = ((val / 32) % 32) * 8; + x() = ((val / 1024) % 32) * 8; + w() = 255; + } + /** * @brief Sets the HSV values of this color. * @@ -439,48 +504,6 @@ class Color : public Point4 setHsv(hf * 255, sf * 255, vf * 255, alpha * 255); } - /** - * Set the color values from an unsigned 5-5-5 RGB value. - * - * The input value is interpreted as follows: - * - The 5 least significant bits represent the red component. - * - The next 5 bits represent the green component. - * - The 5 most significant bits represent the blue component. - * - * Each color component is scaled from 0 to 255 by multiplying the value - * by 8. - * - * @param[in] val: The unsigned 5-5-5 RGB value to set. - */ - void setUnsignedR5G5B5(unsigned short val) - { - x() = val % 32 * 8; - y() = ((val / 32) % 32) * 8; - z() = ((val / 1024) % 32) * 8; - w() = 255; - } - - /** - * Set the color values from an unsigned 5-5-5 BGR value. - * - * The input value is interpreted as follows: - * - The 5 least significant bits represent the blue component. - * - The next 5 bits represent the green component. - * - The 5 most significant bits represent the red component. - * - * Each color component is scaled from 0 to 255 by multiplying the value - * by 8. - * - * @param[in] val: The unsigned 5-5-5 BGR value to set. - */ - void setUnsignedB5G5R5(unsigned short val) - { - z() = val % 32 * 8; - y() = ((val / 32) % 32) * 8; - x() = ((val / 1024) % 32) * 8; - w() = 255; - } - /** * @brief Returns true if this color has the same RGB and alpha values * as otherColor; otherwise returns false.