The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

color.cpp

Go to the documentation of this file.
00001 /*
00002  * color.cpp
00003  *
00004  * Copyright (c) 2003 Machine Perception Laboratory
00005  * University of California San Diego.
00006  * Please read the disclaimer and notes about redistribution
00007  * at the end of this file.
00008  *
00009  * Authors: Josh Susskind
00010  */
00011 
00012 #include "color.h"
00013 #include "../common.h"
00014 
00015 // ================================================================
00016 
00017 bool MPColorTools::RGBtoHSV (const unsigned char *pix, float &h, float &s, float &v)
00018 {       
00019         const RGBTRIPLE *rpix = (RGBTRIPLE *)pix;
00020         const int ri = rpix->rgbtRed;
00021         const int gi = rpix->rgbtGreen;
00022         const int bi = rpix->rgbtBlue;
00023 
00024         const float r = (float)ri/255.f;
00025         const float g = (float)gi/255.f;
00026         const float b = (float)bi/255.f;
00027 
00028         float max, min;
00029         if (r >= g) {
00030                 max = r; 
00031                 min = g;
00032         } else { 
00033                 max = g; 
00034                 min = r; 
00035         }
00036         if (b > max) 
00037                 max = b; 
00038         else if (b < min) 
00039                 min = b;
00040         v = max;
00041         
00042         float delta = max-min;
00043 
00044         if (v == 0 || delta == 0) {
00045                 h = -1;
00046                 return (false);
00047         } else if (r == v) {
00048                 h = 60.f*(g-b)/delta;
00049         } else if (g == v) {
00050                 h = 120.f + 60.f*(b-r)/delta;
00051         } else if (b == v) {
00052           h = 240.f + 60.f*(r-g)/delta;
00053         }
00054 
00055         h /= 2.0;
00056         s *= 255.0;
00057         v *= 255.0;
00058 
00059         return(true);
00060 }
00061 
00062 // ================================================================
00063 
00064 void MPColorTools::hilitePixel(void *pix, const int &type)
00065 {
00066         static const unsigned char color = static_cast<unsigned char>(255);
00067         RGBTRIPLE *rpix = (RGBTRIPLE *)pix;
00068         unsigned char &r = rpix->rgbtRed;
00069         unsigned char &g = rpix->rgbtGreen;
00070         unsigned char &b = rpix->rgbtBlue;
00071         r = g = b = 0;
00072         switch (type) {
00073                 case RGBRED:
00074                         r = color;
00075                         break;
00076                 case RGBGREEN:
00077                         g = color;
00078                         break;
00079                 case RGBBLUE:
00080                         b = color;
00081                         break;
00082         }
00083 }
00084 
00085 // ================================================================
00086 
00087 /*
00088  * 
00089  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00090  * 
00091  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00092  *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
00093  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00094  * 
00095  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00096  * 
00097  */

Generated on Mon Nov 8 17:07:31 2004 for MPT by  doxygen 1.3.9.1