渐变填充算法模拟

实现了渐变和按照正弦曲线变化,效果如图:

上图为竖直放向颜色按照正弦变化,下图为线性渐变。

只要有好的函数,就可以实现出更好的填充算法。

代码如下,程序用GDI实现的,因为GDI中图像以像素为单位。

/*---------------------------------------
Gradient效果
11/18/2009 RYF
---------------------------------------*/

#include <windows.h>
#include <math.h>

//
//全局数据
const int g_iAreaWid = 400;		//以像素为单位
const int g_iAreaHgh = 100;
int g_iAreaBgPnt[] = {100, 100, 100, 400 };


//
typedef unsigned char (*pGetColorFuncPtr)(unsigned char, unsigned char, int, int);
void	LinearGradientFillingArea( unsigned char, unsigned char, unsigned char,
						  unsigned char, unsigned char, unsigned char,
						  HDC hdc,
						  int, int, int, int,
						  pGetColorFuncPtr pFunc);

// 线性关系渐变颜色
unsigned char GetLinearColor( unsigned char first, unsigned char last, 
							  int areaHgh, int index );
// 正弦关系渐变颜色
unsigned char GetSinColor( unsigned char first, unsigned char last,
						   int areaHgh, int index );


LRESULT CALLBACK	WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
					PSTR szCmdLine, int iCmdShow)
{
	static char szAppName[] = "Fill_Algorithm" ;
	HWND        hwnd ;
	MSG         msg ;
	WNDCLASSEX  wndclass ;

	wndclass.cbSize        = sizeof (wndclass) ;
	wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
	wndclass.lpfnWndProc   = WndProc ;
	wndclass.cbClsExtra    = 0 ;
	wndclass.cbWndExtra    = 0 ;
	wndclass.hInstance     = hInstance ;
	wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
	wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
	wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
	wndclass.lpszMenuName  = NULL ;
	wndclass.lpszClassName = szAppName ;
	wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;

	RegisterClassEx (&wndclass) ;

	hwnd = CreateWindow (szAppName, "Fill_Algorithm",
						WS_OVERLAPPEDWINDOW,
						CW_USEDEFAULT, CW_USEDEFAULT,
						CW_USEDEFAULT, CW_USEDEFAULT,
						NULL, NULL, hInstance, NULL) ;

	ShowWindow (hwnd, iCmdShow) ;
	UpdateWindow (hwnd) ;

	while (GetMessage (&msg, NULL, 0, 0))
	{
		TranslateMessage (&msg) ;
		DispatchMessage (&msg) ;
	}
	return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	HDC          hdc ;
	int          cxClient, cyClient ;
	PAINTSTRUCT  ps ;

	switch (iMsg)
	{
	case WM_SIZE:
		cxClient = LOWORD (lParam) ;
		cyClient = HIWORD (lParam) ;

		return 0 ;

	case WM_MOUSEMOVE:
		return 0 ;

	case WM_PAINT:
		InvalidateRect (hwnd, NULL, TRUE) ;

		hdc = BeginPaint (hwnd, &ps) ;
		{
			//以像素为单位绘制填充区域
			Rectangle( hdc, g_iAreaBgPnt[0], g_iAreaBgPnt[1],
							g_iAreaBgPnt[0] + g_iAreaWid,
							g_iAreaBgPnt[1] + g_iAreaHgh );
			//
			LinearGradientFillingArea( 239, 119, 119, 255, 255, 255,
							 hdc, g_iAreaBgPnt[0], g_iAreaBgPnt[1],
							 g_iAreaWid, g_iAreaHgh,
							 GetSinColor );

			LinearGradientFillingArea( 239, 119, 119, 0, 255, 255,
							 hdc, g_iAreaBgPnt[2], g_iAreaBgPnt[3],
							 g_iAreaWid, g_iAreaHgh,
							 GetLinearColor );

		}EndPaint (hwnd, &ps) ;
		return 0 ;

	case WM_DESTROY:
		PostQuitMessage (0) ;
		return 0 ;
	}

	return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}

void LinearGradientFillingArea( unsigned char firRed, unsigned char firGreen, unsigned char firBlue,
								unsigned char latRed, unsigned char latGreen, unsigned char latBlue,
								HDC hdc,
								int areaX, int areaY, int areaWid, int areaHgh,
								pGetColorFuncPtr pFunc )
{
	HBITMAP hbmp;
	HDC		hBfr;

	hbmp = CreateCompatibleBitmap( hdc, areaWid, areaHgh );
	hBfr = CreateCompatibleDC( hdc );
	SelectObject( hBfr,  hbmp );


	for (int i=0; i<areaWid; i++)
	{
		unsigned char temR, temB, temG;
		for (int j=0; j<areaHgh; j++)
		{
			//红色
			//--------------------------------------------------
			temR = pFunc(firRed, latRed, areaHgh, j);			


			//绿色
			//--------------------------------------------------
			temG = pFunc(firGreen, latGreen, areaHgh, j);		

			//蓝色
			//--------------------------------------------------
			temB = pFunc(firBlue, latBlue, areaHgh, j);		

			//显示象素
			//--------------------------------------------------
			//创建一个图像pos(0, 0), size(areaWid, areaHgh)
			SetPixelV( hBfr, i, j, RGB(temR, temG, temB) );
		}
	}

	//显示 也可以缩放
	StretchBlt( hdc, areaX, areaY, areaWid, areaHgh, 
				hBfr, 0, 0, areaWid , areaHgh, SRCCOPY );
}

unsigned char GetLinearColor( unsigned char first, unsigned char last, int areaHgh, int index )
{
	unsigned char temC = abs(first - last);
	float cOffset = (float)temC / (float)areaHgh;

	return first > last ? first - (unsigned char)(cOffset * index) :
						  first + (unsigned char)(cOffset * index) ;
}

unsigned char GetSinColor( unsigned char first, unsigned char last, int areaHgh, int index )
{
	unsigned char temC = abs( first - last);
	
	double sinValue = sin( 3.1415926 * (float)index / (float)areaHgh );

	return first > last ? first - (unsigned char)( temC * sinValue ):
						  first + (unsigned char)( temC * sinValue );
}


http://blog.csdn.net/ryfdizuo/article/details/4841173

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值