Img2Num C++ (Internal Developer Docs)  dev
API Documentation
RGBPixel.h
1 #ifndef RGBPIXEL_H
2 #define RGBPIXEL_H
3 
4 #include <cmath>
5 
6 #include "internal/Pixel.h"
7 
8 namespace ImageLib {
9 template <typename NumberT>
10 struct RGBPixel : public Pixel<NumberT> {
11  // ----- Members -----
12  NumberT red, green, blue;
13 
14  constexpr RGBPixel(NumberT red = 0, NumberT green = 0, NumberT blue = 0)
15  : red(red), green(green), blue(blue) {
16  }
17 
18  // ----- Modifiers -----
19  [[nodiscard]] inline bool operator==(const RGBPixel &other) const {
20  return red == other.red && green == other.green && blue == other.blue;
21  }
22  [[nodiscard]] inline bool operator!=(const RGBPixel &other) const {
23  return !(*this == other);
24  }
25 
26  // ----- Utilities -----
27  inline void setGray(NumberT gray) {
28  red = green = blue = gray;
29  }
30 
31  static inline float colorDistance(const RGBPixel<NumberT> &a, const RGBPixel<NumberT> &b) {
32  RGBPixel<float> af{static_cast<float>(a.red), static_cast<float>(a.green),
33  static_cast<float>(a.blue)};
34  RGBPixel<float> bf{static_cast<float>(b.red), static_cast<float>(b.green),
35  static_cast<float>(b.blue)};
36  return std::sqrt((af.red - bf.red) * (af.red - bf.red) +
37  (af.green - bf.green) * (af.green - bf.green) +
38  (af.blue - bf.blue) * (af.blue - bf.blue));
39  }
40 
41 } __attribute__((packed));
42 } // namespace ImageLib
43 
44 #endif // RGBPIXEL_H