⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 api_colors.cpp

📁 这是一个GPS相关的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		break;

	case SG_COLORS_GREEN_BLUE:
		Set_Ramp(SG_GET_RGB(  0, 255,   0), SG_GET_RGB(  0,   0, 255));
		break;

	case SG_COLORS_RED_GREY_BLUE:
		Set_Count(5);
		Set_Color(0, SG_GET_RGB(127,   0,   0));
		Set_Color(1, SG_GET_RGB(255, 127,   0));
		Set_Color(2, SG_GET_RGB(200, 200, 200));
		Set_Color(3, SG_GET_RGB(  0, 127, 255));
		Set_Color(4, SG_GET_RGB(  0,   0, 127));
		break;

	case SG_COLORS_RED_GREY_GREEN:
		Set_Count(5);
		Set_Color(0, SG_GET_RGB(127,   0,   0));
		Set_Color(1, SG_GET_RGB(255, 127,   0));
		Set_Color(2, SG_GET_RGB(200, 200, 200));
		Set_Color(3, SG_GET_RGB(  0, 255, 127));
		Set_Color(4, SG_GET_RGB(  0, 127,   0));
		break;

	case SG_COLORS_GREEN_GREY_BLUE:
		Set_Count(5);
		Set_Color(0, SG_GET_RGB(  0, 127,   0));
		Set_Color(1, SG_GET_RGB(127, 255,   0));
		Set_Color(2, SG_GET_RGB(200, 200, 200));
		Set_Color(3, SG_GET_RGB(  0, 127, 255));
		Set_Color(4, SG_GET_RGB(  0,   0, 127));
		break;

	case SG_COLORS_RED_GREEN_BLUE:
		Set_Count(5);
		Set_Color(0, SG_GET_RGB(127,   0, 127));
		Set_Color(1, SG_GET_RGB(255,   0,   0));
		Set_Color(2, SG_GET_RGB(  0, 255,   0));
		Set_Color(3, SG_GET_RGB(  0,   0, 255));
		Set_Color(4, SG_GET_RGB(127,   0, 127));
		break;

	case SG_COLORS_RED_BLUE_GREEN:
		Set_Count(5);
		Set_Color(0, SG_GET_RGB(127, 127,   0));
		Set_Color(1, SG_GET_RGB(255,   0,   0));
		Set_Color(2, SG_GET_RGB(  0,   0, 255));
		Set_Color(3, SG_GET_RGB(  0, 255,   0));
		Set_Color(4, SG_GET_RGB(127, 127,   0));
		break;

	case SG_COLORS_GREEN_RED_BLUE:
		Set_Count(5);
		Set_Color(0, SG_GET_RGB(  0, 127, 127));
		Set_Color(1, SG_GET_RGB(  0, 255,   0));
		Set_Color(2, SG_GET_RGB(255,   0,   0));
		Set_Color(3, SG_GET_RGB(  0,   0, 255));
		Set_Color(4, SG_GET_RGB(  0, 127, 127));
		break;

	case SG_COLORS_RAINBOW:
		Set_Count(8);
		Set_Color(0, SG_GET_RGB(127,   0, 127));
		Set_Color(1, SG_GET_RGB(  0,   0, 255));
		Set_Color(2, SG_GET_RGB(  0, 255, 255));
		Set_Color(3, SG_GET_RGB(  0, 191,   0));
		Set_Color(4, SG_GET_RGB(255, 255,   0));
		Set_Color(5, SG_GET_RGB(255, 127,   0));
		Set_Color(6, SG_GET_RGB(255,   0,   0));
		Set_Color(7, SG_GET_RGB(127,   0,   0));
		break;

	case SG_COLORS_NEON:
		Set_Count(7);
		Set_Color(0, SG_GET_RGB(  0,   0,   0));
		Set_Color(1, SG_GET_RGB(255,   0,   0));
		Set_Color(2, SG_GET_RGB(  0,   0,   0));
		Set_Color(3, SG_GET_RGB(255, 255,   0));
		Set_Color(4, SG_GET_RGB(  0,   0,   0));
		Set_Color(5, SG_GET_RGB(  0, 255,   0));
		Set_Color(6, SG_GET_RGB(  0,   0,   0));
		break;
	}

	//-----------------------------------------------------
	Set_Count(nColors);

	if( bRevert )
	{
		Revert();
	}

	return( true );
}

