supereight
Loading...
Searching...
No Matches
rgb.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 Smart Robotics Lab, Imperial College London, Technical University of Munich
3 * SPDX-FileCopyrightText: 2024 Sotiris Papatheodorou
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef SE_COMMON_RGB_HPP
8#define SE_COMMON_RGB_HPP
9
10#include <cassert>
11#include <cstdint>
12#include <limits>
13#include <tuple>
14
15namespace se {
16
18struct RGB {
19 std::uint8_t r = 0x00;
20 std::uint8_t g = 0x00;
21 std::uint8_t b = 0x00;
22
23
28 // Passing lhs by value helps optimize chained additions:
29 // https://en.cppreference.com/w/cpp/language/operators#Binary_arithmetic_operators
30 friend RGB operator+(RGB lhs, const RGB rhs)
31 {
32 // Asserts work due to implicit conversion of std::uint8_t to int for arithmetic operators.
33 assert(lhs.r + rhs.r <= std::numeric_limits<std::uint8_t>::max() && "Avoid overflow.");
34 assert(lhs.g + rhs.g <= std::numeric_limits<std::uint8_t>::max() && "Avoid overflow.");
35 assert(lhs.b + rhs.b <= std::numeric_limits<std::uint8_t>::max() && "Avoid overflow.");
36 lhs.r += rhs.r;
37 lhs.g += rhs.g;
38 lhs.b += rhs.b;
39 return lhs;
40 }
41
45 friend RGB operator*(RGB lhs, const float t)
46 {
47 assert(t >= 0.0f && "Avoid under/overflow.");
48 assert(t <= 1.0f && "Avoid under/overflow.");
49 lhs.r *= t;
50 lhs.g *= t;
51 lhs.b *= t;
52 return lhs;
53 }
54
56 friend RGB operator*(const float t, RGB rhs)
57 {
58 return rhs * t;
59 }
60
61
62 friend bool operator==(const RGB lhs, const RGB rhs)
63 {
64 return lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b;
65 }
66
67 friend bool operator!=(const RGB lhs, const RGB rhs)
68 {
69 return !(lhs == rhs);
70 }
71
72 friend bool operator<(const RGB lhs, const RGB rhs)
73 {
74 // Perform lexicographical comparison.
75 return std::tie(lhs.r, lhs.g, lhs.b) < std::tie(rhs.r, rhs.g, rhs.b);
76 }
77
78 friend bool operator>(const RGB lhs, const RGB rhs)
79 {
80 return rhs < lhs;
81 }
82
83 friend bool operator<=(const RGB lhs, const RGB rhs)
84 {
85 return !(lhs > rhs);
86 }
87
88 friend bool operator>=(const RGB lhs, const RGB rhs)
89 {
90 return !(lhs < rhs);
91 }
92};
93
94} // namespace se
95
96#endif // SE_COMMON_RGB_HPP
Definition image.hpp:19
Helper wrapper to allocate and de-allocate octants in the octree.
Definition bounded_vector.hpp:14
A colour represented as a Red-Green-Blue tuple with 8-bits per channel.
Definition rgb.hpp:18
friend bool operator<(const RGB lhs, const RGB rhs)
Definition rgb.hpp:72
friend bool operator!=(const RGB lhs, const RGB rhs)
Definition rgb.hpp:67
friend RGB operator*(const float t, RGB rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition rgb.hpp:56
std::uint8_t b
Definition rgb.hpp:21
friend bool operator==(const RGB lhs, const RGB rhs)
Definition rgb.hpp:62
std::uint8_t r
Definition rgb.hpp:19
friend RGB operator*(RGB lhs, const float t)
Scale all channels of rhs by the factor t in the interval [0, 1] inclusive.
Definition rgb.hpp:45
friend bool operator>=(const RGB lhs, const RGB rhs)
Definition rgb.hpp:88
friend bool operator>(const RGB lhs, const RGB rhs)
Definition rgb.hpp:78
friend bool operator<=(const RGB lhs, const RGB rhs)
Definition rgb.hpp:83
std::uint8_t g
Definition rgb.hpp:20
friend RGB operator+(RGB lhs, const RGB rhs)
Add lhs and rhs channel-wise.
Definition rgb.hpp:30