////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 RWS Inc, All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as published by
// the Free Software Foundation
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#include <string.h>
#include "Blue.h"
#ifdef PATHS_IN_INCLUDES
	#include "GREEN/Image/Image.h"
	#include "ORANGE/color/colormatch.h"
	#include "GREEN/BLiT/BLIT.H" // ONLY for the debug command... can be disabled if nec
	#include "ORANGE/QuickMath/FixedPoint.h"
#else
	#include "Image.h"
	#include "colormatch.h"
	#include "BLIT.H" // ONLY for the debug command... can be disabled if nec
	#include "fixedpoint.h"
#endif

uint8_t rspMatchColorRGB(int32_t r,int32_t g,int32_t b,int16_t sStart,int16_t sNum,
					 uint8_t* pr,uint8_t* pg,uint8_t* pb,int32_t linc)
	{
	int32_t lMatch = 0,i;
	int32_t lMax =  int32_t(16777217),lDist;
	int32_t lOffset = linc * sStart; // array offset

	pr += lOffset;
	pg += lOffset;
	pb += lOffset;
	
	for (i=0;i<sNum;i++)
		{
		lDist = SQR(int32_t(*pr)-r)+SQR(int32_t(*pg)-g)+SQR(int32_t(*pb)-b);
		pr += linc;
		pg += linc;
		pb += linc;
		if (lDist < lMax)
			{
			lMax = lDist;
			lMatch = i;
			}
		}

	return (uint8_t)(lMatch + sStart);
	}