//---------------------------------------------------------
bool CSG_Colors::Set_Ramp(long Color_A, long Color_B)
{
	return( Set_Ramp(Color_A, Color_B, 0, Get_Count() - 1) );
}

//---------------------------------------------------------
bool CSG_Colors::Set_Ramp(long Color_A, long Color_B, int iColor_A, int iColor_B)
{
	int		i, n, ar, ag, ab;
	double	dr, dg, db;

	//-----------------------------------------------------
	if( iColor_A > iColor_B )
	{
		i			= iColor_A;
		iColor_A	= iColor_B;
		iColor_B	= i;
	}

	if( iColor_A < 0 )
	{
		iColor_A	= 0;
	}

	if( iColor_B >= Get_Count() )
	{
		iColor_B	= Get_Count() - 1;
	}

	//-----------------------------------------------------
	if( (n = iColor_B - iColor_A) > 0 )
	{
		ar		= SG_GET_R(Color_A);
		dr		= (double)(SG_GET_R(Color_B) - ar) / (double)n;

		ag		= SG_GET_G(Color_A);
		dg		= (double)(SG_GET_G(Color_B) - ag) / (double)n;

		ab		= SG_GET_B(Color_A);
		db		= (double)(SG_GET_B(Color_B) - ab) / (double)n;

		for(i=0; i<=n; i++)
		{
			Set_Color(iColor_A + i,
				(int)(ar + i * dr),
				(int)(ag + i * dg),
				(int)(ab + i * db)
			);
		}

		return( true );
	}

	return( false );
}

//---------------------------------------------------------
bool CSG_Colors::Set_Ramp_Brighness(int Brightness_A, int Brightness_B)
{
	return( Set_Ramp_Brighness(Brightness_A, Brightness_B, 0, Get_Count() - 1) );
}

