Img2Num C++ (Internal Developer Docs) dev
API Documentation
Loading...
Searching...
No Matches
RGBPixel.h
1#ifndef RGBPIXEL_H
2#define RGBPIXEL_H
3
4#include "internal/Pixel.h"
5
6#include <cmath>
7#include <iostream>
8
9namespace ImageLib {
10
11#ifdef _MSC_VER
12#pragma pack(push, 1)
13#endif
14template <typename NumberT> struct RGBPixel : public Pixel<NumberT> {
15 // ----- Members -----
16 NumberT red, green, blue;
17
18 constexpr RGBPixel(NumberT red = 0, NumberT green = 0, NumberT blue = 0)
19 : red(red)
20 , green(green)
21 , blue(blue) {
22 }
23
24 // ----- Modifiers -----
25 [[nodiscard]] inline bool operator==(const RGBPixel& other) const {
26 return red == other.red && green == other.green && blue == other.blue;
27 }
28 [[nodiscard]] inline bool operator!=(const RGBPixel& other) const {
29 return !(*this == other);
30 }
31
32 // ----- Utilities -----
33 inline void setGray(NumberT gray) {
34 red = green = blue = gray;
35 }
36
37 static inline float colorDistance(const RGBPixel<NumberT>& a, const RGBPixel<NumberT>& b) {
39 static_cast<float>(a.red), static_cast<float>(a.green), static_cast<float>(a.blue)};
41 static_cast<float>(b.red), static_cast<float>(b.green), static_cast<float>(b.blue)};
42 return std::sqrt(
43 (af.red - bf.red) * (af.red - bf.red) + (af.green - bf.green) * (af.green - bf.green) +
44 (af.blue - bf.blue) * (af.blue - bf.blue)
45 );
46 }
47
48}
49#ifndef _MSC_VER
50__attribute__((packed))
51#endif
52;
53#ifdef _MSC_VER
54#pragma pack(pop)
55#endif
56
57template <typename NumberT>
58std::ostream& operator<<(std::ostream& out, const ImageLib::RGBPixel<NumberT>& pixel) {
59 out << "( " << pixel.red << "," << pixel.green << "," << pixel.blue << " )";
60 return out;
61}
62
63} // namespace ImageLib
64
65#endif // RGBPIXEL_H