Hey I wrote a struct in C++ trying to represent a Pixel Component of a color Here it is
template<typename T, T min, T max>
struct PixelComponent {
static constexpr T MIN = min;
static constexpr T MAX = max;
T value;
PixelComponent() = default;
PixelComponent(T value) : value(value) {}
inline bool operator==(const PixelComponent &other) {
return value == other.value;
}
inline bool operator!=(const PixelComponent &other) {
return !(*this == other);
}
inline bool operator>(const PixelComponent &other) {
return value > other.value;
}
inline bool operator<(const PixelComponent &other) {
return !(*this > other) && *this != other;
}
inline bool operator>=(const PixelComponent& other){
return !(*this < other);
}
inline bool operator<=(const PixelComponent& other){
return !(*this > other);
}
inline PixelComponent &operator=(const T &elem) {
if (value == elem) {
return *this;
}
value = elem > MAX ? MAX : elem < MIN ? MIN : elem;
return *this;
}
inline PixelComponent &operator=(const PixelComponent &other) {
if (*this == other) {
return *this;
}
*this = other.value;
return *this;
}
inline PixelComponent &operator++(){
*this = ++value;
return *this;
}
inline PixelComponent &operator--(){
*this = --value;
return *this;
}
inline PixelComponent operator+(const T& elem){
PixelComponent result;
result = value + elem;
return result;
}
inline PixelComponent operator-(const T& elem){
PixelComponent result;
result = value - elem;
return result;
}
inline PixelComponent operator*(const T& elem){
PixelComponent result;
result = value * elem;
return result;
}
inline PixelComponent operator/(const T& elem){
PixelComponent result;
result = value / elem;
return result;
}
inline PixelComponent operator+(const PixelComponent& other){
PixelComponent result;
result = value + other.value;
return result;
}
inline PixelComponent operator-(const PixelComponent& other){
PixelComponent result;
result = value - other.value;
return result;
}
inline PixelComponent operator*(const PixelComponent& other){
PixelComponent result;
result = value * other.value;
return result;
}
inline PixelComponent operator/(const PixelComponent& other){
PixelComponent result;
result = value / other.value;
return result;
}
inline PixelComponent operator+=(const T& elem){
*this = *this + elem;
return *this;
}
inline PixelComponent operator-=(const T& elem){
*this = *this - elem;
return *this;
}
inline PixelComponent operator*=(const T& elem){
*this = *this * elem;
return *this;
}
inline PixelComponent operator/=(const T& elem){
*this = *this / elem;
return *this;
}
inline PixelComponent& operator+=(const PixelComponent& other){
*this = *this + other;
return *this;
}
inline PixelComponent& operator-=(const PixelComponent& other){
*this = *this - other;
return *this;
}
inline PixelComponent& operator*=(const PixelComponent& other){
*this = *this * other;
return *this;
}
inline PixelComponent& operator/=(const PixelComponent& other){
*this = *this / other;
return *this;
}
};
template<typename T, T min, T max>
std::ostream &operator<<(std::ostream &os, const PixelComponent<T, min, max> &component) {
os << component.value;
return os;
}
Please review this. I will be thankful for any suggestion or typos noticed) Please also review implementation of value always staying in range [MIN,MAX]. I have not added that validation in constructor, is there a nice way to do that?
inlinerequest, code in a class declaration are inlined by default. \$\endgroup\$