//---------------------------------------------------------
bool CSG_Colors::Set_Ramp_Brighness(int Brightness_A, int Brightness_B, int iColor_A, int iColor_B)
{
	int		i, n;
	double	dBrightness;

	//-----------------------------------------------------
	if( iColor_A > iColor_B )
	{
		i			= iColor_A;
		iColor_A	= iColor_B;
		iColor_B	= i;
	}

	if( iColor_A < 0 )
	{
		iColor_A	= 0;
	}

	if( iColor_B >= Get_Count() )
	{
		iColor_B	= Get_Count() - 1;
	}

	//-----------------------------------------------------
	if( (n = iColor_B - iColor_A) > 0 )
	{
		dBrightness	= (double)(Brightness_B - Brightness_A) / (double)n;

		for(i=0; i<=n; i++)
		{
			Set_Brightness(iColor_A + i, (int)(Brightness_A + i * dBrightness));
		}

		return( true );
	}

	return( false );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
bool CSG_Colors::Random(void)
{
	int		i;

	for(i=0; i<Get_Count(); i++)
	{
		Set_Color(i,
			(int)(255.0 * (double)rand() / (double)RAND_MAX),
			(int)(255.0 * (double)rand() / (double)RAND_MAX),
			(int)(255.0 * (double)rand() / (double)RAND_MAX)
		);
	}

	return( Get_Count() > 0 );
}

//---------------------------------------------------------
bool CSG_Colors::Invert(void)
{
	int		i;

	for(i=0; i<Get_Count(); i++)
	{
		Set_Color(i, 255 - Get_Red(i), 255 - Get_Green(i), 255 - Get_Blue(i));
	}

	return( Get_Count() > 0 );
}

//---------------------------------------------------------
bool CSG_Colors::Revert(void)
{
	int		i, j;
	long	c;

	for(i=0, j=Get_Count()-1; i<j; i++, j--)
	{
		c		=    Get_Color(j);
		Set_Color(j, Get_Color(i));
		Set_Color(i, c);
	}

	return( Get_Count() > 0 );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
CSG_Colors & CSG_Colors::operator = (const CSG_Colors &Colors)
{
	Assign(Colors);

	return( *this );
}

bool CSG_Colors::Assign(const CSG_Colors &Colors)
{
	if( Colors.m_nColors > 0 )
	{
		m_nColors	= Colors.m_nColors;
		m_Colors	= (long *)SG_Realloc(m_Colors, m_nColors * sizeof(long));

		memcpy(m_Colors, Colors.m_Colors, m_nColors * sizeof(long));

		return( true );
	}

	return( false );
}

bool CSG_Colors::Assign(CSG_Colors *pColors)
{
	return( pColors ? Assign(*pColors) : false );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
bool CSG_Colors::Load(const SG_Char *File_Name)
{
	CSG_String	Version;
	CSG_File	Stream;

	if( Stream.Open(File_Name, SG_FILE_R, true) )
	{
		Stream.Read(Version, sizeof(COLORS_SERIAL_VERSION_BINARY));

		if( !Version.Cmp(COLORS_SERIAL_VERSION_BINARY) )
		{
			return( Serialize(Stream, false, true) );
		}
		else if( !Version.Cmp(COLORS_SERIAL_VERSION__ASCII) )
		{
			return( Serialize(Stream, false, false) );
		}
		else	// SAGA 1.x compatibility...
		{
			short		nColors;

			Stream.Seek_Start();
			Stream.Read(&nColors, sizeof(short));

			if( Stream.Length() == (int)(sizeof(short) + 3 * nColors) )
			{
				BYTE	*R, *G, *B;

				R	= (BYTE *)SG_Malloc(nColors * sizeof(BYTE));
				G	= (BYTE *)SG_Malloc(nColors * sizeof(BYTE));
				B	= (BYTE *)SG_Malloc(nColors * sizeof(BYTE));

				Stream.Read(R, nColors * sizeof(BYTE));
				Stream.Read(G, nColors * sizeof(BYTE));
				Stream.Read(B, nColors * sizeof(BYTE));

				Set_Count(nColors);

				for(int i=0; i<nColors; i++)
				{
					Set_Color(i, R[i], G[i], B[i]);
				}

				SG_Free(R);
				SG_Free(G);
				SG_Free(B);

				return( true );
			}
		}
	}

	return( false );
}

//---------------------------------------------------------
bool CSG_Colors::Save(const SG_Char *File_Name, bool bBinary)
{
	CSG_File	Stream;

	if( Stream.Open(File_Name, SG_FILE_W, true) )
	{
		if( bBinary )
		{
			Stream.Write((void *)COLORS_SERIAL_VERSION_BINARY, sizeof(COLORS_SERIAL_VERSION_BINARY));
		}
		else
		{
			Stream.Printf(SG_T("%s\n"), COLORS_SERIAL_VERSION__ASCII);
		}

		Serialize(Stream, true, bBinary);

		return( true );
	}

	return( false );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
bool CSG_Colors::Serialize(CSG_File &Stream, bool bSave, bool bBinary)
{
	int		i, r, g, b;

	if( Stream.is_Open() )
	{
		if( bBinary )
		{
			if( bSave )
			{
				if( m_nColors > 0 )
				{
					Stream.Write(&m_nColors, sizeof(m_nColors));
					Stream.Write(m_Colors, sizeof(long), m_nColors);
				}
			}
			else
			{
				Stream.Read(&i, sizeof(m_nColors));

				if( i > 0 )
				{
					Set_Count(i);

					Stream.Read(m_Colors, sizeof(long), m_nColors);
				}
			}

			return( true );
		}
		else
		{
			if( bSave )
			{
				if( m_nColors > 0 )
				{
					Stream.Printf(SG_T("%d\n"), m_nColors);

					for(i=0; i<m_nColors; i++)
					{
						Stream.Printf(SG_T("%03d %03d %03d\n"), (int)Get_Red(i), (int)Get_Green(i), (int)Get_Blue(i));
					}
				}
			}
			else
			{
				SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%d"), &i);

				if( i > 0 )
				{
					Set_Count(i);

					for(i=0; i<m_nColors; i++)
					{
						SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%d %d %d"), &r, &g, &b);

						m_Colors[i]	= SG_GET_RGB(r, g, b);
					}
				}
			}

			return( true );
		}
	}

	return( false );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -