-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnsiColor.ps1
More file actions
135 lines (108 loc) · 5.27 KB
/
Copy pathAnsiColor.ps1
File metadata and controls
135 lines (108 loc) · 5.27 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
#!/usr/bin/pwsh
<#
This file is part of helpimnotdrowning's PwshUtils.
PwshUtils is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
PwshUtils is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
PwshUtils. If not, see <https://www.gnu.org/licenses/>.
#>
class AnsiEscapeCodes {
static [String] $Reset = $PSStyle.Reset
static [String] $Blink = $PSStyle.Blink
static [String] $BlinkOff = $PSStyle.BlinkOff
static [String] $Bold = $PSStyle.Bold
static [String] $BoldOff = $PSStyle.BoldOdd
static [String] $Dim = $PSStyle.Dim
static [String] $DimOff = $PSStyle.DimOff
static [String] $Hidden = $PSStyle.Hidden
static [String] $HiddenOff = $PSStyle.HiddenOff
static [String] $Reverse = $PSStyle.Reverse
static [String] $ReverseOff = $PSStyle.ReverseOff
static [String] $Italic = $PSStyle.Italic
static [String] $ItalicOff = $PSStyle.ItalicOff
static [String] $Underline = $PSStyle.Underline
static [String] $UnderlineOff = $PSStyle.UnderlineOff
static [String] $Strikethrough = $PSStyle.Strikethrough
static [String] $StrikethroughOff = $PSStyle.StrikethroughOff
static [String] $FBlack = $PSStyle.Foreground.Black
static [String] $FBrightBlack = $PSStyle.Foreground.BrightBlack
static [String] $FWhite = $PSStyle.Foreground.White
static [String] $FBrightWhite = $PSStyle.Foreground.BrightWhite
static [String] $FRed = $PSStyle.Foreground.Red
static [String] $FBrightRed = $PSStyle.Foreground.BrightRed
static [String] $FMagenta = $PSStyle.Foreground.Magenta
static [String] $FBrightMagenta = $PSStyle.Foreground.BrightMagenta
static [String] $FBlue = $PSStyle.Foreground.Blue
static [String] $FBrightBlue = $PSStyle.Foreground.BrightBlue
static [String] $FCyan = $PSStyle.Foreground.Cyan
static [String] $FBrightCyan = $PSStyle.Foreground.BrightCyan
static [String] $FGreen = $PSStyle.Foreground.Green
static [String] $FBrightGreen = $PSStyle.Foreground.BrightGreen
static [String] $FYellow = $PSStyle.Foreground.Yellow
static [String] $FBrightYellow = $PSStyle.Foreground.BrightYellow
static [String] $BBlack = $PSStyle.Background.Black
static [String] $BBrightBlack = $PSStyle.Background.BrightBlack
static [String] $BWhite = $PSStyle.Background.White
static [String] $BBrightWhite = $PSStyle.Background.BrightWhite
static [String] $BRed = $PSStyle.Background.Red
static [String] $BBrightRed = $PSStyle.Background.BrightRed
static [String] $BMagenta = $PSStyle.Background.Magenta
static [String] $BBrightMagenta = $PSStyle.Background.BrightMagenta
static [String] $BBlue = $PSStyle.Background.Blue
static [String] $BBrightBlue = $PSStyle.Background.BrightBlue
static [String] $BCyan = $PSStyle.Background.Cyan
static [String] $BBrightCyan = $PSStyle.Background.BrightCyan
static [String] $BGreen = $PSStyle.Background.Green
static [String] $BBrightGreen = $PSStyle.Background.BrightGreen
static [String] $BYellow = $PSStyle.Background.Yellow
static [String] $BBrightYellow = $PSStyle.Background.BrightYellow
}
function ConvertTo-AnsiColorCode {
param (
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName="StringColor")]
[ValidateLength(6, 8)]
[ValidatePattern("(#|0x)?[0-9a-fA-F]{6}")] # MATCH (optional '#' || '0x') + (ANY 6 chars IN (0..9 || a..f || A..F))
[String] $ColorString,
[Parameter(Mandatory, ParameterSetName="TripletByteColor")]
[ValidateNotNull()]
[Byte] $Red,
[Parameter(Mandatory, ParameterSetName="TripletByteColor")]
[ValidateNotNull()]
[Byte] $Blue,
[Parameter(Mandatory, ParameterSetName="TripletByteColor")]
[ValidateNotNull()]
[Byte] $Green,
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName="IntColor")]
[ValidateNotNull()]
[Int] $Color,
[ValidateSet("Foreground", "Background")]
[String] $Type = "Foreground"
)
if ($ColorString) {
# strip '#' from color; its allowed for convenience but we don't actually want that
if ($ColorString[0] -eq "#") {
$Color = [Int]"0x$($ColorString[1..6] -join '')"
# already in 0x form :D
} elseif ($ColorString[1] -eq 'x') {
$Color = [Int]$ColorString
# just a pile of characters, add 0x prefix
} else {
$Color = [Int]"0x$ColorString"
}
} elseif ($Color) {
#
} else {
# add together channels to form full color
$Color = ($Red -shl 16) + ($Green -shl 8) + ($Blue)
}
write-host $color
switch ($Type) {
"Foreground" { return $PSStyle.Foreground.FromRgb($Color) }
"Background" { return $PSStyle.Background.FromRgb($Color) }
}
}