Refactor color utils (#955)

Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com>
This commit is contained in:
Murat Seker
2020-08-08 13:22:37 +02:00
committed by GitHub
parent 63d95a5a2a
commit a18ccb8b48
20 changed files with 181 additions and 120 deletions

View File

@@ -20,17 +20,17 @@ struct ColorRgb
uint8_t blue;
/// 'Black' RgbColor (0, 0, 0)
static ColorRgb BLACK;
static const ColorRgb BLACK;
/// 'Red' RgbColor (255, 0, 0)
static ColorRgb RED;
static const ColorRgb RED;
/// 'Green' RgbColor (0, 255, 0)
static ColorRgb GREEN;
static const ColorRgb GREEN;
/// 'Blue' RgbColor (0, 0, 255)
static ColorRgb BLUE;
static const ColorRgb BLUE;
/// 'Yellow' RgbColor (255, 255, 0)
static ColorRgb YELLOW;
static const ColorRgb YELLOW;
/// 'White' RgbColor (255, 255, 255)
static ColorRgb WHITE;
static const ColorRgb WHITE;
};
/// Assert to ensure that the size of the structure is 'only' 3 bytes
@@ -45,7 +45,12 @@ static_assert(sizeof(ColorRgb) == 3, "Incorrect size of ColorRgb");
///
inline std::ostream& operator<<(std::ostream& os, const ColorRgb& color)
{
os << "{" << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
os << "{"
<< color.red << ","
<< color.green << ","
<< color.blue
<< "}";
return os;
}
@@ -58,30 +63,51 @@ inline std::ostream& operator<<(std::ostream& os, const ColorRgb& color)
///
inline QTextStream& operator<<(QTextStream &os, const ColorRgb& color)
{
os << "{" << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
os << "{"
<< color.red << ","
<< color.green << ","
<< color.blue
<< "}";
return os;
}
/// Compare operator to check if a color is 'equal' to another color
inline bool operator==(const ColorRgb & lhs, const ColorRgb & rhs)
{
return lhs.red == rhs.red &&
lhs.green == rhs.green &&
lhs.blue == rhs.blue;
}
/// Compare operator to check if a color is 'smaller' than another color
inline bool operator<(const ColorRgb & lhs, const ColorRgb & rhs)
{
return (lhs.red < rhs.red) && (lhs.green < rhs.green) && (lhs.blue < rhs.blue);
return lhs.red < rhs.red &&
lhs.green < rhs.green &&
lhs.blue < rhs.blue;
}
/// Compare operator to check if a color is 'not equal' to another color
inline bool operator!=(const ColorRgb & lhs, const ColorRgb & rhs)
{
return !(lhs == rhs);
}
/// Compare operator to check if a color is 'smaller' than or 'equal' to another color
inline bool operator<=(const ColorRgb & lhs, const ColorRgb & rhs)
{
return (lhs.red <= rhs.red) && (lhs.green <= rhs.green) && (lhs.blue <= rhs.blue);
return lhs < rhs || lhs == rhs;
}
/// Compare operator to check if a color is 'greater' to another color
inline bool operator>(const ColorRgb & lhs, const ColorRgb & rhs)
{
return (lhs.red > rhs.red) && (lhs.green > rhs.green) && (lhs.blue > rhs.blue);
return !(lhs < rhs) && lhs != rhs;
}
/// Compare operator to check if a color is 'greater' than or 'equal' to another color
inline bool operator>=(const ColorRgb & lhs, const ColorRgb & rhs)
{
return (lhs.red >= rhs.red) && (lhs.green >= rhs.green) && (lhs.blue >= rhs.blue);
return lhs > rhs || lhs == rhs;
}