Gradient.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Gradient.cpp: implementation of the CGradient class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Gradient.h"
  6. #include "math.h"
  7. CGradient::CGradient()
  8. {
  9. }
  10. CGradient::~CGradient()
  11. {
  12. }
  13. // ˮƽÌݶÈ;
  14. void CGradient::HorizontalGradient(CDC* pDC, LPRECT lpRect, COLORREF sColor, COLORREF eColor, BOOL bGamma, double gamma)
  15. {
  16. // Gradient params
  17. int width = lpRect->right - lpRect->left - 1;
  18. int height = lpRect->bottom - lpRect->top - 1;
  19. // Draw gradient
  20. HBRUSH hBrush = NULL;
  21. double percent;
  22. unsigned char red, green, blue;
  23. COLORREF color;
  24. RECT rect;
  25. for (int i=0; i<width-1; i++)
  26. {
  27. // Gradient color percent
  28. percent = 1 - (double)i / (double)(width-2);
  29. // Gradient color
  30. red = (unsigned char)(GetRValue(sColor)*percent) + (unsigned char)(GetRValue(eColor)*(1-percent));
  31. green = (unsigned char)(GetGValue(sColor)*percent) + (unsigned char)(GetGValue(eColor)*(1-percent));
  32. blue = (unsigned char)(GetBValue(sColor)*percent) + (unsigned char)(GetBValue(eColor)*(1-percent));
  33. if (bGamma)
  34. {
  35. red = (unsigned char)(pow((double)red/255.0, gamma) * 255);
  36. green = (unsigned char)(pow((double)green/255.0, gamma) * 255);
  37. blue = (unsigned char)(pow((double)blue/255.0, gamma) * 255);
  38. }
  39. color = RGB(red, green, blue);
  40. // Gradient
  41. rect.left = lpRect->left + i + 1;
  42. rect.top = lpRect->top + 1;
  43. rect.right = rect.left + 1;
  44. rect.bottom = lpRect->bottom - 1;
  45. pDC->FillSolidRect(&rect, color);
  46. }
  47. }
  48. // ´¹Ö±ÌݶÈ;
  49. void CGradient::VerticalGradient(CDC* pDC, LPRECT lpRect, COLORREF sColor, COLORREF eColor, BOOL bGamma, double gamma)
  50. {
  51. // Gradient params
  52. int width = lpRect->right - lpRect->left - 1;
  53. int height = lpRect->bottom - lpRect->top - 1;
  54. // Draw gradient
  55. HBRUSH hBrush = NULL;
  56. double percent;
  57. unsigned char red, green, blue;
  58. COLORREF color;
  59. RECT rect;
  60. for (int i=0; i<height-1; i++)
  61. {
  62. // Gradient color percent
  63. percent = 1 - (double)i / (double)(height-2);
  64. // Gradient color
  65. red = (unsigned char)(GetRValue(sColor)*percent) + (unsigned char)(GetRValue(eColor)*(1-percent));
  66. green = (unsigned char)(GetGValue(sColor)*percent) + (unsigned char)(GetGValue(eColor)*(1-percent));
  67. blue = (unsigned char)(GetBValue(sColor)*percent) + (unsigned char)(GetBValue(eColor)*(1-percent));
  68. if (bGamma)
  69. {
  70. red = (unsigned char)(pow((double)red/255.0, gamma) * 255);
  71. green = (unsigned char)(pow((double)green/255.0, gamma) * 255);
  72. blue = (unsigned char)(pow((double)blue/255.0, gamma) * 255);
  73. }
  74. color = RGB(red, green, blue);
  75. // Gradient
  76. rect.left = lpRect->left + 1;
  77. rect.top = lpRect->top + i + 1;
  78. rect.right = lpRect->right - 1;
  79. rect.bottom = rect.top + 1;
  80. pDC->FillSolidRect(&rect, color);
  81. }
  82. }
  83. // ˮƽÌݶÈ;
  84. void CGradient::HorizontalGradient(CDC* pDC, HRGN hRgn, COLORREF sColor, COLORREF eColor, BOOL bGamma, double gamma)
  85. {
  86. RECT rgnRect;
  87. GetRgnBox(hRgn, &rgnRect);
  88. // Gradient params
  89. int width = rgnRect.right - rgnRect.left - 1;
  90. int height = rgnRect.bottom - rgnRect.top - 1;
  91. // Draw gradient
  92. HBRUSH hBrush = NULL;
  93. unsigned char red, green, blue;
  94. COLORREF color;
  95. int startY, endY;
  96. RECT rect;
  97. bool foundStart, foundEnd;
  98. double rrate = (GetRValue(sColor)-GetRValue(eColor))*1.0/width;
  99. double grate = (GetGValue(sColor)-GetGValue(eColor))*1.0/width;
  100. double brate = (GetBValue(sColor)-GetBValue(eColor))*1.0/width;
  101. unsigned char sred = GetRValue(sColor);
  102. unsigned char sgreen = GetGValue(sColor);
  103. unsigned char sblue = GetBValue(sColor);
  104. for (int i=0; i<width; i=i+2)
  105. {
  106. // Gradient color
  107. red = sred + (unsigned char)(rrate*i);
  108. green = sgreen + (unsigned char)(grate*i);
  109. blue = sblue + (unsigned char)(brate*i);
  110. color = RGB(red, green, blue);
  111. endY = rgnRect.top;
  112. while (endY<=rgnRect.bottom)
  113. {
  114. foundStart = foundEnd = false;
  115. // Top offset
  116. startY = endY;
  117. while (startY<=rgnRect.bottom)
  118. {
  119. if ( PtInRegion(hRgn, rgnRect.left+i, startY) )
  120. {
  121. foundStart = true;
  122. break;
  123. }
  124. else
  125. startY++;
  126. }
  127. // Bottom offset
  128. endY = startY;
  129. while (endY<=rgnRect.bottom)
  130. {
  131. if ( !PtInRegion(hRgn, rgnRect.left+i, endY) )
  132. {
  133. foundEnd = true;
  134. break;
  135. }
  136. else
  137. endY++;
  138. }
  139. if (foundStart && foundEnd)
  140. {
  141. // Gradient rectangle
  142. rect.left = rgnRect.left + i;
  143. rect.top = startY;
  144. rect.right = rect.left + 2;
  145. rect.bottom = endY;
  146. pDC->FillSolidRect(&rect, color);
  147. }
  148. }
  149. }
  150. }
  151. // ´¹Ö±ÌݶÈ;
  152. void CGradient::VerticalGradient(CDC* pDC, HRGN hRgn, COLORREF sColor, COLORREF eColor, BOOL bGamma, double gamma)
  153. {
  154. RECT rgnRect;
  155. GetRgnBox(hRgn, &rgnRect);
  156. // Gradient params
  157. int width = rgnRect.right - rgnRect.left - 1;
  158. int height = rgnRect.bottom - rgnRect.top - 1;
  159. // Draw gradient
  160. HBRUSH hBrush = NULL;
  161. double percent;
  162. unsigned char red, green, blue;
  163. COLORREF color;
  164. int startX, endX;
  165. RECT rect;
  166. bool foundStart, foundEnd;
  167. double rrate = (GetRValue(sColor)-GetRValue(eColor))*1.0/height;
  168. double grate = (GetGValue(sColor)-GetGValue(eColor))*1.0/height;
  169. double brate = (GetBValue(sColor)-GetBValue(eColor))*1.0/height;
  170. unsigned char sred = GetRValue(sColor);
  171. unsigned char sgreen = GetGValue(sColor);
  172. unsigned char sblue = GetBValue(sColor);
  173. for (int i=0; i<height; i=i+2)
  174. {
  175. // Gradient color percent
  176. percent = 1 - (double)i / (double)(height);
  177. // Gradient color
  178. red = sred + (unsigned char)(rrate*i);
  179. green = sgreen + (unsigned char)(grate*i);
  180. blue = sblue + (unsigned char)(brate*i);
  181. color = RGB(red, green, blue);
  182. endX = rgnRect.left;
  183. while (endX<=rgnRect.right)
  184. {
  185. foundStart = foundEnd = false;
  186. // Left offset
  187. startX = endX;
  188. while (startX<=rgnRect.right)
  189. {
  190. if ( PtInRegion(hRgn, startX, rgnRect.top+i) )
  191. {
  192. foundStart = true;
  193. break;
  194. }
  195. startX++;
  196. }
  197. // Right offset
  198. endX = startX;
  199. while (endX<=rgnRect.right)
  200. {
  201. if ( !PtInRegion(hRgn, endX, rgnRect.top+i) )
  202. {
  203. foundEnd = true;
  204. break;
  205. }
  206. endX++;
  207. }
  208. if (foundStart && foundEnd)
  209. {
  210. // Gradient rectangle
  211. rect.left = startX;
  212. rect.top = rgnRect.top + i;
  213. rect.right = endX;
  214. rect.bottom = rect.top + 2;
  215. pDC->FillSolidRect(&rect, color);
  216. }
  217. }
  218. }
  219. }