Exemplo n.º 1
0
/*
 * Search a palette to find the nearest color requested.
 * Uses a weighted squares comparison.
 */
MWPIXELVAL
GdFindNearestColor(MWPALENTRY *pal, int size, MWCOLORVAL cr)
{
	MWPALENTRY *	rgb;
	int		r, g, b;
	int		R, G, B;
	long		diff = 0x7fffffffL;
	long		sq;
	int		best = 0;

	r = REDVALUE(cr);
	g = GREENVALUE(cr);
	b = BLUEVALUE(cr);
	for(rgb=pal; diff && rgb < &pal[size]; ++rgb) {
		R = rgb->r - r;
		G = rgb->g - g;
		B = rgb->b - b;
#if 1
		/* speedy linear distance method*/
		sq = abs(R) + abs(G) + abs(B);
#else
		/* slower distance-cubed with luminance adjustment*/
		/* gray is .30R + .59G + .11B*/
		/* = (R*77 + G*151 + B*28)/256*/
		sq = (long)R*R*30*30 + (long)G*G*59*59 + (long)B*B*11*11;
#endif

		if(sq < diff) {
			best = rgb - pal;
			if((diff = sq) == 0)
				return best;
		}
	}
	return best;
}
Exemplo n.º 2
0
UINT32 cgfx_FindNearestColor(CRastPort *rp, int size, UINT32 cr)
{
	int		best = 0;
//	APTR SysBase = g_SysBase;
//	DPrintF("FindNcolor: PixType: %x \n", PF_TRUECOLOR8888);
#if 0
 	MWPALENTRY *pal
	MWPALENTRY *	rgb;
	int		r, g, b;
	int		R, G, B;
	int32_t		diff = 0x7fffffffL;
	int32_t		sq;

	r = REDVALUE(cr);
	g = GREENVALUE(cr);
	b = BLUEVALUE(cr);
	for(rgb=pal; diff && rgb < &pal[size]; ++rgb) 
	{
		R = rgb->r - r;
		G = rgb->g - g;
		B = rgb->b - b;
		/* speedy linear distance method*/
		sq = abs(R) + abs(G) + abs(B);

		if(sq < diff) 
		{
			best = rgb - pal;
			if((diff = sq) == 0) return best;
		}
	}
#endif
	return best;
}