//-----------------------------------------------------------------------------------------------
// Rgba.cpp
//
#include "GypsyPrecompiledHeader.hpp"

//-----------------------------------------------------------------------------------------------
#include "Engine/Core/Rgba.hpp"
#include "Engine/Core/ErrorWarningAssert.hpp"
#include <stdlib.h>


//-----------------------------------------------------------------------------------------------
STATIC const int Rgba::MAX_RGBA_COMMAS = 3;
STATIC const Rgba Rgba::BLACK		( 0, 0, 0 );
STATIC const Rgba Rgba::WHITE		( 255, 255, 255 );
STATIC const Rgba Rgba::RED			( 255, 0, 0 );
STATIC const Rgba Rgba::GREEN		( 0, 255, 0 );
STATIC const Rgba Rgba::BLUE		( 0, 0, 255 );
STATIC const Rgba Rgba::YELLOW		( 255, 255, 0 );
STATIC const Rgba Rgba::MAGENTA		( 255, 0, 255 );
STATIC const Rgba Rgba::CYAN		( 0, 255, 255 );
STATIC const Rgba Rgba::SILVER		( 192, 192, 192 );
STATIC const Rgba Rgba::GREY		( 127, 127, 127 );
STATIC const Rgba Rgba::DARKGREY	( 64, 64, 64 );
STATIC const Rgba Rgba::DEEPGREY	( 32, 32, 32 );
STATIC const Rgba Rgba::DARKRED		( 128, 0, 0 );
STATIC const Rgba Rgba::DARKGREEN	( 0, 128, 0 );
STATIC const Rgba Rgba::DARKBLUE	( 0, 0, 128 );
STATIC const Rgba Rgba::PURPLE		( 128, 0, 128 );
STATIC const Rgba Rgba::DARKYELLOW	( 128, 128, 0 );
STATIC const Rgba Rgba::DARKCYAN	( 0, 128, 128 );
STATIC const Rgba Rgba::ORANGE		( 255, 128, 0 );

STATIC const Rgba Rgba::ORGANIC_RED		( 255, 44, 44 );
STATIC const Rgba Rgba::ORGANIC_ORANGE	( 255, 112, 11 );
STATIC const Rgba Rgba::ORGANIC_YELLOW	( 255, 214, 0 );
STATIC const Rgba Rgba::ORGANIC_GREEN	( 76, 254, 0 );
STATIC const Rgba Rgba::ORGANIC_BLUE	( 22, 157, 255 );
STATIC const Rgba Rgba::ORGANIC_PURPLE	( 190, 40, 255 );
STATIC const Rgba Rgba::ORGANIC_GREY	( 102, 102, 102 );
STATIC const Rgba Rgba::ORGANIC_BROWN	( 133, 84, 57 );

STATIC const Rgba Rgba::ORGANIC_DIM_RED		( 114, 0, 0 );
STATIC const Rgba Rgba::ORGANIC_DIM_YELLOW	( 62, 52, 0 );
STATIC const Rgba Rgba::ORGANIC_DIM_GREEN	( 21, 70, 0 );
STATIC const Rgba Rgba::ORGANIC_DIM_BLUE	( 0, 53, 91 );
STATIC const Rgba Rgba::ORGANIC_DIM_PURPLE	( 76, 0, 109 );

STATIC const Rgba Rgba::GAINSBORO	( 220, 220, 220 );
STATIC const Rgba Rgba::JET			( 52, 52, 52 );
STATIC const Rgba Rgba::CHARCOAL	( 54, 69, 79 );
STATIC const Rgba Rgba::PINK		( 255, 192, 203 );
STATIC const Rgba Rgba::ASH			( 178, 190, 181 );


//-----------------------------------------------------------------------------------------------
// Takes a presumed Rgba-as-text (or Rgb-as-text) string and splices it "in place", with
//  '\0' null terminators replacing the commas in the now-populated out_splicedTextBufferToFill.
//
// substringStartsInSplicedBuffer[] notes the starts of each of the (typcically 3 or 4) comma-delimited substrings in the output buffer.
//
void Rgba::SpliceIntoSubstrings( const char* originalText, char* out_splicedTextBufferToFill, char** substringStartsInSplicedBuffer )
{
	const char* readScan = originalText;
	char* writeScan = out_splicedTextBufferToFill;
	int commaCount = 0;

	for( ;; )
	{
		switch( *readScan )
		{
			case '\0':
				*writeScan = '\0';
				return;

			case ',':
				*writeScan = '\0';
				if( commaCount >= MAX_RGBA_COMMAS )
					return;

				substringStartsInSplicedBuffer[ commaCount ] = writeScan + 1;
				++ commaCount;
				break;

			default:
				*writeScan = *readScan;
				break;
		}

		++ writeScan;
		++ readScan;
	}
}


//-----------------------------------------------------------------------------------------------
Rgba::Rgba( const char* asText )
	: r( 0 )
	, g( 0 )
	, b( 0 )
	, a( 255 )
{
	SetFromText( asText );
}


//-----------------------------------------------------------------------------------------------
void Rgba::SetFromText( const char* asText )
{
	r = g = b = 0;
	a = 255;

	const int MAX_RGBA_TEXT_LENGTH = 256;
	char splicedText[ MAX_RGBA_TEXT_LENGTH ];
	char* bgaSubstringStarts[ MAX_RGBA_COMMAS ] = { nullptr, nullptr, nullptr };
	SpliceIntoSubstrings( asText, splicedText, bgaSubstringStarts );

	r = static_cast< unsigned char >( atoi( splicedText ) );

	if( bgaSubstringStarts[ 0 ] )
		g = static_cast< unsigned char >( atoi( bgaSubstringStarts[ 0 ] ) );

	if( bgaSubstringStarts[ 1 ] )
		b = static_cast< unsigned char >( atoi( bgaSubstringStarts[ 1 ] ) );

	if( bgaSubstringStarts[ 2 ] )
		a = static_cast< unsigned char >( atoi( bgaSubstringStarts[ 2 ] ) );
}


