123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #ifndef COLORRGBSCALAR_H
- #define COLORRGBSCALAR_H
- // STL includes
- #include <cstdint>
- #include <iostream>
- #include <QString>
- #include <QTextStream>
- #include <QRgb>
- #include <utils/ColorRgb.h>
- ///
- /// Plain-Old-Data structure containing the red-green-blue color specification. Size of the
- /// structure is exactly 3 times int for easy writing to led-device
- ///
- struct ColorRgbScalar
- {
- /// The red color channel
- int red;
- /// The green color channel
- int green;
- /// The blue color channel
- int blue;
- /// 'Black' RgbColor (0, 0, 0)
- static const ColorRgbScalar BLACK;
- /// 'Red' RgbColor (255, 0, 0)
- static const ColorRgbScalar RED;
- /// 'Green' RgbColor (0, 255, 0)
- static const ColorRgbScalar GREEN;
- /// 'Blue' RgbColor (0, 0, 255)
- static const ColorRgbScalar BLUE;
- /// 'Yellow' RgbColor (255, 255, 0)
- static const ColorRgbScalar YELLOW;
- /// 'White' RgbColor (255, 255, 255)
- static const ColorRgbScalar WHITE;
- ColorRgbScalar() = default;
- ColorRgbScalar(int _red, int _green,int _blue):
- red(_red),
- green(_green),
- blue(_blue)
- {
- }
- ColorRgbScalar(ColorRgb rgb):
- red(rgb.red),
- green(rgb.green),
- blue(rgb.blue)
- {
- }
- ColorRgbScalar operator-(const ColorRgbScalar& b) const
- {
- ColorRgbScalar a(*this);
- a.red -= b.red;
- a.green -= b.green;
- a.blue -= b.blue;
- return a;
- }
- void setRgb(QRgb rgb)
- {
- red = qRed(rgb);
- green = qGreen(rgb);
- blue = qBlue(rgb);
- }
- void setRgb(ColorRgb rgb)
- {
- red = rgb.red;
- green = rgb.green;
- blue = rgb.blue;
- }
- QString toQString() const
- {
- return QString("(%1,%2,%3)").arg(red).arg(green).arg(blue);
- }
- };
- /// Assert to ensure that the size of the structure is 'only' 3 times int
- static_assert(sizeof(ColorRgbScalar) == 3 * sizeof(int), "Incorrect size of ColorRgbInt");
- ///
- /// Stream operator to write ColorRgbInt to an outputstream (format "'{'[red]','[green]','[blue]'}'")
- ///
- /// @param os The output stream
- /// @param color The color to write
- /// @return The output stream (with the color written to it)
- ///
- inline std::ostream& operator<<(std::ostream& os, const ColorRgbScalar& color)
- {
- os << "{"
- << static_cast<unsigned>(color.red) << ","
- << static_cast<unsigned>(color.green) << ","
- << static_cast<unsigned>(color.blue)
- << "}";
- return os;
- }
- ///
- /// Stream operator to write ColorRgbInt to a QTextStream (format "'{'[red]','[green]','[blue]'}'")
- ///
- /// @param os The output stream
- /// @param color The color to write
- /// @return The output stream (with the color written to it)
- ///
- inline QTextStream& operator<<(QTextStream &os, const ColorRgbScalar& color)
- {
- os << "{"
- << static_cast<unsigned>(color.red) << ","
- << static_cast<unsigned>(color.green) << ","
- << static_cast<unsigned>(color.blue)
- << "}";
- return os;
- }
- /// Compare operator to check if a color is 'equal' to another color
- inline bool operator==(const ColorRgbScalar & lhs, const ColorRgbScalar & 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 ColorRgbScalar & lhs, const ColorRgbScalar & rhs)
- {
- 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 ColorRgbScalar & lhs, const ColorRgbScalar & rhs)
- {
- return !(lhs == rhs);
- }
- /// Compare operator to check if a color is 'smaller' than or 'equal' to another color
- inline bool operator<=(const ColorRgbScalar & lhs, const ColorRgbScalar & rhs)
- {
- return lhs.red <= rhs.red &&
- lhs.green <= rhs.green &&
- lhs.blue <= rhs.blue;
- }
- /// Compare operator to check if a color is 'greater' to another color
- inline bool operator>(const ColorRgbScalar & lhs, const ColorRgbScalar & rhs)
- {
- return lhs.red > rhs.red &&
- lhs.green > rhs.green &&
- lhs.blue > rhs.blue;
- }
- /// Compare operator to check if a color is 'greater' than or 'equal' to another color
- inline bool operator>=(const ColorRgbScalar & lhs, const ColorRgbScalar & rhs)
- {
- return lhs.red >= rhs.red &&
- lhs.green >= rhs.green &&
- lhs.blue >= rhs.blue;
- }
- inline ColorRgbScalar& operator+=(ColorRgbScalar& lhs, const ColorRgbScalar& rhs)
- {
- lhs.red += rhs.red;
- lhs.green += rhs.green;
- lhs.blue += rhs.blue;
- return lhs;
- }
- inline ColorRgbScalar operator+(ColorRgbScalar lhs, const ColorRgbScalar rhs)
- {
- lhs += rhs;
- return lhs;
- }
- inline ColorRgbScalar& operator/=(ColorRgbScalar& lhs, int count)
- {
- if (count > 0)
- {
- lhs.red /= count;
- lhs.green /= count;
- lhs.blue /= count;
- }
- return lhs;
- }
- inline ColorRgbScalar operator/(ColorRgbScalar lhs, int count)
- {
- lhs /= count;
- return lhs;
- }
- #endif // COLORRGBSCALAR_H
|