TextEffectsScenario.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using Terminal.Gui;
  6. using Terminal.Gui.Drawing;
  7. namespace UICatalog.Scenarios;
  8. [ScenarioMetadata ("Text Effects", "Text Effects.")]
  9. [ScenarioCategory ("Colors")]
  10. public class TextEffectsScenario : Scenario
  11. {
  12. private TabView tabView;
  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) =>
  23. {
  24. SetupGradientLineCanvas (w, w.Frame.Size);
  25. // TODO: Does not work
  26. // SetupGradientLineCanvas (tabView, tabView.Frame.Size);
  27. };
  28. w.SizeChanging += (s,e)=>
  29. {
  30. if(e.Size.HasValue)
  31. {
  32. SetupGradientLineCanvas (w, e.Size.Value);
  33. }
  34. // TODO: Does not work
  35. //SetupGradientLineCanvas (tabView, tabView.Frame.Size);
  36. };
  37. w.ColorScheme = new ColorScheme
  38. {
  39. Normal = new Terminal.Gui.Attribute (ColorName.White, ColorName.Black),
  40. Focus = new Terminal.Gui.Attribute (ColorName.Black,ColorName.White),
  41. HotNormal = new Terminal.Gui.Attribute (ColorName.White, ColorName.Black),
  42. HotFocus = new Terminal.Gui.Attribute (ColorName.White, ColorName.Black),
  43. Disabled = new Terminal.Gui.Attribute (ColorName.Gray, ColorName.Black)
  44. };
  45. // Creates a window that occupies the entire terminal with a title.
  46. tabView = new TabView ()
  47. {
  48. Width = Dim.Fill (),
  49. Height = Dim.Fill (),
  50. };
  51. var t1 = new Tab ()
  52. {
  53. View = new GradientsView ()
  54. {
  55. Width = Dim.Fill (),
  56. Height = Dim.Fill (),
  57. },
  58. DisplayText = "Gradients"
  59. };
  60. tabView.AddTab (t1,false);
  61. w.Add (tabView);
  62. Application.Run (w);
  63. w.Dispose ();
  64. Application.Shutdown ();
  65. this.Dispose ();
  66. }
  67. private void SetupGradientLineCanvas (View w, Size size)
  68. {
  69. GetAppealingGradientColors (out var stops, out var steps);
  70. var g = new Gradient (stops, steps);
  71. var fore = new GradientFill (
  72. new Rectangle (0, 0, size.Width, size.Height), g, Gradient.Direction.Diagonal);
  73. var back = new SolidFill (new Terminal.Gui.Color (ColorName.Black));
  74. w.LineCanvas.Fill = new FillPair (
  75. fore,
  76. back);
  77. }
  78. public static void GetAppealingGradientColors (out List<Color> stops, out List<int> steps)
  79. {
  80. // Define the colors of the gradient stops with more appealing colors
  81. stops = new List<Color>
  82. {
  83. new Color(0, 128, 255), // Bright Blue
  84. new Color(0, 255, 128), // Bright Green
  85. new Color(255, 255, 0), // Bright Yellow
  86. new Color(255, 128, 0), // Bright Orange
  87. new Color(255, 0, 128) // Bright Pink
  88. };
  89. // Define the number of steps between each color for smoother transitions
  90. steps = new List<int> { 15,15, 15, 15 };
  91. }
  92. }
  93. internal class GradientsView : View
  94. {
  95. public override void OnDrawContent (Rectangle viewport)
  96. {
  97. base.OnDrawContent (viewport);
  98. DrawTopLineGradient (viewport);
  99. int x = 2;
  100. int y = 3;
  101. if (viewport.Height < 25) // Not enough space, render in a single line
  102. {
  103. DrawGradientArea (Gradient.Direction.Horizontal, x, y);
  104. DrawGradientArea (Gradient.Direction.Vertical, x + 32, y);
  105. DrawGradientArea (Gradient.Direction.Radial, x + 64, y);
  106. DrawGradientArea (Gradient.Direction.Diagonal, x + 96, y);
  107. }
  108. else // Enough space, render in two lines
  109. {
  110. DrawGradientArea (Gradient.Direction.Horizontal, x, y);
  111. DrawGradientArea (Gradient.Direction.Vertical, x + 32, y);
  112. DrawGradientArea (Gradient.Direction.Radial, x, y + 17);
  113. DrawGradientArea (Gradient.Direction.Diagonal, x + 32, y + 17);
  114. }
  115. }
  116. private void DrawGradientArea (Gradient.Direction direction, int xOffset, int yOffset)
  117. {
  118. // Define the colors of the gradient stops
  119. var stops = new List<Color>
  120. {
  121. new Color(255, 0, 0), // Red
  122. new Color(0, 255, 0), // Green
  123. new Color(238, 130, 238) // Violet
  124. };
  125. // Define the number of steps between each color
  126. var steps = new List<int> { 10, 10 }; // 10 steps between Red -> Green, and Green -> Blue
  127. // Create the gradient
  128. var radialGradient = new Gradient (stops, steps, loop: false);
  129. // Define the size of the rectangle
  130. int maxRow = 15; // Adjusted to keep aspect ratio
  131. int maxColumn = 30;
  132. // Build the coordinate-color mapping for a radial gradient
  133. var gradientMapping = radialGradient.BuildCoordinateColorMapping (maxRow, maxColumn, direction);
  134. // Print the gradient
  135. for (int row = 0; row <= maxRow; row++)
  136. {
  137. for (int col = 0; col <= maxColumn; col++)
  138. {
  139. var coord = new Point (col, row);
  140. var color = gradientMapping [coord];
  141. SetColor (color);
  142. AddRune (col + xOffset, row + yOffset, new Rune ('█'));
  143. }
  144. }
  145. }
  146. private void DrawTopLineGradient (Rectangle viewport)
  147. {
  148. // Define the colors of the rainbow
  149. var stops = new List<Color>
  150. {
  151. new Color(255, 0, 0), // Red
  152. new Color(255, 165, 0), // Orange
  153. new Color(255, 255, 0), // Yellow
  154. new Color(0, 128, 0), // Green
  155. new Color(0, 0, 255), // Blue
  156. new Color(75, 0, 130), // Indigo
  157. new Color(238, 130, 238) // Violet
  158. };
  159. // Define the number of steps between each color
  160. var steps = new List<int>
  161. {
  162. 20, // between Red and Orange
  163. 20, // between Orange and Yellow
  164. 20, // between Yellow and Green
  165. 20, // between Green and Blue
  166. 20, // between Blue and Indigo
  167. 20 // between Indigo and Violet
  168. };
  169. // Create the gradient
  170. var rainbowGradient = new Gradient (stops, steps, loop: true);
  171. for (int x = 0; x < viewport.Width; x++)
  172. {
  173. double fraction = (double)x / (viewport.Width - 1);
  174. Color color = rainbowGradient.GetColorAtFraction (fraction);
  175. SetColor (color);
  176. AddRune (x, 0, new Rune ('█'));
  177. }
  178. }
  179. private void SetColor (Color color)
  180. {
  181. // Assuming AddRune is a method you have for drawing at specific positions
  182. Application.Driver.SetAttribute (
  183. new Attribute (
  184. new Terminal.Gui.Color (color.R, color.G, color.B),
  185. new Terminal.Gui.Color (color.R, color.G, color.B)
  186. )); // Setting color based on RGB
  187. }
  188. }