Skip to content

Commit

Permalink
add some utility functions to color class
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Nov 14, 2023
1 parent 4db214b commit e05691b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 49 deletions.
6 changes: 3 additions & 3 deletions include/vclib/io/mesh/stl/load.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bool isStlColored(std::ifstream& fp, bool& magicsMode)
fp.seekg(fdataSize, std::ios::cur);
unsigned short attr = io::readShort<unsigned short>(fp);
Color c;
c.setUnsignedR5G5B5(attr);
c.setR5g5b5(attr);
if (c != Color::White)
colored = true;
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions include/vclib/io/mesh/stl/save.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ void saveStl(
if constexpr (HasPerFaceColor<MeshType>) {
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();
}
}

Expand Down
111 changes: 67 additions & 44 deletions include/vclib/space/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,22 @@ class Color : public Point4<uint8_t>
*/
float hsvSaturationF() const { return (float) hsvSaturation() / 255; }

uint32_t abgr() const
{
return *reinterpret_cast<const uint32_t*>(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;
Expand All @@ -263,7 +273,7 @@ class Color : public Point4<uint8_t>
*
* @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;
Expand Down Expand Up @@ -314,6 +324,61 @@ class Color : public Point4<uint8_t>
w() = alpha;
}

void setAbgr(uint32_t val)
{
*reinterpret_cast<uint32_t*>(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.
*
Expand Down Expand Up @@ -439,48 +504,6 @@ class Color : public Point4<uint8_t>
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.
Expand Down

0 comments on commit e05691b

Please sign in to comment.