TextEffectsScenario.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System.Collections.Generic;
  2. namespace UICatalog.Scenarios;
  3. [ScenarioMetadata ("Text Effects", "Text Effects.")]
  4. [ScenarioCategory ("Colors")]
  5. [ScenarioCategory ("Text and Formatting")]
  6. public class TextEffectsScenario : Scenario
  7. {
  8. /// <summary>
  9. /// Enable or disable looping of the gradient colors.
  10. /// </summary>
  11. public static bool _loopingGradient;
  12. public override void Main ()
  13. {
  14. Application.Init ();
  15. var w = new Window
  16. {
  17. Width = Dim.Fill (),
  18. Height = Dim.Fill (),
  19. Title = "Text Effects Scenario"
  20. };
  21. w.Loaded += (s, e) => { SetupGradientLineCanvas (w, w.Frame.Size); };
  22. w.SizeChanging += (s, e) =>
  23. {
  24. if (e.Size.HasValue)
  25. {
  26. SetupGradientLineCanvas (w, e.Size.Value);
  27. }
  28. };
  29. w.SetScheme (new ()
  30. {
  31. Normal = new (ColorName16.White, ColorName16.Black),
  32. Focus = new (ColorName16.Black, ColorName16.White),
  33. HotNormal = new (ColorName16.White, ColorName16.Black),
  34. HotFocus = new (ColorName16.White, ColorName16.Black),
  35. Disabled = new (ColorName16.Gray, ColorName16.Black)
  36. });
  37. var gradientsView = new GradientsView
  38. {
  39. Width = Dim.Fill (),
  40. Height = Dim.Fill ()
  41. };
  42. var cbLooping = new CheckBox
  43. {
  44. Text = "Looping",
  45. Y = Pos.AnchorEnd (1)
  46. };
  47. cbLooping.CheckedStateChanging += (s, e) =>
  48. {
  49. _loopingGradient = e.Result == CheckState.Checked;
  50. SetupGradientLineCanvas (w, w.Frame.Size);
  51. };
  52. gradientsView.Add (cbLooping);
  53. w.Add (gradientsView);
  54. Application.Run (w);
  55. w.Dispose ();
  56. Application.Shutdown ();
  57. }
  58. private static void SetupGradientLineCanvas (View w, Size size)
  59. {
  60. GetAppealingGradientColors (out List<Color> stops, out List<int> steps);
  61. var g = new Gradient (stops, steps, _loopingGradient);
  62. var fore = new GradientFill (
  63. new (0, 0, size.Width, size.Height),
  64. g,
  65. GradientDirection.Diagonal);
  66. var back = new SolidFill (new (ColorName16.Black));
  67. w.LineCanvas.Fill = new (
  68. fore,
  69. back);
  70. }
  71. public static void GetAppealingGradientColors (out List<Color> stops, out List<int> steps)
  72. {
  73. // Define the colors of the gradient stops with more appealing colors
  74. stops =
  75. [
  76. new (0, 128, 255), // Bright Blue
  77. new (0, 255, 128), // Bright Green
  78. new (255, 255), // Bright Yellow
  79. new (255, 128), // Bright Orange
  80. new (255, 0, 128)
  81. ];
  82. // Define the number of steps between each color for smoother transitions
  83. // If we pass only a single value then it will assume equal steps between all pairs
  84. steps = [15];
  85. }
  86. }
  87. internal class GradientsView : View
  88. {
  89. private const int GRADIENT_WIDTH = 30;
  90. private const int GRADIENT_HEIGHT = 15;
  91. private const int LABEL_HEIGHT = 1;
  92. private const int GRADIENT_WITH_LABEL_HEIGHT = GRADIENT_HEIGHT + LABEL_HEIGHT + 1; // +1 for spacing
  93. protected override bool OnDrawingContent ()
  94. {
  95. DrawTopLineGradient (Viewport);
  96. var x = 2;
  97. var y = 3;
  98. List<(string Label, GradientDirection Direction)> gradients = new ()
  99. {
  100. ("Horizontal", GradientDirection.Horizontal),
  101. ("Vertical", GradientDirection.Vertical),
  102. ("Radial", GradientDirection.Radial),
  103. ("Diagonal", GradientDirection.Diagonal)
  104. };
  105. foreach ((string label, GradientDirection direction) in gradients)
  106. {
  107. if (x + GRADIENT_WIDTH > Viewport.Width)
  108. {
  109. x = 2; // Reset to left margin
  110. y += GRADIENT_WITH_LABEL_HEIGHT; // Move down to next row
  111. }
  112. DrawLabeledGradientArea (label, direction, x, y);
  113. x += GRADIENT_WIDTH + 2; // Move right for next gradient, +2 for spacing
  114. }
  115. return true;
  116. }
  117. private void DrawLabeledGradientArea (string label, GradientDirection direction, int xOffset, int yOffset)
  118. {
  119. DrawGradientArea (direction, xOffset, yOffset);
  120. CenterText (label, xOffset, yOffset + GRADIENT_HEIGHT); // Adjusted for text below the gradient
  121. }
  122. private void CenterText (string text, int xOffset, int yOffset)
  123. {
  124. if (yOffset + 1 >= Viewport.Height)
  125. {
  126. // Not enough space for label
  127. return;
  128. }
  129. int width = text.Length;
  130. int x = xOffset + (GRADIENT_WIDTH - width) / 2; // Center the text within the gradient area width
  131. SetAttribute (GetAttributeForRole (VisualRole.Normal));
  132. Move (x, yOffset + 1);
  133. AddStr (text);
  134. }
  135. private void DrawGradientArea (GradientDirection direction, int xOffset, int yOffset)
  136. {
  137. // Define the colors of the gradient stops
  138. List<Color> stops =
  139. [
  140. new (255, 0), // Red
  141. new (0, 255), // Green
  142. new (238, 130, 238)
  143. ];
  144. // Define the number of steps between each color
  145. List<int> steps = [10, 10]; // 10 steps between Red -> Green, and Green -> Blue
  146. // Create the gradient
  147. var radialGradient = new Gradient (stops, steps, TextEffectsScenario._loopingGradient);
  148. // Define the size of the rectangle
  149. int maxRow = GRADIENT_HEIGHT; // Adjusted to keep aspect ratio
  150. int maxColumn = GRADIENT_WIDTH;
  151. // Build the coordinate-color mapping for a radial gradient
  152. Dictionary<Point, Color> gradientMapping = radialGradient.BuildCoordinateColorMapping (maxRow, maxColumn, direction);
  153. // Print the gradient
  154. for (var row = 0; row <= maxRow; row++)
  155. {
  156. for (var col = 0; col <= maxColumn; col++)
  157. {
  158. var coord = new Point (col, row);
  159. Color color = gradientMapping [coord];
  160. SetColor (color);
  161. AddRune (col + xOffset, row + yOffset, new ('█'));
  162. }
  163. }
  164. }
  165. private void DrawTopLineGradient (Rectangle viewport)
  166. {
  167. // Define the colors of the rainbow
  168. List<Color> stops =
  169. [
  170. new (255, 0), // Red
  171. new (255, 165), // Orange
  172. new (255, 255), // Yellow
  173. new (0, 128), // Green
  174. new (0, 0, 255), // Blue
  175. new (75, 0, 130), // Indigo
  176. new (238, 130, 238)
  177. ];
  178. // Define the number of steps between each color
  179. List<int> steps =
  180. [
  181. 20, // between Red and Orange
  182. 20, // between Orange and Yellow
  183. 20, // between Yellow and Green
  184. 20, // between Green and Blue
  185. 20, // between Blue and Indigo
  186. 20
  187. ];
  188. // Create the gradient
  189. var rainbowGradient = new Gradient (stops, steps, TextEffectsScenario._loopingGradient);
  190. for (var x = 0; x < viewport.Width; x++)
  191. {
  192. double fraction = (double)x / (viewport.Width - 1);
  193. Color color = rainbowGradient.GetColorAtFraction (fraction);
  194. SetColor (color);
  195. AddRune (x, 0, new ('█'));
  196. }
  197. }
  198. private void SetColor (Color color) { SetAttribute (new (color, color)); }
  199. }