123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- // Gradient.cpp: implementation of the CGradient class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Gradient.h"
- #include "math.h"
- CGradient::CGradient()
- {
- }
- CGradient::~CGradient()
- {
- }
- // ˮƽÌݶÈ;
- void CGradient::HorizontalGradient(CDC* pDC, LPRECT lpRect, COLORREF sColor, COLORREF eColor, BOOL bGamma, double gamma)
- {
- // Gradient params
- int width = lpRect->right - lpRect->left - 1;
- int height = lpRect->bottom - lpRect->top - 1;
- // Draw gradient
- HBRUSH hBrush = NULL;
- double percent;
- unsigned char red, green, blue;
- COLORREF color;
- RECT rect;
- for (int i=0; i<width-1; i++)
- {
- // Gradient color percent
- percent = 1 - (double)i / (double)(width-2);
- // Gradient color
- red = (unsigned char)(GetRValue(sColor)*percent) + (unsigned char)(GetRValue(eColor)*(1-percent));
- green = (unsigned char)(GetGValue(sColor)*percent) + (unsigned char)(GetGValue(eColor)*(1-percent));
- blue = (unsigned char)(GetBValue(sColor)*percent) + (unsigned char)(GetBValue(eColor)*(1-percent));
- if (bGamma)
- {
- red = (unsigned char)(pow((double)red/255.0, gamma) * 255);
- green = (unsigned char)(pow((double)green/255.0, gamma) * 255);
- blue = (unsigned char)(pow((double)blue/255.0, gamma) * 255);
- }
- color = RGB(red, green, blue);
- // Gradient
- rect.left = lpRect->left + i + 1;
- rect.top = lpRect->top + 1;
- rect.right = rect.left + 1;
- rect.bottom = lpRect->bottom - 1;
- pDC->FillSolidRect(&rect, color);
- }
- }
- // ´¹Ö±ÌݶÈ;
- void CGradient::VerticalGradient(CDC* pDC, LPRECT lpRect, COLORREF sColor, COLORREF eColor, BOOL bGamma, double gamma)
- {
- // Gradient params
- int width = lpRect->right - lpRect->left - 1;
- int height = lpRect->bottom - lpRect->top - 1;
- // Draw gradient
- HBRUSH hBrush = NULL;
- double percent;
- unsigned char red, green, blue;
- COLORREF color;
- RECT rect;
- for (int i=0; i<height-1; i++)
- {
- // Gradient color percent
- percent = 1 - (double)i / (double)(height-2);
- // Gradient color
- red = (unsigned char)(GetRValue(sColor)*percent) + (unsigned char)(GetRValue(eColor)*(1-percent));
- green = (unsigned char)(GetGValue(sColor)*percent) + (unsigned char)(GetGValue(eColor)*(1-percent));
- blue = (unsigned char)(GetBValue(sColor)*percent) + (unsigned char)(GetBValue(eColor)*(1-percent));
- if (bGamma)
- {
- red = (unsigned char)(pow((double)red/255.0, gamma) * 255);
- green = (unsigned char)(pow((double)green/255.0, gamma) * 255);
- blue = (unsigned char)(pow((double)blue/255.0, gamma) * 255);
- }
- color = RGB(red, green, blue);
- // Gradient
- rect.left = lpRect->left + 1;
- rect.top = lpRect->top + i + 1;
- rect.right = lpRect->right - 1;
- rect.bottom = rect.top + 1;
- pDC->FillSolidRect(&rect, color);
- }
- }
- // ˮƽÌݶÈ;
- void CGradient::HorizontalGradient(CDC* pDC, HRGN hRgn, COLORREF sColor, COLORREF eColor, BOOL bGamma, double gamma)
- {
- RECT rgnRect;
- GetRgnBox(hRgn, &rgnRect);
- // Gradient params
- int width = rgnRect.right - rgnRect.left - 1;
- int height = rgnRect.bottom - rgnRect.top - 1;
- // Draw gradient
- HBRUSH hBrush = NULL;
- unsigned char red, green, blue;
- COLORREF color;
- int startY, endY;
- RECT rect;
- bool foundStart, foundEnd;
- double rrate = (GetRValue(sColor)-GetRValue(eColor))*1.0/width;
- double grate = (GetGValue(sColor)-GetGValue(eColor))*1.0/width;
- double brate = (GetBValue(sColor)-GetBValue(eColor))*1.0/width;
- unsigned char sred = GetRValue(sColor);
- unsigned char sgreen = GetGValue(sColor);
- unsigned char sblue = GetBValue(sColor);
- for (int i=0; i<width; i=i+2)
- {
- // Gradient color
- red = sred + (unsigned char)(rrate*i);
- green = sgreen + (unsigned char)(grate*i);
- blue = sblue + (unsigned char)(brate*i);
- color = RGB(red, green, blue);
- endY = rgnRect.top;
- while (endY<=rgnRect.bottom)
- {
- foundStart = foundEnd = false;
- // Top offset
- startY = endY;
- while (startY<=rgnRect.bottom)
- {
- if ( PtInRegion(hRgn, rgnRect.left+i, startY) )
- {
- foundStart = true;
- break;
- }
- else
- startY++;
- }
- // Bottom offset
- endY = startY;
- while (endY<=rgnRect.bottom)
- {
- if ( !PtInRegion(hRgn, rgnRect.left+i, endY) )
- {
- foundEnd = true;
- break;
- }
- else
- endY++;
- }
- if (foundStart && foundEnd)
- {
- // Gradient rectangle
- rect.left = rgnRect.left + i;
- rect.top = startY;
- rect.right = rect.left + 2;
- rect.bottom = endY;
- pDC->FillSolidRect(&rect, color);
- }
- }
- }
- }
- // ´¹Ö±ÌݶÈ;
- void CGradient::VerticalGradient(CDC* pDC, HRGN hRgn, COLORREF sColor, COLORREF eColor, BOOL bGamma, double gamma)
- {
- RECT rgnRect;
- GetRgnBox(hRgn, &rgnRect);
- // Gradient params
- int width = rgnRect.right - rgnRect.left - 1;
- int height = rgnRect.bottom - rgnRect.top - 1;
- // Draw gradient
- HBRUSH hBrush = NULL;
- double percent;
- unsigned char red, green, blue;
- COLORREF color;
- int startX, endX;
- RECT rect;
- bool foundStart, foundEnd;
- double rrate = (GetRValue(sColor)-GetRValue(eColor))*1.0/height;
- double grate = (GetGValue(sColor)-GetGValue(eColor))*1.0/height;
- double brate = (GetBValue(sColor)-GetBValue(eColor))*1.0/height;
- unsigned char sred = GetRValue(sColor);
- unsigned char sgreen = GetGValue(sColor);
- unsigned char sblue = GetBValue(sColor);
- for (int i=0; i<height; i=i+2)
- {
- // Gradient color percent
- percent = 1 - (double)i / (double)(height);
- // Gradient color
- red = sred + (unsigned char)(rrate*i);
- green = sgreen + (unsigned char)(grate*i);
- blue = sblue + (unsigned char)(brate*i);
- color = RGB(red, green, blue);
- endX = rgnRect.left;
- while (endX<=rgnRect.right)
- {
- foundStart = foundEnd = false;
- // Left offset
- startX = endX;
- while (startX<=rgnRect.right)
- {
- if ( PtInRegion(hRgn, startX, rgnRect.top+i) )
- {
- foundStart = true;
- break;
- }
-
- startX++;
- }
- // Right offset
- endX = startX;
- while (endX<=rgnRect.right)
- {
- if ( !PtInRegion(hRgn, endX, rgnRect.top+i) )
- {
- foundEnd = true;
- break;
- }
-
- endX++;
- }
- if (foundStart && foundEnd)
- {
- // Gradient rectangle
- rect.left = startX;
- rect.top = rgnRect.top + i;
- rect.right = endX;
- rect.bottom = rect.top + 2;
- pDC->FillSolidRect(&rect, color);
- }
- }
- }
- }
|