Hydrotronics

Paintutils for C#

Mar 23rd, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.24 KB | None | 0 0
  1. public static class Paintutils
  2.     {
  3.         public static ConsoleColor ToNearestConsoleColor(this Color color)
  4.         {
  5.             ConsoleColor ClosestColor = 0;
  6.             double delta = double.MaxValue;
  7.  
  8.             foreach (ConsoleColor consoleColor in Enum.GetValues(typeof(ConsoleColor)))
  9.             {
  10.                 string ConsoleColorName = Enum.GetName(typeof(ConsoleColor), consoleColor);
  11.                 ConsoleColorName = string.Equals(ConsoleColorName, nameof(ConsoleColor.DarkYellow), StringComparison.Ordinal) ? nameof(Color.Orange) : ConsoleColorName;
  12.                 Color rgbColor = Color.FromName(ConsoleColorName);
  13.                 double sum = Math.Pow(rgbColor.R - color.R, 2) + Math.Pow(rgbColor.G + color.G, 2) + Math.Pow(rgbColor.B + color.B, 2);
  14.  
  15.                 double epsilon = 2.0;
  16.                 if (sum < epsilon)
  17.                 {
  18.                     return consoleColor;
  19.                 }
  20.  
  21.                 if (sum < delta)
  22.                 {
  23.                     delta = sum;
  24.                     ClosestColor = consoleColor;
  25.                 }
  26.             }
  27.             return ClosestColor;
  28.         }
  29.         public static Color CreateColor(int R, int G, int B)
  30.         {
  31.             Color newColor = Color.FromArgb(R, G, B);
  32.             return newColor;
  33.         }
  34.         public static void SetBackground(ConsoleColor color)
  35.         {
  36.             Console.BackgroundColor = color;
  37.             Console.Clear();
  38.         }
  39.         public static void DrawPixel(int x, int y, ConsoleColor color)
  40.         {
  41.             if (x < 0 || x > Console.WindowWidth - 1 || y < 0 || y > Console.WindowHeight - 1)
  42.             {
  43.                 throw new ArgumentException("Position Out Of Bounds");
  44.             }
  45.             Console.BackgroundColor = color;
  46.             Console.SetCursorPosition(x, y);
  47.             Console.Write(" ");
  48.         }
  49.         public static void DrawPixel(int x, int y, int color)
  50.         {
  51.             if (x < 0 || x > Console.WindowWidth - 1 || y < 0 || y > Console.WindowHeight - 1)
  52.             {
  53.                 throw new ArgumentException("Position Out Of Bounds");
  54.             }
  55.             Console.BackgroundColor = (ConsoleColor)color;
  56.             Console.SetCursorPosition(x, y);
  57.             Console.Write(" ");
  58.         }
  59.         public static List<int> LoadImage(string path)
  60.         {
  61.             string line = "";
  62.             List<string> data = new List<string>();
  63.             List<string> split = new List<string>();
  64.             List<int> lImage = new List<int>();
  65.  
  66.             if (!File.Exists(path))
  67.             {
  68.                 throw new System.ArgumentException("Path does not exist");
  69.             }
  70.  
  71.             using (StreamReader fs = new StreamReader(path))
  72.             {
  73.                 line = fs.ReadLine();
  74.                 while (line != null)
  75.                 {
  76.                     data.Add(line);
  77.                     data.Add("|");
  78.                     line = fs.ReadLine();
  79.                 }
  80.             }
  81.             foreach (string ln in data)
  82.             {
  83.                 try
  84.                 {
  85.                     if (ln == "|")
  86.                     {
  87.                         split.Add("|");
  88.                     } else
  89.                     {
  90.                         foreach (char c in ln)
  91.                         {
  92.                             split.Add(Convert.ToString(c));
  93.                         }
  94.                     }
  95.                 } catch {}
  96.             }
  97.             foreach (string c in split)
  98.             {
  99.                 if (c != null)
  100.                 {
  101.                     switch (c)
  102.                     {
  103.                         case " ":
  104.                             lImage.Add(16);
  105.                             break;
  106.                         case "|":
  107.                             lImage.Add(17);
  108.                             break;
  109.                         default:
  110.                             lImage.Add(Convert.ToInt32(c, 16));
  111.                             break;
  112.                     }
  113.                 }
  114.             }
  115.             return lImage;
  116.         }
  117.  
  118.         public static void DrawImage(List<int> image, int x, int y)
  119.         {
  120.             int lines = 0;
  121.             int pos = 0;
  122.            
  123.             foreach (int code in image)
  124.             {
  125.                 switch (code)
  126.                 {
  127.                     case 16:
  128.                         pos++;
  129.                         break;
  130.                     case 17:
  131.                         pos = 0;
  132.                         lines++;
  133.                         break;
  134.                     default:
  135.                         Paintutils.DrawPixel(x + pos, y + lines, code);
  136.                         pos++;
  137.                         break;
  138.                 }
  139.             }
  140.         }
  141.  
  142.         public static void DrawLine(int x1, int y1, int x2, int y2, ConsoleColor color)
  143.         {
  144.             if (x1 == x2 & y1 == y2)
  145.             {
  146.                 Paintutils.DrawPixel(x1, y2, color);
  147.                 return;
  148.             }
  149.  
  150.             int minX = Math.Min(x1, x2);
  151.             int maxX = Math.Max(x1, x2);
  152.             int minY = Math.Min(y1, y2);
  153.             int maxY = Math.Max(y1, y2);
  154.  
  155.             decimal xDiff = maxX - minX;
  156.             decimal yDiff = maxY - minY;
  157.  
  158.             if (xDiff > yDiff)
  159.             {
  160.                 decimal y = minY;
  161.                 decimal dyy = (yDiff / xDiff);
  162.                 for (int x = minX; x < maxX; x++)
  163.                 {
  164.                     Paintutils.DrawPixel(x, Convert.ToInt32(y), color);
  165.                     y += dyy;
  166.                 }
  167.             } else
  168.             {
  169.                 decimal x = minX;
  170.                 decimal dxx = xDiff / yDiff;
  171.                
  172.                 for (int y = minY;y < maxY;y++)
  173.                 {
  174.                     Paintutils.DrawPixel(Convert.ToInt32(x), y, color);
  175.                     x += dxx;
  176.                 }
  177.             }
  178.         }
  179.  
  180.         public static void DrawLine(int x1, int y1, int x2, int y2, int colour)
  181.         {
  182.             ConsoleColor color = (ConsoleColor)colour;
  183.             if (x1 == x2 & y1 == y2)
  184.             {
  185.                 Paintutils.DrawPixel(x1, y2, color);
  186.                 return;
  187.             }
  188.  
  189.             int minX = Math.Min(x1, x2);
  190.             int maxX = Math.Max(x1, x2);
  191.             int minY = Math.Min(y1, y2);
  192.             int maxY = Math.Max(y1, y2);
  193.  
  194.             decimal xDiff = maxX - minX;
  195.             decimal yDiff = maxY - minY;
  196.  
  197.             if (xDiff > yDiff)
  198.             {
  199.                 decimal y = minY;
  200.                 decimal dyy = (yDiff / xDiff);
  201.                 for (int x = minX; x < maxX; x++)
  202.                 {
  203.                     Paintutils.DrawPixel(x, Convert.ToInt32(y), color);
  204.                     y += dyy;
  205.                 }
  206.             }
  207.             else
  208.             {
  209.                 decimal x = minX;
  210.                 decimal dxx = xDiff / yDiff;
  211.  
  212.                 for (int y = minY; y < maxY; y++)
  213.                 {
  214.                     Paintutils.DrawPixel(Convert.ToInt32(x), y, color);
  215.                     x += dxx;
  216.                 }
  217.             }
  218.         }
  219.  
  220.         public static void DrawBox(int x1, int y1, int x2, int y2, ConsoleColor color)
  221.         {
  222.             if (x1 == x2 & y1 == y2)
  223.             {
  224.                 Paintutils.DrawPixel(x1, y1, color);
  225.             } else if(x1 == x2 || y1 == y2)
  226.             {
  227.                 DrawLine(x1, y1, x2, y2,color);
  228.             }
  229.             Paintutils.DrawLine(x1, y1, x2, y1, color);
  230.             Paintutils.DrawLine(x1, y1+1, x1, y2+1, color);
  231.             Paintutils.DrawLine(x2, y2, x1+1, y2, color);
  232.             Paintutils.DrawLine(x2, y2+1, x2, y1, color);
  233.         }
  234.         public static void DrawBox(int x1, int y1, int x2, int y2, int colour)
  235.         {
  236.             ConsoleColor color = (ConsoleColor)colour;
  237.             if (x1 == x2 & y1 == y2)
  238.             {
  239.                 Paintutils.DrawPixel(x1, y1, color);
  240.             }
  241.             else if (x1 == x2 || y1 == y2)
  242.             {
  243.                 DrawLine(x1, y1, x2, y2, color);
  244.             }
  245.             Paintutils.DrawLine(x1, y1, x2, y1, color);
  246.             Paintutils.DrawLine(x1, y1 + 1, x1, y2 + 1, color);
  247.             Paintutils.DrawLine(x2, y2, x1 + 1, y2, color);
  248.             Paintutils.DrawLine(x2, y2 + 1, x2, y1, color);
  249.         }
  250.  
  251.         public static void DrawFilledBox(int x1, int y1, int x2, int y2, ConsoleColor color)
  252.         {
  253.             if (x1 == x2 & y1 == y2)
  254.             {
  255.                 Paintutils.DrawPixel(x1, y1, color);
  256.             }
  257.             else if (x1 == x2 || y1 == y2)
  258.             {
  259.                 DrawLine(x1, y1, x2, y2, color);
  260.             }
  261.             int minX = Math.Min(x1, x2);
  262.             int maxX = Math.Max(x1, x2);
  263.             int minY = Math.Min(y1, y2);
  264.             int maxY = Math.Max(y1, y2);
  265.  
  266.             decimal xDiff = maxX - minX;
  267.             decimal yDiff = maxY - minY;
  268.  
  269.             decimal lDiff = Math.Max(xDiff, yDiff);
  270.             int start;
  271.             if (lDiff == xDiff)
  272.             {
  273.                 start = minX;
  274.             } else
  275.             {
  276.                 start = minY;
  277.             }
  278.  
  279.             decimal x1D = x1 / lDiff;
  280.             decimal x2D = x2 / lDiff;
  281.             decimal y1D = y1 / lDiff;
  282.             decimal y2D = y2 / lDiff;
  283.  
  284.             decimal x1Pos = x1;
  285.             decimal x2Pos = x2;
  286.             decimal y1Pos = y1;
  287.             decimal y2Pos = y2;
  288.             for (int i = start; i < lDiff; i++)
  289.             {
  290.                 DrawBox(Convert.ToInt32(x1Pos), Convert.ToInt32(y1Pos), Convert.ToInt32(x2Pos), Convert.ToInt32(y2Pos), color);
  291.                 x1Pos += x1D;
  292.                 x2Pos -= x2D;
  293.                 y1Pos += y1D;
  294.                 y2Pos -= y2D;
  295.             }
  296.         }
  297.         public static void DrawFilledBox(int x1, int y1, int x2, int y2, int colour)
  298.         {
  299.             ConsoleColor color = (ConsoleColor)colour;
  300.             if (x1 == x2 & y1 == y2)
  301.             {
  302.                 Paintutils.DrawPixel(x1, y1, color);
  303.             }
  304.             else if (x1 == x2 || y1 == y2)
  305.             {
  306.                 DrawLine(x1, y1, x2, y2, color);
  307.             }
  308.             int minX = Math.Min(x1, x2);
  309.             int maxX = Math.Max(x1, x2);
  310.             int minY = Math.Min(y1, y2);
  311.             int maxY = Math.Max(y1, y2);
  312.  
  313.             decimal xDiff = maxX - minX;
  314.             decimal yDiff = maxY - minY;
  315.  
  316.             decimal lDiff = Math.Max(xDiff, yDiff);
  317.             int start;
  318.             if (lDiff == xDiff)
  319.             {
  320.                 start = minX;
  321.             }
  322.             else
  323.             {
  324.                 start = minY;
  325.             }
  326.  
  327.             decimal x1D = x1 / lDiff;
  328.             decimal x2D = x2 / lDiff;
  329.             decimal y1D = y1 / lDiff;
  330.             decimal y2D = y2 / lDiff;
  331.  
  332.             decimal x1Pos = x1;
  333.             decimal x2Pos = x2;
  334.             decimal y1Pos = y1;
  335.             decimal y2Pos = y2;
  336.             for (int i = start; i < lDiff; i++)
  337.             {
  338.                 DrawBox(Convert.ToInt32(x1Pos), Convert.ToInt32(y1Pos), Convert.ToInt32(x2Pos), Convert.ToInt32(y2Pos), color);
  339.                 x1Pos += x1D;
  340.                 x2Pos -= x2D;
  341.                 y1Pos += y1D;
  342.                 y2Pos -= y2D;
  343.             }
  344.         }
  345.     }
Advertisement
Add Comment
Please, Sign In to add comment