// // Programmer: Craig Stuart Sapp // Creation Date: Thu Apr 11 12:33:09 PDT 2002 // Last Modified: Thu Apr 11 12:33:12 PDT 2002 // Filename: ...sig/maint/code/base/PixelColor/PixelColor.cpp // Web Address: http://sig.sapp.org/include/sigBase/PixelColor.cpp // Documentation: http://sig.sapp.org/doc/classes/PixelColor // Syntax: C++ // #include "PixelColor.h" #include #include #include #include #ifndef OLDCPP #include using namespace std; #else #include #endif ////////////////////////////// // // PixelColor::PixelColor -- // PixelColor::PixelColor(void) { // do nothing } PixelColor::PixelColor(const PixelColor& color) { Red = color.Red; Green = color.Green; Blue = color.Blue; } PixelColor::PixelColor(const char* color) { setColor(color); } PixelColor::PixelColor(int red, int green, int blue) { Red = (uint)limit(red, 0, 255); Green = (uint)limit(green, 0, 255); Blue = (uint)limit(blue, 0, 255); } PixelColor::PixelColor(float red, float green, float blue) { Red = (uint)floatToChar(red); Green = (uint)floatToChar(green); Blue = (uint)floatToChar(blue); } PixelColor::PixelColor(double red, double green, double blue) { Red = (uint)limit(floatToChar((float)red), 0, 255); Green = (uint)limit(floatToChar((float)green), 0, 255); Blue = (uint)limit(floatToChar((float)blue), 0, 255); } ////////////////////////////// // // PixelColor::~PixelColor -- // PixelColor::~PixelColor() { // do nothing } ////////////////////////////// // // PixelColor::invert -- negate the color. // void PixelColor::invert(void) { Red = ~Red; Green = ~Green; Blue = ~Blue; } ////////////////////////////// // // PixelColor::setColor -- set the contents to the specified value. // PixelColor& PixelColor::setColor(const char* colorstring) { PixelColor color; color = getColor(colorstring); Red = color.Red; Green = color.Green; Blue = color.Blue; return *this; } ////////////////////////////// // // PixelColor::getRed -- // int PixelColor::getRed(void) { return Red; } ////////////////////////////// // // PixelColor::getGreen -- // int PixelColor::getGreen(void) { return Green; } ////////////////////////////// // // PixelColor::getBlue -- // int PixelColor::getBlue(void) { return Blue; } ////////////////////////////// // // PixelColor::setRed -- // void PixelColor::setRed(int value) { Red = (uchar)limit(value, 0, 255); } ////////////////////////////// // // PixelColor::setGreen -- // void PixelColor::setGreen(int value) { Green = (uchar)limit(value, 0, 255); } ////////////////////////////// // // PixelColor::setBlue -- // void PixelColor::setBlue(int value) { Blue = (uchar)limit(value, 0, 255); } ////////////////////////////// // // PixelColor::getRedF -- // float PixelColor::getRedF(void) { return charToFloat(Red); } ////////////////////////////// // // PixelColor::getGreenF -- // float PixelColor::getGreenF(void) { return charToFloat(Green); } ////////////////////////////// // // PixelColor::getBlueF -- // float PixelColor::getBlueF(void) { return charToFloat(Blue); } ////////////////////////////// // // PixelColor::setRedF -- // void PixelColor::setRedF(float value) { Red = (uint)floatToChar(value); } ////////////////////////////// // // PixelColor::setGreenF -- // void PixelColor::setGreenF(float value) { Green = (uint)floatToChar(value); } ////////////////////////////// // // PixelColor::setBlueF -- // void PixelColor::setBlueF(float value) { Blue = (uint)floatToChar(value); } ////////////////////////////// // // PixelColor::setColor -- // void PixelColor::setColor(PixelColor color) { Red = color.Red; Green = color.Green; Blue = color.Blue; } ////////////////////////////// // // PixelColor::setColor -- // PixelColor& PixelColor::setColor(int red, int green, int blue) { Red = (uint)limit(red, 0, 255); Green = (uint)limit(green, 0, 255); Blue = (uint)limit(blue, 0, 255); return *this; } ////////////////////////////// // // PixelColor::makeGrey -- // PixelColor& PixelColor::makeGrey(void) { uchar average = limit((int)(((int)Red+(int)Green+(int)Blue)/3.0+0.5),0,255); Red = Green = Blue = average; return *this; } ////////////////////////////// // // PixelColor::setGrayNormalized -- input in the range from 0.0 to 1.0. // PixelColor& PixelColor::setGrayNormalized(double value) { int graylevel = int(value * 256.0); if (graylevel >= 256) { graylevel = 255; } if (graylevel < 0) { graylevel = 0; } Red = Green = Blue = graylevel; return *this; } PixelColor& PixelColor::setGreyNormalized(double value) { return setGrayNormalized(value); } ////////////////////////////// // // PixelColor::makeGray -- // PixelColor& PixelColor::makeGray(void) { return makeGrey(); } ////////////////////////////// // // PixelColor::operator> -- // int PixelColor::operator>(int number) { if (Red <= number) return 0; if (Green <= number) return 0; if (Blue <= number) return 0; return 1; } ////////////////////////////// // // PixelColor::operator< -- // int PixelColor::operator<(int number) { if (Red >= number) return 0; if (Green >= number) return 0; if (Blue >= number) return 0; return 1; } ////////////////////////////// // // PixelColor::operator== -- // int PixelColor::operator==(PixelColor color) { if (Red != color.Red) { return 0; } if (Green != color.Green) { return 0; } if (Blue != color.Blue) { return 0; } return 1; } ////////////////////////////// // // PixelColor::operator!= -- // int PixelColor::operator!=(PixelColor color) { if ((Red == color.Red) && (Green == color.Green) && (Blue == color.Blue)) { return 0; } else { return 1; } } ////////////////////////////// // // PixelColor::operator*= -- // PixelColor& PixelColor::operator*=(double number) { Red = (uchar)limit(floatToChar(charToFloat(Red)*number), 0, 255); Green = (uchar)limit(floatToChar(charToFloat(Green)*number), 0, 255); Blue = (uchar)limit(floatToChar(charToFloat(Blue)*number), 0, 255); return *this; } ////////////////////////////// // // PixelColor::operator= -- // PixelColor& PixelColor::operator=(PixelColor color) { if (this == &color) { return *this; } Red = color.Red; Green = color.Green; Blue = color.Blue; return *this; } PixelColor& PixelColor::operator=(int value) { Red = (uchar)limit(value, 0, 255); Green = Red; Blue = Red; return *this; } ////////////////////////////// // // PixelColor::operator+ -- // PixelColor PixelColor::operator+(PixelColor color) { PixelColor output; output.Red = (uchar)limit((int)Red + color.Red, 0, 255); output.Green = (uchar)limit((int)Green + color.Green, 0, 255); output.Blue = (uchar)limit((int)Blue + color.Blue, 0, 255); return output; } ////////////////////////////// // // PixelColor::operator+= -- // PixelColor& PixelColor::operator+=(int number) { setRed(getRed() + number); setGreen(getGreen() + number); setBlue(getBlue() + number); return *this; } ////////////////////////////// // // PixelColor::operator- -- // PixelColor PixelColor::operator-(PixelColor color) { PixelColor output; output.Red = (uchar)limit((int)Red - color.Red, 0, 255); output.Green = (uchar)limit((int)Green - color.Green, 0, 255); output.Blue = (uchar)limit((int)Blue - color.Blue, 0, 255); return output; } ////////////////////////////// // // PixelColor::operator* -- // PixelColor PixelColor::operator*(PixelColor color) { PixelColor output; output.Red = (uchar)limit(floatToChar(charToFloat(Red)*charToFloat(color.Red)), 0, 255); output.Green = (uchar)limit(floatToChar(charToFloat(Green)*charToFloat(color.Green)), 0, 255); output.Blue = (uchar)limit(floatToChar(charToFloat(Blue)*charToFloat(color.Blue)), 0, 255); return output; } PixelColor PixelColor::operator*(double number) { PixelColor output; output.Red = (uchar)limit(floatToChar(charToFloat(Red)*number), 0, 255); output.Green = (uchar)limit(floatToChar(charToFloat(Green)*number), 0, 255); output.Blue = (uchar)limit(floatToChar(charToFloat(Blue)*number), 0, 255); return output; } PixelColor PixelColor::operator*(int number) { PixelColor output; output.Red = (uchar)limit(floatToChar(charToFloat(Red)*number), 0, 255); output.Green = (uchar)limit(floatToChar(charToFloat(Green)*number), 0, 255); output.Blue = (uchar)limit(floatToChar(charToFloat(Blue)*number), 0, 255); return output; } ////////////////////////////// // // PixelColor::operator/ -- // PixelColor PixelColor::operator/(double number) { PixelColor output; output.Red = (uchar)limit(floatToChar(charToFloat(Red)/number), 0, 255); output.Green = (uchar)limit(floatToChar(charToFloat(Green)/number), 0, 255); output.Blue = (uchar)limit(floatToChar(charToFloat(Blue)/number), 0, 255); return output; } PixelColor PixelColor::operator/(int number) { PixelColor output; output.Red = (uchar)limit(floatToChar(charToFloat(Red)/(double)number), 0, 255); output.Green = (uchar)limit(floatToChar(charToFloat(Green)/(double)number), 0, 255); output.Blue = (uchar)limit(floatToChar(charToFloat(Blue)/(double)number), 0, 255); return output; } ////////////////////////////// // // PixelColor::getColor -- // PixelColor PixelColor::getColor(const char* colorstring) { PixelColor output; int i = 0; int start = 0; int hasdigit = 0; int length = strlen(colorstring); if (length > 128) { cout << "ERROR: color string too long: " << colorstring << endl; exit(1); } if (length == 7) { if (colorstring[0] == '#') { hasdigit = 1; start = 1; } } else if (length == 6) { int allxdigit = 1; start = 0; for (i=start; i 0 && tgreen > 0 && tblue > 0) { output.setColor(tred, tgreen, tblue); return output; } } if (hasdigit) { char rv[3] = {0}; char gv[3] = {0}; char bv[3] = {0}; strncpy(rv, &colorstring[start], 2); strncpy(gv, &colorstring[start+2], 2); strncpy(bv, &colorstring[start+4], 2); int rval = strtol(rv, NULL, 16); int gval = strtol(gv, NULL, 16); int bval = strtol(bv, NULL, 16); output.setColor(rval, gval, bval); return output; } // color string char buffer[128] = {0}; strncpy(buffer, colorstring, 100); length = strlen(buffer); for (i=0; i max) { value = max; } return value; } /////////////////////////////////////////////////////////////////////////// // // other functions // ////////////////////////////// // // operator<< -- // // for use with P3 ASCII pnm images: print red green blue triplet. // ostream& operator<<(ostream& out, PixelColor apixel) { out << apixel.getRed() << ' '; out << apixel.getGreen() << ' '; out << apixel.getBlue(); return out; } // md5sum: 31a31a4351590f36f06479346c040487 PixelColor.cpp [20050403]