Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed ros_lib_kinetic
RGBA.h
00001 #ifndef RGBA_H 00002 #define RGBA_H 00003 00004 #include <iostream> 00005 #include <cmath> 00006 00007 #define for_i for(std::size_t i = 0; i != 4; ++i) 00008 00009 template <typename T> 00010 struct RGBA 00011 { 00012 union { 00013 struct { 00014 T r; 00015 T g; 00016 T b; 00017 T a; 00018 }; 00019 T elem[4]; 00020 }; 00021 00022 // CONSTRUCTORS 00023 00024 RGBA() { 00025 for_i elem[i] = T(); 00026 } 00027 explicit RGBA(T b) { 00028 for_i elem[i] = b; 00029 } 00030 RGBA(T b0, T b1, T b2, T b3) { 00031 elem[0] = b0; elem[1] = b1; elem[2] = b2; elem[3] = b3; 00032 } 00033 00034 // MODIFIERS 00035 00036 /** Add scalar @a b to this RGBA */ 00037 RGBA& operator+=(T b) { 00038 for_i elem[i] += b; 00039 return *this; 00040 } 00041 /** Subtract scalar @a b from this RGBA */ 00042 RGBA& operator-=(T b) { 00043 for_i elem[i] -= b; 00044 return *this; 00045 } 00046 /** Scale this RGBA up by scalar @a b */ 00047 RGBA& operator*=(T b) { 00048 for_i elem[i] *= b; 00049 return *this; 00050 } 00051 /** Scale this RGBA down by scalar @a b */ 00052 RGBA& operator/=(T b) { 00053 for_i elem[i] /= b; 00054 return *this; 00055 } 00056 /** Add RGBA @a b to this RGBA */ 00057 RGBA& operator+=(const RGBA& b) { 00058 for_i elem[i] += b[i]; 00059 return *this; 00060 } 00061 /** Subtract RGBA @a b from this RGBA */ 00062 RGBA& operator-=(const RGBA& b) { 00063 for_i elem[i] -= b[i]; 00064 return *this; 00065 } 00066 /** Scale this RGBA up by factors in @a b */ 00067 RGBA& operator*=(const RGBA& b) { 00068 for_i elem[i] *= b[i]; 00069 return *this; 00070 } 00071 /** Scale this RGBA down by factors in @a b */ 00072 RGBA& operator/=(const RGBA& b) { 00073 for_i elem[i] /= b[i]; 00074 return *this; 00075 } 00076 00077 // ACCESSORS 00078 00079 T& operator[](std::size_t i) { return elem[i]; } 00080 const T& operator[](std::size_t i) const { return elem[i]; } 00081 00082 T* data() { return elem; } 00083 const T* data() const { return elem; } 00084 00085 T& front() { return elem[0]; } 00086 const T& front() const { return elem[0]; } 00087 T& back() { return elem[3]; } 00088 const T& back() const { return elem[3]; } 00089 00090 std::size_t size() { return 4; } 00091 std::size_t max_size() { return 4; } 00092 bool empty() { return false; } 00093 00094 // ITERATORS 00095 00096 T* begin() { return elem; } 00097 const T* begin() const { return elem; } 00098 const T* cbegin() const { return elem; } 00099 00100 T* end() { return elem+4; } 00101 const T* end() const { return elem+4; } 00102 const T* cend() const { return elem+4; } 00103 }; 00104 00105 // STREAM OPERATORS 00106 00107 /** Write a RGBA to an output stream */ 00108 template <typename T> 00109 std::ostream& operator<<(std::ostream& s, const RGBA<T>& a) { 00110 return (s << a.r << ' ' << a.g << ' ' << a.b ' ' << a.a); 00111 } 00112 /** Read a Vec from an input stream */ 00113 template <typename T> 00114 std::istream& operator>>(std::istream& s, RGBA<T>& a) { 00115 return (s >> a.r >> a.g >> a.b >> a.a); 00116 } 00117 00118 // COMPARATORS 00119 template <typename T> 00120 bool operator==(const RGBA<T>& a, const RGBA<T>& b) { 00121 return std::equal(a.begin(), a.end(), b.begin()); 00122 } 00123 template <typename T> 00124 bool operator!=(const RGBA<T>& a, const RGBA<T>& b) { 00125 return !(a == b); 00126 } 00127 00128 // ARITHMETIC OPERATORS 00129 00130 /** Unary negation: Return -@a a */ 00131 template <typename T> 00132 RGBA<T> operator-(const RGBA<T>& a) { 00133 return RGBA<T>(-a.r, -a.g, -a.b, -a.a); 00134 } 00135 /** Unary plus: Return @a a. ("+a" should work if "-a" works.) */ 00136 template <typename T> 00137 RGBA<T> operator+(const RGBA<T>& a) { 00138 return a; 00139 } 00140 template <typename T> 00141 RGBA<T> operator+(RGBA<T> a, const RGBA<T>& b) { 00142 return a += b; 00143 } 00144 template <typename T> 00145 RGBA<T> operator+(RGBA<T> a, double b) { 00146 return a += b; 00147 } 00148 template <typename T> 00149 RGBA<T> operator+(double b, RGBA<T> a) { 00150 return a += b; 00151 } 00152 template <typename T> 00153 RGBA<T> operator-(RGBA<T> a, const RGBA<T>& b) { 00154 return a -= b; 00155 } 00156 template <typename T> 00157 RGBA<T> operator-(RGBA<T> a, double b) { 00158 return a -= b; 00159 } 00160 template <typename T> 00161 RGBA<T> operator-(double b, const RGBA<T>& a) { 00162 return (-a) += b; 00163 } 00164 template <typename T> 00165 RGBA<T> operator*(RGBA<T> a, const RGBA<T>& b) { 00166 return a *= b; 00167 } 00168 template <typename T> 00169 RGBA<T> operator*(RGBA<T> a, double b) { 00170 return a *= b; 00171 } 00172 template <typename T> 00173 RGBA<T> operator*(double b, RGBA<T> a) { 00174 return a *= b; 00175 } 00176 template <typename T> 00177 RGBA<T> operator/(RGBA<T> a, const RGBA<T>& b) { 00178 return a /= b; 00179 } 00180 template <typename T> 00181 RGBA<T> operator/(RGBA<T> a, double b) { 00182 return a /= b; 00183 } 00184 00185 #undef for_i 00186 00187 #endif
Generated on Tue Jul 12 2022 21:35:43 by
