www.pudn.com > mputil.rar > color.cpp


/* 
 * color.cpp 
 * 
 * Copyright (c) 2003 Machine Perception Laboratory 
 * University of California San Diego. 
 * Please read the disclaimer and notes about redistribution 
 * at the end of this file. 
 * 
 * Authors: Josh Susskind 
 */ 
 
#include "color.h" 
#include "../common.h" 
 
// ================================================================ 
 
bool MPColorTools::RGBtoHSV (const unsigned char *pix, float &h, float &s, float &v) 
{	 
	const RGBTRIPLE *rpix = (RGBTRIPLE *)pix; 
	const int ri = rpix->rgbtRed; 
	const int gi = rpix->rgbtGreen; 
	const int bi = rpix->rgbtBlue; 
 
	const float r = (float)ri/255.f; 
	const float g = (float)gi/255.f; 
	const float b = (float)bi/255.f; 
 
	float max, min; 
	if (r >= g) { 
		max = r;  
		min = g; 
	} else {  
		max = g;  
		min = r;  
	} 
	if (b > max)  
		max = b;  
	else if (b < min)  
		min = b; 
	v = max; 
	 
	float delta = max-min; 
 
	if (v == 0 || delta == 0) { 
		h = -1; 
		return (false); 
	} else if (r == v) { 
		h = 60.f*(g-b)/delta; 
	} else if (g == v) { 
		h = 120.f + 60.f*(b-r)/delta; 
	} else if (b == v) { 
	  h = 240.f + 60.f*(r-g)/delta; 
	} 
 
	h /= 2.0; 
	s *= 255.0; 
	v *= 255.0; 
 
	return(true); 
} 
 
// ================================================================ 
 
void MPColorTools::hilitePixel(void *pix, const int &type) 
{ 
	static const unsigned char color = static_cast(255); 
	RGBTRIPLE *rpix = (RGBTRIPLE *)pix; 
	unsigned char &r = rpix->rgbtRed; 
	unsigned char &g = rpix->rgbtGreen; 
	unsigned char &b = rpix->rgbtBlue; 
	r = g = b = 0; 
	switch (type) { 
		case RGBRED: 
			r = color; 
			break; 
		case RGBGREEN: 
			g = color; 
			break; 
		case RGBBLUE: 
			b = color; 
			break; 
	} 
} 
 
// ================================================================ 
 
/* 
 *  
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 
 *  
 *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
 *    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. 
 *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. 
 *  
 * 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. 
 *  
 */