-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathholi.cs
More file actions
163 lines (123 loc) · 4.29 KB
/
holi.cs
File metadata and controls
163 lines (123 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//Holi.cs
using System.Linq;
using System.Runtime.InteropServices;
using System;
using System.IO;
using BepInEx;
public static class Holi
{
const int STD_OUTPUT_HANDLE = -11;
const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
public const string RESET = "\x1B[0m";
public const string UNDERLINE = "\x1B[4m";
public const string BOLD = "\x1B[1m";
public const string ITALIC = "\x1B[3m";
public const string BLINK = "\x1B[5m";
public const string BLINKRAPID = "\x1B[6m";
public const string DEFAULTFORE = "\x1B[39m";
public const string DEFAULTBACK = "\x1B[49m";
static Holi()
{
var handle = GetStdHandle(STD_OUTPUT_HANDLE);
uint mode;
GetConsoleMode(handle, out mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(handle, mode);
}
public static byte[] HexToRgb(string hexcolor)
{
hexcolor = hexcolor.Remove(0, 1);
if (hexcolor.Length != 6)
throw new Exception("Not a valid hex color");
string[] rgb = hexcolor.Select((obj, index) => new { obj, index })
.GroupBy(o => o.index / 2)
.Select(g => new string(g.Select(a => a.obj)
.ToArray())).ToArray<string>();
return new byte[] { Convert.ToByte(rgb[0], 16), Convert.ToByte(rgb[1], 16), Convert.ToByte(rgb[2], 16) };
}
public static string ForeColor(this string text, byte red, byte green, byte blue)
{
return $"\x1B[38;2;{red};{green};{blue}m{text}";
}
public static string ForeColor(this string text, string hexrgb)
{
byte[] rgb = HexToRgb(hexrgb);
return ForeColor(text, rgb[0], rgb[1], rgb[2]);
}
public static string ForeColor(this string text, int id)
{
return $"\u001b[38;5;{id}m{text}\u001b[0m";
}
public static string BackColor(this string text, byte red, byte green, byte blue)
{
return $"\x1B[48;2;{red};{green};{blue}m{text}";
}
public static string BackColor(this string text, string hexrgb)
{
byte[] rgb = HexToRgb(hexrgb);
return BackColor(text, rgb[0], rgb[1], rgb[2]);
}
public static string ResetColor(this string text)
{
return $"{RESET}{text}";
}
public static string Bold(this string text)
{
return $"{BOLD}{text}";
}
public static string Italic(this string text)
{
return $"{ITALIC}{text}";
}
public static string Underline(this string text)
{
return $"{UNDERLINE}{text}";
}
public static string Add(this string text, string addText)
{
return $"{text}{RESET}{addText}";
}
public static void Print(this string text)
{
ConsoleManager.ConsoleStream?.Write($"{text}{RESET}");
}
public static void Printf(this string text, byte red = 0, byte green = 0, byte blue = 0)
{
ConsoleManager.ConsoleStream?.Write($"{ForeColor(red, green, blue)}{text}{RESET}");
}
public static void Printf(this string text, string hexColor)
{
byte[] rgb = HexToRgb(hexColor);
ConsoleManager.ConsoleStream?.Write($"{ForeColor(rgb[0], rgb[1], rgb[2])}{text}{RESET}");
}
public static string ForeColor(params byte[] rgb)
{
if (rgb == null || rgb.Length == 0)
return "\x1B[0m";
if (rgb.Length == 3)
return $"\x1B[38;2;{rgb[0]};{rgb[1]};{rgb[2]}m";
if (rgb.Length == 2)
return $"\x1B[38;2;{rgb[0]};{rgb[1]};0m";
if (rgb.Length == 2)
return $"\x1B[38;2;{rgb[0]};0;0m";
return "\x1B[0m";
}
public static string BackColor(params byte[] rgb)
{
if (rgb == null || rgb.Length == 0)
return "\x1B[0m";
if (rgb.Length == 3)
return $"\x1B[48;2;{rgb[0]};{rgb[1]};{rgb[2]}m";
if (rgb.Length == 2)
return $"\x1B[48;2;{rgb[0]};{rgb[1]};0m";
if (rgb.Length == 2)
return $"\x1B[48;2;{rgb[0]};0;0m";
return "\x1B[0m";
}
}