/ExtLib/GdiPlus/Demos/GdiPlus10/Filling Shapes with a Gradient Brush/uDemoPathGradient.pas

http://sx-library.googlecode.com/ · Pascal · 314 lines · 176 code · 29 blank · 109 comment · 0 complexity · e3e5274b51e7220b13116da6091f1a9d MD5 · raw file

  1. unit uDemoPathGradient;
  2. interface
  3. uses
  4. GdiPlus,
  5. uDemo;
  6. type
  7. TDemoPathGradient = class(TDemo)
  8. strict private
  9. procedure Example1;
  10. procedure Example2;
  11. procedure Example3;
  12. procedure Example4;
  13. procedure Example5;
  14. procedure Example6;
  15. procedure Example7;
  16. strict protected
  17. procedure Run; override;
  18. end;
  19. implementation
  20. { TDemoPathGradient }
  21. {$REGION}
  22. /// The <A>IGPPathGradientBrush</A> interface allows you to customize the way you
  23. /// fill a shape with gradually changing colors. A <A>IGPPathGradientBrush</A>
  24. /// object has a boundary path and a center point. You can specify one color for
  25. /// the center point and another color for the boundary. You can also specify
  26. /// separate colors for each of several points along the boundary.
  27. ///
  28. /// Note In GDI+, a path is a sequence of lines and curves maintained by a
  29. /// <A>IGPGraphicsPath</A> object. For more information about GDI+ paths, see
  30. /// the examples under <A>Constructing and Drawing Paths</A>.
  31. ///
  32. /// The following example fills an ellipse with a path gradient brush. The
  33. /// center color is set to blue and the boundary color is set to aqua.
  34. procedure TDemoPathGradient.Example1;
  35. var
  36. Path: IGPGraphicsPath;
  37. Brush: IGPPathGradientBrush;
  38. SurroundColors: array [0..0] of TGPColor;
  39. begin
  40. Path := TGPGraphicsPath.Create;
  41. Path.AddEllipse(0, 0, 140, 70);
  42. Brush := TGPPathGradientBrush.Create(Path);
  43. Brush.CenterColor := TGPColor.Create(255, 0, 0, 255);
  44. SurroundColors[0].Initialize(255, 0, 255, 255);
  45. Brush.SetSurroundColors(SurroundColors);
  46. Graphics.FillEllipse(Brush, 0, 0, 140, 70);
  47. end;
  48. /// The top-left illustration above shows the filled ellipse.
  49. ///
  50. /// By default, a path gradient brush does not extend outside the boundary of
  51. /// the path. If you use the path gradient brush to fill a shape that extends
  52. /// beyond the boundary of the path, the area of the screen outside the path
  53. /// will not be filled. The next illustration aboveshows what happens if you
  54. /// change the <A>FillEllipse</A> call in the preceding code to the following:
  55. procedure TDemoPathGradient.Example2;
  56. var
  57. Path: IGPGraphicsPath;
  58. Brush: IGPPathGradientBrush;
  59. SurroundColors: array [0..0] of TGPColor;
  60. begin
  61. Path := TGPGraphicsPath.Create;
  62. Path.AddEllipse(150, 0, 140, 70);
  63. Brush := TGPPathGradientBrush.Create(Path);
  64. Brush.CenterColor := TGPColor.Create(255, 0, 0, 255);
  65. SurroundColors[0].Initialize(255, 0, 255, 255);
  66. Brush.SetSurroundColors(SurroundColors);
  67. Graphics.FillRectangle(Brush, 150, 10, 200, 40);
  68. end;
  69. /// <H>Specifying Points on the Boundary</H>
  70. /// The following example constructs a path gradient brush from a star-shaped
  71. /// path. The code sets the <A>CenterColor</A> property to the color at the
  72. /// centroid of the star to red. Then the code calls the <A>SetSurroundColors</A>
  73. /// method to specify various colors (stored in the colors array) at the
  74. /// individual points in the points array. The final code statement fills the
  75. /// star-shaped path with the path gradient brush.
  76. procedure TDemoPathGradient.Example3;
  77. const
  78. Points: array [0..9] of TGPPoint = (
  79. (X: 375; Y: 0), (X: 400; Y: 50),
  80. (X: 450; Y: 50), (X: 412; Y: 75),
  81. (X: 450; Y: 150), (X: 375; Y: 100),
  82. (X: 300; Y: 150), (X: 337; Y: 75),
  83. (X: 300; Y: 50), (X: 350; Y: 50));
  84. var
  85. Path: IGPGraphicsPath;
  86. Brush: IGPPathGradientBrush;
  87. SurroundColors: array [0..9] of TGPColor;
  88. begin
  89. Path := TGPGraphicsPath.Create;
  90. Path.AddLines(Points);
  91. Brush := TGPPathGradientBrush.Create(Path);
  92. Brush.CenterColor := TGPColor.Create(255, 255, 0, 0);
  93. SurroundColors[0].Initialize(255, 0, 0, 0);
  94. SurroundColors[1].Initialize(255, 0, 255, 0);
  95. SurroundColors[2].Initialize(255, 0, 0, 255);
  96. SurroundColors[3].Initialize(255, 255, 255, 255);
  97. SurroundColors[4].Initialize(255, 0, 0, 0);
  98. SurroundColors[5].Initialize(255, 0, 255, 0);
  99. SurroundColors[6].Initialize(255, 0, 0, 255);
  100. SurroundColors[7].Initialize(255, 255, 255, 255);
  101. SurroundColors[8].Initialize(255, 0, 0, 0);
  102. SurroundColors[9].Initialize(255, 0, 255, 0);
  103. Brush.SetSurroundColors(SurroundColors);
  104. Graphics.FillPath(Brush, Path);
  105. end;
  106. /// The 3rd illustration above shows the filled star.
  107. ///
  108. /// The following example constructs a path gradient brush based on an array of
  109. /// points. A color is assigned to each of the five points in the array. If you
  110. /// were to connect the five points by straight lines, you would get a
  111. /// five-sided polygon. A color is also assigned to the center (centroid) of
  112. /// that polygon &#x2014; in this example, the center is set to white. The final code
  113. /// statement in the example fills a rectangle with the path gradient brush.
  114. ///
  115. /// The color used to fill the rectangle is white at the center and changes
  116. /// gradually as you move away from the center toward the points in the array.
  117. /// For example, as you move from the center to the top-left corner, the color
  118. /// changes gradually from white to red, and as you move from the center to the
  119. /// top-right corner, the color changes gradually from white to green.
  120. procedure TDemoPathGradient.Example4;
  121. const
  122. Points: array [0..4] of TGPPointF = (
  123. (X: 460; Y: 0),
  124. (X: 620; Y: 0),
  125. (X: 620; Y: 200),
  126. (X: 540; Y: 150),
  127. (X: 460; Y: 200));
  128. var
  129. Brush: IGPPathGradientBrush;
  130. Pen: IGPPen;
  131. SurroundColors: array [0..4] of TGPColor;
  132. begin
  133. Brush := TGPPathGradientBrush.Create(Points);
  134. Brush.CenterColor := TGPColor.White;
  135. SurroundColors[0].Initialize(255, 255, 0, 0);
  136. SurroundColors[1].Initialize(255, 0, 255, 0);
  137. SurroundColors[2].Initialize(255, 0, 255, 0);
  138. SurroundColors[3].Initialize(255, 0, 0, 255);
  139. SurroundColors[4].Initialize(255, 255, 0, 0);
  140. Brush.SetSurroundColors(SurroundColors);
  141. Graphics.FillRectangle(Brush, 460, 0, 180, 220);
  142. Pen := TGPPen.Create(TGPColor.Blue);
  143. Graphics.DrawRectangle(Pen, 460, 0, 180, 220);
  144. end;
  145. /// Note that there is no <A>IGPGraphicsPath</A> object in the preceding code. The
  146. /// particular <A>TGPPathGradientBrush</A> constructor in the example receives an
  147. /// array of points but does not require a <A>IGPGraphicsPath</A> object. Also,
  148. /// note that the path gradient brush is used to fill a rectangle, not a path.
  149. /// The rectangle is larger than the path used to define the brush, so some of
  150. /// the rectangle is not painted by the brush. The 4th illustration above shows
  151. /// the rectangle (blue line) and the portion of the rectangle painted by the
  152. /// path gradient brush.
  153. ///
  154. /// <H>Customizing a Path Gradient</H>
  155. /// One way to customize a path gradient brush is to set its focus scales. The
  156. /// focus scales specify an inner path that lies inside the main path. The
  157. /// center color is displayed everywhere inside that inner path rather than only
  158. /// at the center point. To set the focus scales of a path gradient brush, call
  159. /// the <A>SetFocusScales</A> method.
  160. ///
  161. /// The following example creates a path gradient brush based on an elliptical
  162. /// path. The code sets the boundary color to blue, sets the center color to
  163. /// aqua, and then uses the path gradient brush to fill the elliptical path.
  164. ///
  165. /// Next the code sets the focus scales of the path gradient brush. The x focus
  166. /// scale is set to 0.3, and the y focus scale is set to 0.8. The code calls the
  167. /// <A>TranslateTransform</A> method of a <A>IGPGraphics</A> object so that the
  168. /// subsequent call to <A>FillPath</A> fills an ellipse that sits to the right
  169. /// of the first ellipse.
  170. ///
  171. /// To see the effect of the focus scales, imagine a small ellipse that shares
  172. /// its center with the main ellipse. The small (inner) ellipse is the main
  173. /// ellipse scaled (about its center) horizontally by a factor of 0.3 and
  174. /// vertically by a factor of 0.8. As you move from the boundary of the outer
  175. /// ellipse to the boundary of the inner ellipse, the color changes gradually
  176. /// from blue to aqua. As you move from the boundary of the inner ellipse to the
  177. /// shared center, the color remains aqua.
  178. procedure TDemoPathGradient.Example5;
  179. var
  180. Path: IGPGraphicsPath;
  181. Brush: IGPPathGradientBrush;
  182. SurroundColors: array [0..0] of TGPColor;
  183. begin
  184. Path := TGPGraphicsPath.Create;
  185. Path.AddEllipse(0, 230, 200, 100);
  186. Brush := TGPPathGradientBrush.Create(Path);
  187. Brush.GammaCorrection := True;
  188. SurroundColors[0].Initialize(255, 0, 0, 255);
  189. Brush.SetSurroundColors(SurroundColors);
  190. Brush.CenterColor := TGPColor.Create(255, 0, 255, 255);
  191. Graphics.FillPath(Brush, Path);
  192. Brush.SetFocusScales(0.3, 0.8);
  193. Graphics.TranslateTransform(220, 0);
  194. Graphics.FillPath(Brush, Path);
  195. Graphics.ResetTransform;
  196. end;
  197. /// The fifth illustration above (the first on the 2nd row) shows the output of
  198. /// the preceding code. The ellipse on the left is aqua only at the center
  199. /// point. The ellipse on the right is aqua everywhere inside the inner path.
  200. ///
  201. /// Another way to customize a path gradient brush is to specify an array of
  202. /// preset colors and an array of interpolation positions.
  203. ///
  204. /// The following example creates a path gradient brush based on a triangle. The
  205. /// code sets the <A>IGPPathGradientBrush.InterpolationColors</A> property of the
  206. /// path gradient brush to specify an array of interpolation colors (dark green,
  207. /// aqua, blue) and an array of interpolation positions (0, 0.25, 1). As you
  208. /// move from the boundary of the triangle to the center point, the color
  209. /// changes gradually from dark green to aqua and then from aqua to blue. The
  210. /// change from dark green to aqua happens in 25 percent of the distance from
  211. /// dark green to blue.
  212. procedure TDemoPathGradient.Example6;
  213. const
  214. Points: array [0..2] of TGPPoint = (
  215. (X: 540; Y: 230), (X: 640; Y: 430), (X: 440; Y: 430));
  216. InterPositions: array [0..2] of Single = (
  217. 0.00, // Dark green is at the boundary of the triangle.
  218. 0.25, // Aqua is 25 percent of the way from the boundary to the center point
  219. 1.00); // Blue is at the center point.
  220. var
  221. Brush: IGPPathGradientBrush;
  222. PresetColors: array [0..2] of TGPColor;
  223. Blend: IGPColorBlend;
  224. begin
  225. Brush := TGPPathGradientBrush.Create(Points);
  226. PresetColors[0] := TGPColor.Green;
  227. PresetColors[1] := TGPColor.Aqua;
  228. PresetColors[2] := TGPColor.Blue;
  229. Blend := TGPColorBlend.Create(PresetColors, InterPositions);
  230. Brush.InterpolationColors := Blend;
  231. Graphics.FillRectangle(Brush, 440, 230, 200, 200);
  232. end;
  233. /// The triangle in the illustration above shows the output of the preceding
  234. /// code.
  235. ///
  236. /// <H>Setting the Center Point</H>
  237. /// By default, the center point of a path gradient brush is at the centroid of
  238. /// the path used to construct the brush. You can change the location of the
  239. /// center point by setting the <A>IGPPathGradientBrush.CenterPoint</A> property
  240. /// of the <A>IGPPathGradientBrush</A> interface.
  241. ///
  242. /// The following example creates a path gradient brush based on an ellipse. The
  243. /// center of the ellipse is at (70, 385), but the center point of the path
  244. /// gradient brush is set to (120, 390).
  245. procedure TDemoPathGradient.Example7;
  246. var
  247. Path: IGPGraphicsPath;
  248. Brush: IGPPathGradientBrush;
  249. SurroundColors: array [0..0] of TGPColor;
  250. begin
  251. Path := TGPGraphicsPath.Create;
  252. Path.AddEllipse(0, 350, 140, 70);
  253. Brush := TGPPathGradientBrush.Create(Path);
  254. Brush.CenterPoint := TGPPointF.Create(120, 390);
  255. SurroundColors[0] := TGPColor.Aqua;
  256. Brush.SetSurroundColors(SurroundColors);
  257. Brush.CenterColor := TGPColor.Blue;
  258. Graphics.FillEllipse(Brush, 0, 350, 140, 70);
  259. Brush.CenterPoint := TGPPointF.Create(145, 385);
  260. SurroundColors[0] := TGPColor.Yellow;
  261. Brush.SetSurroundColors(SurroundColors);
  262. Brush.CenterColor := TGPColor.Red;
  263. Graphics.TranslateTransform(150, 0);
  264. Graphics.FillEllipse(Brush, 0, 350, 140, 70);
  265. end;
  266. /// The next illustration above shows the filled ellipse and the center point of
  267. /// the path gradient brush.
  268. ///
  269. /// You can set the center point of a path gradient brush to a location outside
  270. /// the path that was used to construct the brush, as you can see in the final
  271. /// illustration above.
  272. {$ENDREGION}
  273. procedure TDemoPathGradient.Run;
  274. begin
  275. Example1;
  276. Example2;
  277. Example3;
  278. Example4;
  279. Example5;
  280. Example6;
  281. Example7;
  282. end;
  283. initialization
  284. RegisterDemo('Filling Shapes with a Gradient Brush\Creating a Path Gradient', TDemoPathGradient);
  285. end.