00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "vpcolor.h"
00022 #include <cassert>
00023 #include <cstdlib>
00024
00025
00026 #include <iostream>
00027 using namespace std;
00028
00029 typedef unsigned char uchar;
00030
00031 VPColor::VPColor() {
00032 rgba[0] = 0;
00033 rgba[1] = 0;
00034 rgba[2] = 0;
00035 rgba[3] = 255;
00036 }
00037
00038 VPColor::VPColor(uchar r, uchar g, uchar b, uchar a) {
00039
00040 rgba[0] = r;
00041 rgba[1] = g;
00042 rgba[2] = b;
00043 rgba[3] = a;
00044 }
00045
00046 VPColor::VPColor(const VPColor& color) {
00047 rgba[0] = color.rgba[0];
00048 rgba[1] = color.rgba[1];
00049 rgba[2] = color.rgba[2];
00050 rgba[3] = color.rgba[3];
00051 }
00052
00053 const VPColor& VPColor::BLACK() {
00054 static const VPColor black(0,0,0,255);
00055 return black;
00056 }
00057
00058 const VPColor& VPColor::RED() {
00059 static const VPColor red(255,0,0,255);
00060 return red;
00061 }
00062
00063 const VPColor& VPColor::GREEN() {
00064 static const VPColor green(0,255,0,255);
00065 return green;
00066 }
00067
00068 const VPColor& VPColor::BLUE() {
00069 static const VPColor blue(0,0,255,255);
00070 return blue;
00071 }
00072
00073 const VPColor& VPColor::WHITE() {
00074 static const VPColor white(255,255,255,255);
00075 return white;
00076 }
00077
00078 VPColor VPColor::RANDOM() {
00079 VPColor color;
00080 unsigned char byte;
00081 byte = static_cast<unsigned char>(rand() % 256);
00082 color.SetR(byte);
00083 byte = static_cast<unsigned char>(rand() % 256);
00084 color.SetG(byte);
00085 byte = static_cast<unsigned char>(rand() % 256);
00086 color.SetB(byte);
00087 color.SetA(255);
00088 return color;
00089 }
00090
00091 void VPColor::SetRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
00092 rgba[0] = r;
00093 rgba[1] = g;
00094 rgba[2] = b;
00095 rgba[3] = a;
00096 }
00097
00098 void VPColor::GetRGBA(unsigned char* r, unsigned char* g,
00099 unsigned char* b, unsigned char* a) const {
00100 *r = rgba[0];
00101 *g = rgba[1];
00102 *b = rgba[2];
00103 *a = rgba[3];
00104 }
00105
00106 void VPColor::Get(float rgbaVec[4]) const {
00107 rgbaVec[0] = rgba[0]/255.0f;
00108 rgbaVec[1] = rgba[1]/255.0f;
00109 rgbaVec[2] = rgba[2]/255.0f;
00110 rgbaVec[3] = rgba[3]/255.0f;
00111 }
00112 VPColor& VPColor::operator=(const VPColor& color) {
00113 rgba[0] = color.rgba[0];
00114 rgba[1] = color.rgba[1];
00115 rgba[2] = color.rgba[2];
00116 rgba[3] = color.rgba[3];
00117 return *this;
00118 }
00119
00120 VPColor VPColor::operator+(const VPColor& color) const {
00121 VPColor c(rgba[0]+color.rgba[0], rgba[1]+color.rgba[1],
00122 rgba[2]+color.rgba[2], rgba[3]+color.rgba[3]);
00123 return c;
00124 }
00125
00126 VPColor VPColor::operator-(const VPColor& color) const {
00127 VPColor c(rgba[0]-color.rgba[0], rgba[1]-color.rgba[1],
00128 rgba[2]-color.rgba[2], rgba[3]-color.rgba[3]);
00129 return c;
00130 }
00131
00132 bool VPColor::operator==(const VPColor& color) const {
00133 return ( rgba[0] == color.rgba[0] &&
00134 rgba[1] == color.rgba[1] &&
00135 rgba[2] == color.rgba[2] &&
00136 rgba[3] == color.rgba[3] );
00137 }
00138
00139 bool VPColor::operator!=(const VPColor& color) const {
00140 return ( rgba[0] != color.rgba[0] ||
00141 rgba[1] != color.rgba[1] ||
00142 rgba[2] != color.rgba[2] ||
00143 rgba[3] != color.rgba[3] );
00144 }
00145
00146 void VPColor::GetScaled(float escalar, float result[4]) const {
00147 assert(escalar >= 0);
00148 assert(escalar <= 1);
00149 result[0] = rgba[0]/255.0f * escalar;
00150 result[1] = rgba[1]/255.0f * escalar;
00151 result[2] = rgba[2]/255.0f * escalar;
00152 result[3] = rgba[3]/255.0f;
00153 }
00154
00155 void VPColor::GetScaled(float escalar, VPColor* result) const {
00156 assert(escalar >= 0);
00157 assert(escalar <= 1);
00158 result->rgba[0] = static_cast<unsigned char>(rgba[0] * escalar);
00159 result->rgba[1] = static_cast<unsigned char>(rgba[1] * escalar);
00160 result->rgba[2] = static_cast<unsigned char>(rgba[2] * escalar);
00161 result->rgba[3] = rgba[3];
00162 }
00163
00164
00165 ostream& operator<<(ostream& output, const VPColor& c)
00166 {
00167 output << "{" << static_cast<int>(c.rgba[0]) << " "
00168 << static_cast<int>(c.rgba[1]) << " " << static_cast<int>(c.rgba[2])
00169 << " " << static_cast<int>(c.rgba[3]) << "}";
00170 return output;
00171 }