00001 #ifndef _RGB_H_
00002 #define _RGB_H_
00003
00004 #include "cglib/definitions.h"
00005
00007 struct Rgb
00008 {
00009 public:
00010 float r;
00011 float g;
00012 float b;
00013
00015 inline Rgb()
00016 {
00017 r = g = b = 0.0f;
00018 }
00019
00021 inline Rgb(float c)
00022 {
00023 r = g = b = c;
00024 }
00025
00027 inline Rgb(float _r, float _g, float _b)
00028 {
00029 r = _r;
00030 g = _g;
00031 b = _b;
00032 }
00033
00035 inline Rgb operator+(const Rgb &other) const {
00036 return Rgb(r + other.r, g + other.g, b + other.b);
00037 }
00038
00040 inline Rgb operator-(const Rgb &other) const {
00041 return Rgb(r - other.r, g - other.g, b - other.b);
00042 }
00043
00045 inline Rgb operator+(float c) const {
00046 return Rgb(r + c, g + c, b + c);
00047 }
00048
00050 inline Rgb &operator+=(const Rgb &other)
00051 {
00052 r += other.r;
00053 g += other.g;
00054 b += other.b;
00055 return *this;
00056 }
00057
00059 inline Rgb operator*(const Rgb &other) const {
00060 return Rgb(r * other.r, g * other.g, b * other.b);
00061 }
00062
00064 inline Rgb operator*(float c) const {
00065 return Rgb(r * c, g * c, b * c);
00066 }
00067
00069 inline Rgb &operator*=(const Rgb &other)
00070 {
00071 r *= other.r;
00072 g *= other.g;
00073 b *= other.b;
00074 return *this;
00075 }
00076
00078 inline Rgb operator/(const Rgb &other) const {
00079 return Rgb(r / other.r, g / other.g, b / other.b);
00080 }
00081
00083 inline Rgb operator/(float c) const {
00084 float cinv = 1.0f / c;
00085 return Rgb(r * cinv, g * cinv, b * cinv);
00086 }
00087
00089
00092 bool is_near(const Rgb &other, float threshold=EPSILON)
00093 {
00094 float absr = fabs(r - other.r);
00095 float absg = fabs(g - other.g);
00096 float absb = fabs(b - other.b);
00097 return (absr < EPSILON && absg < EPSILON && absb < EPSILON);
00098 }
00099 };
00100
00102 inline Rgb operator+(float c, const Rgb &rgb)
00103 {
00104 return Rgb(rgb.r + c, rgb.g + c, rgb.b + c);
00105 }
00106
00108 inline Rgb operator*(float c, const Rgb &rgb)
00109 {
00110 return Rgb(rgb.r * c, rgb.g * c, rgb.b * c);
00111 }
00112
00113 #endif