-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSet-Colors.ps1
334 lines (280 loc) · 8.75 KB
/
Set-Colors.ps1
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<#
.SYNOPSIS
Set the color theme for command line consoles or set a specific named color.
By default, updates all colors tables for Command, PowerShell, and if installed
ConEmu.
.PARAMETER Name
The name of the color table entry to set. Must be one of the known
System.ConsoleColor enumeration names. -Name and -Color are mutually exclusive
with -Theme.
.PARAMETER Color
The RGB or BGR color expressed as a six digit hex value.
The default is RGB unless the -Bgr switch is specified. -Color and -Name are
mutually exclusive with -Theme.
.PARAMETER Bgr
Indicates that the Color parameter specifies a BGR value;
the default is to specify an RGB value.
.PARAMETER Cmd
Update just the Command console color table.
.PARAMETER ConEmu
Update just the ConEmu console color table.
.PARAMETER PS
Update just the PowerShell console color table.
.PARAMETER Background
Set the specified color as the console background color.
.PARAMETER Foreground
Set the specified color as the console foreground color.
.PARAMETER Theme
Apply the specified theme file from the Themes folder; this folder must be
relative to this script and named ..\Themes\Theme_<name>.json. -Theme is
mutually exclusive with -Name and -Color.
.PARAMETER Verbose
Report each setting as it is changed; default is to run silently.
.PARAMETER WhatIf
Run the command and report changes but don't make any changes.
.DESCRIPTION
Colors for PowerShell and command windows are stored separately. Command
console colors are stored in the Registry while PowerShell colors are
stored in the shortcut link file used to start the PowerShell console.
#>
# CmdletBinding adds -Verbose functionality, SupportsShouldProcess adds -WhatIf
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[Parameter(ParameterSetName="Color", Position=0, Mandatory=$true, HelpMessage="First parameter is table entry name")]
[ValidateScript({
if ([bool]($_ -as [System.ConsoleColor] -is [System.ConsoleColor])) { $true } else {
Throw 'Name must specify a known System.ConsoleColor name'
}
})]
[string] $Name,
[parameter(ParameterSetName="Color", Position=1, Mandatory=$true, HelpMessage="Second parameter is the color hex value")]
[ValidateScript({
if ($_ -match '^(0x|#)?[A-Fa-f0-9]{1,6}$') { $true } else {
Throw 'Value must specify a color hex value of 1-6 characters '
}
})]
[string] $Color,
[Parameter(ParameterSetName="Color")]
[switch] $Bgr,
[Parameter(ParameterSetName="Color")]
[switch] $Background,
[Parameter(ParameterSetName="Color")]
[switch] $Foreground,
[Parameter(ParameterSetName="Theme", HelpMessage="Name of a theme")]
[ValidateScript({
$p = Split-Path $Script:MyInvocation.MyCommand.Path -Parent | Split-Path -Parent | Split-Path -Parent
$t = Join-Path $p "Themes\Theme_$_.json"
if (Test-Path $t) { $true } else {
Write-Host "Cannot find $t" -ForegroundColor Yellow
Throw "Theme must specify a known theme file, e.g. ..\Themes\Theme_<name>.json $p"
}
})]
[string] $Theme,
[switch] $Cmd,
[switch] $ConEmu,
[switch] $PS
)
Begin
{
function GetColorIndex ($name)
{
# correct for case-sensitivity, find index of name within color table
return [System.Enum]::GetNames([System.ConsoleColor]).indexOf(($name -as [System.ConsoleColor]).ToString())
}
function NormalizeColor ($color)
{
if ($color.StartsWith('0x', [System.StringComparison]::InvariantCultureIgnoreCase))
{
$color = $color.Substring(2)
}
elseif ($color.StartsWith('#'))
{
$color = $color.Substring(1)
}
return [System.Convert]::ToInt32($color, 16)
}
function Reverse ($value)
{
# RGB to BGR, or BGR to RGB
return (($value -band 0xFF0000) -shr 16) + ($value -band 0x00FF00) + (($value -band 0x0000FF) -shl 16)
}
function SetCommandColor ()
{
$index = GetColorIndex $Name
$entry = ("ColorTable{0:00}" -f $index)
$value = NormalizeColor $Color
if (-not $Bgr) { $value = Reverse $value }
if ($WhatIfPreference)
{
$hex = ("{0:X6}" -f $value).ToUpper()
Write-Host "CMD: Set-ItemProperty HKCU:\Console -Name $entry -Value $value (#$hex) -Type DWord" -ForegroundColor DarkGray
}
else
{
Write-Verbose "Setting command console color $Name to $value ($entry)"
Push-Location HKCU:\Console
Set-ItemProperty . -Name $entry -Value $value -Type DWord
if ($Background -or $Foreground)
{
$screen = (Get-ItemPropertyValue . 'ScreenColors')
if ($Background) { $screen = ($screen -band 0xFFFF) + ($index -shl 16) }
else { $screen = ($screen -band 0xFFFF0000) + $index }
Set-ItemProperty . -Name 'ScreenColors' -Value $screen -Type DWord -Force
}
Pop-Location
}
}
function SetPowerShellColor ()
{
$folder = "${env:APPDATA}\Microsoft\Windows\Start Menu\Programs\Windows PowerShell\"
$index = GetColorIndex $Name
$value = NormalizeColor $Color
if ($Bgr) { $value = Reverse $value }
$rgb = '#' + ("{0:X6}" -f $value).ToUpper()
# 64-bit shortcut
$linkpath = "$folder\Windows PowerShell.lnk"
if (Test-Path $linkpath)
{
$link = Get-Link -Path $linkpath
$link.ConsoleColors[$index] = $rgb
# set one or the other; don't allow both because that would be stupid
if ($Background) { $link.ScreenBackgroundColor = $index }
elseif ($Foreground) { $link.ScreenTextColor = $index }
#$lnk.PopUpBackgroundColor=0x...
#$lnk.PopUpTextColor=0x...
if ($WhatIfPreference) {
Write-Host "PWS: Update x64 LNK -Name $Name -Index $index -Value $rgb" -ForegroundColor DarkGray
}
else {
Write-Verbose "Setting PowerShell console color $Name to $value"
$link.Save()
}
}
# 32-bit shortcut
$linkpath = "$folder\Windows PowerShell (x86).lnk"
if (Test-Path $linkpath)
{
$link = Get-Link -Path $linkpath
$link.ConsoleColors[$index] = $rgb
if ($WhatIfPreference) {
Write-Host "PWS: Update x86 LNK -Name $Name -Index $index -Value $rgb" -ForegroundColor DarkGray
}
else {
Write-Verbose 'Setting PowerShell x86 console color'
$link.Save()
}
}
}
function SetConEmuColor ()
{
$file = "${env:APPDATA}\ConEmu.xml"
if (Test-Path $file)
{
$index = GetColorIndex $Name
$entry = ("ColorTable{0:00}" -f $index)
$value = NormalizeColor $Color
if (-not $Bgr) { $value = Reverse $value }
$value = ("{0:X8}" -f $value).ToUpper()
$xml = [xml](Get-Content $file)
$xml | Select-Xml -XPath "//value[@name='$entry']" | % { $_.Node.data = $value }
if ($Background) {
$xml | Select-Xml -XPath "//value[@name='BackColorIdx']" | % {
$_.Node.data = "{0:X2}" -f $index
}
}
elseif ($Foreground) {
$xml | Select-Xml -XPath "//value[@name='TextColorIdx']" | % {
$_.Node.data = "{0:X2}" -f $index
}
}
if ($WhatIfPreference) {
Write-Host "EMU: Set attribute $entry to $value" -ForegroundColor DarkGray
}
else {
Write-Verbose "Setting ConEmu console color $Name to $value"
# convert XmlDocument to XElement to output formatted XML better
$null = [Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq")
$xml = [System.Xml.Linq.XElement]::Parse($xml.OuterXml)
$xml.Save($file)
}
}
elseif ($ConEmu) # only show warning if requested ConEmu
{
Write-Host "*** ConEmu config file not found: $file" -ForegroundColor Yellow
}
}
function SetTheme ()
{
$p = Split-Path $Script:MyInvocation.MyCommand.Path -Parent | Split-Path -Parent | Split-Path -Parent
$file = Join-Path $p "Themes\Theme_$Theme.json"
$props = Get-Content $file | ConvertFrom-Json
$bg = 'Black'
if ($props.Background) { $bg = $props.Background }
$fg = 'White'
if ($props.Foreground) { $fg = $props.Foreground }
$names = [System.Enum]::GetNames([System.ConsoleColor])
0..15 | % `
{
$script:Name = $names[$_]
if ($props.$Name)
{
$script:Color = $props.$Name
$script:Background = ($bg -eq $Name)
$script:Foreground = ($fg -eq $Name)
if ($Cmd) {
SetCommandColor
}
elseif ($ConEmu) {
SetConEmuColor
}
elseif ($PS) {
SetPowerShellColor
}
else {
SetCommandColor
SetConEmuColor
SetPowerShellColor
}
}
}
if ($props.FaceName) {
Write-Verbose "Setting FaceName to '$($props.FaceName)'"
Set-ItemProperty HKCU:\Console -Name 'FaceName' -Value $props.FaceName -Force
}
if ($props.FontSize) {
$size = [System.Convert]::ToInt16($props.Fontsize, 16)
$size = "$($size.ToString('X4'))0000"
Write-Verbose "Setting FontSize to '$size'"
Set-ItemProperty HKCU:\Console -Name 'FontSize' -Value $props.FontSize -Force
}
# history=100, rows=9999
#Set-ItemProperty HKCU:\Console -Name 'HistoryBufferSize' -Value 0x64 -Force
#Set-ItemProperty HKCU:\Console -Name 'ScreenBufferSize' -Value 0x2329008c -Force
}
}
Process
{
if ($Theme)
{
SetTheme
return
}
if ($Cmd)
{
SetCommandColor
}
elseif ($ConEmu)
{
SetConEmuColor
}
elseif ($PS)
{
SetPowerShellColor
}
else
{
SetCommandColor
SetConEmuColor
SetPowerShellColor
}
}