Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class Paintutils
- {
- public static ConsoleColor ToNearestConsoleColor(this Color color)
- {
- ConsoleColor ClosestColor = 0;
- double delta = double.MaxValue;
- foreach (ConsoleColor consoleColor in Enum.GetValues(typeof(ConsoleColor)))
- {
- string ConsoleColorName = Enum.GetName(typeof(ConsoleColor), consoleColor);
- ConsoleColorName = string.Equals(ConsoleColorName, nameof(ConsoleColor.DarkYellow), StringComparison.Ordinal) ? nameof(Color.Orange) : ConsoleColorName;
- Color rgbColor = Color.FromName(ConsoleColorName);
- double sum = Math.Pow(rgbColor.R - color.R, 2) + Math.Pow(rgbColor.G + color.G, 2) + Math.Pow(rgbColor.B + color.B, 2);
- double epsilon = 2.0;
- if (sum < epsilon)
- {
- return consoleColor;
- }
- if (sum < delta)
- {
- delta = sum;
- ClosestColor = consoleColor;
- }
- }
- return ClosestColor;
- }
- public static Color CreateColor(int R, int G, int B)
- {
- Color newColor = Color.FromArgb(R, G, B);
- return newColor;
- }
- public static void SetBackground(ConsoleColor color)
- {
- Console.BackgroundColor = color;
- Console.Clear();
- }
- public static void DrawPixel(int x, int y, ConsoleColor color)
- {
- if (x < 0 || x > Console.WindowWidth - 1 || y < 0 || y > Console.WindowHeight - 1)
- {
- throw new ArgumentException("Position Out Of Bounds");
- }
- Console.BackgroundColor = color;
- Console.SetCursorPosition(x, y);
- Console.Write(" ");
- }
- public static void DrawPixel(int x, int y, int color)
- {
- if (x < 0 || x > Console.WindowWidth - 1 || y < 0 || y > Console.WindowHeight - 1)
- {
- throw new ArgumentException("Position Out Of Bounds");
- }
- Console.BackgroundColor = (ConsoleColor)color;
- Console.SetCursorPosition(x, y);
- Console.Write(" ");
- }
- public static List<int> LoadImage(string path)
- {
- string line = "";
- List<string> data = new List<string>();
- List<string> split = new List<string>();
- List<int> lImage = new List<int>();
- if (!File.Exists(path))
- {
- throw new System.ArgumentException("Path does not exist");
- }
- using (StreamReader fs = new StreamReader(path))
- {
- line = fs.ReadLine();
- while (line != null)
- {
- data.Add(line);
- data.Add("|");
- line = fs.ReadLine();
- }
- }
- foreach (string ln in data)
- {
- try
- {
- if (ln == "|")
- {
- split.Add("|");
- } else
- {
- foreach (char c in ln)
- {
- split.Add(Convert.ToString(c));
- }
- }
- } catch {}
- }
- foreach (string c in split)
- {
- if (c != null)
- {
- switch (c)
- {
- case " ":
- lImage.Add(16);
- break;
- case "|":
- lImage.Add(17);
- break;
- default:
- lImage.Add(Convert.ToInt32(c, 16));
- break;
- }
- }
- }
- return lImage;
- }
- public static void DrawImage(List<int> image, int x, int y)
- {
- int lines = 0;
- int pos = 0;
- foreach (int code in image)
- {
- switch (code)
- {
- case 16:
- pos++;
- break;
- case 17:
- pos = 0;
- lines++;
- break;
- default:
- Paintutils.DrawPixel(x + pos, y + lines, code);
- pos++;
- break;
- }
- }
- }
- public static void DrawLine(int x1, int y1, int x2, int y2, ConsoleColor color)
- {
- if (x1 == x2 & y1 == y2)
- {
- Paintutils.DrawPixel(x1, y2, color);
- return;
- }
- int minX = Math.Min(x1, x2);
- int maxX = Math.Max(x1, x2);
- int minY = Math.Min(y1, y2);
- int maxY = Math.Max(y1, y2);
- decimal xDiff = maxX - minX;
- decimal yDiff = maxY - minY;
- if (xDiff > yDiff)
- {
- decimal y = minY;
- decimal dyy = (yDiff / xDiff);
- for (int x = minX; x < maxX; x++)
- {
- Paintutils.DrawPixel(x, Convert.ToInt32(y), color);
- y += dyy;
- }
- } else
- {
- decimal x = minX;
- decimal dxx = xDiff / yDiff;
- for (int y = minY;y < maxY;y++)
- {
- Paintutils.DrawPixel(Convert.ToInt32(x), y, color);
- x += dxx;
- }
- }
- }
- public static void DrawLine(int x1, int y1, int x2, int y2, int colour)
- {
- ConsoleColor color = (ConsoleColor)colour;
- if (x1 == x2 & y1 == y2)
- {
- Paintutils.DrawPixel(x1, y2, color);
- return;
- }
- int minX = Math.Min(x1, x2);
- int maxX = Math.Max(x1, x2);
- int minY = Math.Min(y1, y2);
- int maxY = Math.Max(y1, y2);
- decimal xDiff = maxX - minX;
- decimal yDiff = maxY - minY;
- if (xDiff > yDiff)
- {
- decimal y = minY;
- decimal dyy = (yDiff / xDiff);
- for (int x = minX; x < maxX; x++)
- {
- Paintutils.DrawPixel(x, Convert.ToInt32(y), color);
- y += dyy;
- }
- }
- else
- {
- decimal x = minX;
- decimal dxx = xDiff / yDiff;
- for (int y = minY; y < maxY; y++)
- {
- Paintutils.DrawPixel(Convert.ToInt32(x), y, color);
- x += dxx;
- }
- }
- }
- public static void DrawBox(int x1, int y1, int x2, int y2, ConsoleColor color)
- {
- if (x1 == x2 & y1 == y2)
- {
- Paintutils.DrawPixel(x1, y1, color);
- } else if(x1 == x2 || y1 == y2)
- {
- DrawLine(x1, y1, x2, y2,color);
- }
- Paintutils.DrawLine(x1, y1, x2, y1, color);
- Paintutils.DrawLine(x1, y1+1, x1, y2+1, color);
- Paintutils.DrawLine(x2, y2, x1+1, y2, color);
- Paintutils.DrawLine(x2, y2+1, x2, y1, color);
- }
- public static void DrawBox(int x1, int y1, int x2, int y2, int colour)
- {
- ConsoleColor color = (ConsoleColor)colour;
- if (x1 == x2 & y1 == y2)
- {
- Paintutils.DrawPixel(x1, y1, color);
- }
- else if (x1 == x2 || y1 == y2)
- {
- DrawLine(x1, y1, x2, y2, color);
- }
- Paintutils.DrawLine(x1, y1, x2, y1, color);
- Paintutils.DrawLine(x1, y1 + 1, x1, y2 + 1, color);
- Paintutils.DrawLine(x2, y2, x1 + 1, y2, color);
- Paintutils.DrawLine(x2, y2 + 1, x2, y1, color);
- }
- public static void DrawFilledBox(int x1, int y1, int x2, int y2, ConsoleColor color)
- {
- if (x1 == x2 & y1 == y2)
- {
- Paintutils.DrawPixel(x1, y1, color);
- }
- else if (x1 == x2 || y1 == y2)
- {
- DrawLine(x1, y1, x2, y2, color);
- }
- int minX = Math.Min(x1, x2);
- int maxX = Math.Max(x1, x2);
- int minY = Math.Min(y1, y2);
- int maxY = Math.Max(y1, y2);
- decimal xDiff = maxX - minX;
- decimal yDiff = maxY - minY;
- decimal lDiff = Math.Max(xDiff, yDiff);
- int start;
- if (lDiff == xDiff)
- {
- start = minX;
- } else
- {
- start = minY;
- }
- decimal x1D = x1 / lDiff;
- decimal x2D = x2 / lDiff;
- decimal y1D = y1 / lDiff;
- decimal y2D = y2 / lDiff;
- decimal x1Pos = x1;
- decimal x2Pos = x2;
- decimal y1Pos = y1;
- decimal y2Pos = y2;
- for (int i = start; i < lDiff; i++)
- {
- DrawBox(Convert.ToInt32(x1Pos), Convert.ToInt32(y1Pos), Convert.ToInt32(x2Pos), Convert.ToInt32(y2Pos), color);
- x1Pos += x1D;
- x2Pos -= x2D;
- y1Pos += y1D;
- y2Pos -= y2D;
- }
- }
- public static void DrawFilledBox(int x1, int y1, int x2, int y2, int colour)
- {
- ConsoleColor color = (ConsoleColor)colour;
- if (x1 == x2 & y1 == y2)
- {
- Paintutils.DrawPixel(x1, y1, color);
- }
- else if (x1 == x2 || y1 == y2)
- {
- DrawLine(x1, y1, x2, y2, color);
- }
- int minX = Math.Min(x1, x2);
- int maxX = Math.Max(x1, x2);
- int minY = Math.Min(y1, y2);
- int maxY = Math.Max(y1, y2);
- decimal xDiff = maxX - minX;
- decimal yDiff = maxY - minY;
- decimal lDiff = Math.Max(xDiff, yDiff);
- int start;
- if (lDiff == xDiff)
- {
- start = minX;
- }
- else
- {
- start = minY;
- }
- decimal x1D = x1 / lDiff;
- decimal x2D = x2 / lDiff;
- decimal y1D = y1 / lDiff;
- decimal y2D = y2 / lDiff;
- decimal x1Pos = x1;
- decimal x2Pos = x2;
- decimal y1Pos = y1;
- decimal y2Pos = y2;
- for (int i = start; i < lDiff; i++)
- {
- DrawBox(Convert.ToInt32(x1Pos), Convert.ToInt32(y1Pos), Convert.ToInt32(x2Pos), Convert.ToInt32(y2Pos), color);
- x1Pos += x1D;
- x2Pos -= x2D;
- y1Pos += y1D;
- y2Pos -= y2D;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment