TextEffectsScenario.cs 7.5 KB

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