实现了渐变和按照正弦曲线变化,效果如图:
上图为竖直放向颜色按照正弦变化,下图为线性渐变。
只要有好的函数,就可以实现出更好的填充算法。
代码如下,程序用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 );
}