Gradient.cpp 6.0 KB

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