TextEffectsScenario.cs 7.6 KB

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