-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertFrom-ItermColoursToHex.psm1
98 lines (80 loc) · 2.68 KB
/
ConvertFrom-ItermColoursToHex.psm1
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
$Script:colourNames = @{
'Ansi 0 Color' = 'Black'
'Ansi 1 Color' = 'Red'
'Ansi 2 Color' = 'Green'
'Ansi 3 Color' = 'Yellow'
'Ansi 4 Color' = 'Blue'
'Ansi 5 Color' = 'Magenta'
'Ansi 6 Color' = 'Cyan'
'Ansi 7 Color' = 'White'
'Ansi 8 Color' = 'Bright Black'
'Ansi 9 Color' = 'Bright Red'
'Ansi 10 Color' = 'Bright Green'
'Ansi 11 Color' = 'Bright Yellow'
'Ansi 12 Color' = 'Bright Blue'
'Ansi 13 Color' = 'Bright Magenta'
'Ansi 14 Color' = 'Bright Cyan'
'Ansi 15 Color' = 'Bright White'
'Background Color' = 'Background'
'Foreground Color' = 'Text'
'Bold Color' = 'Bold Text'
'Selection Color' = 'Selection Background'
'Selected Text Color' = 'Selected Foregrond'
'Cursor Color' = 'Cursor Background'
'Cursor Text Color' = 'Cursor Foreground'
}
function ConvertFrom-ItermColoursToHex {
<#
.SYNOPSIS
Converts an SRGB iTerm colour scheme to hex RGB values
.DESCRIPTION
Converts an SRGB iTerm colour scheme to hex RGB values
.PARAMETER InputXml
The content of the colour scheme as an XmlDocument
.INPUTS
XmlDocument
.OUTPUTS
PSCustomObject
.EXAMPLE
ConvertFrom-ItermColoursToHex -InputXml [xml](Get-Content '.\colourscheme.itermcolors)
.EXAMPLE
[xml](Get-Content '.\colourscheme.itermcolors) | ConvertFrom-ItermColoursToHex
.LINK
https://github.com/charliebillen/ConvertItermColoursToHex
#>
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[xml]
$InputXml
)
process {
foreach ($key in $InputXml.GetElementsByTagName('key')) {
$mappedName = $Script:colourNames[$key.InnerText]
if (-not $mappedName) {
continue
}
[pscustomobject]@{
Name = $mappedName
Colour = Get-HexFromNode $key.NextSibling
}
}
}
}
function Get-HexFromNode($colourNode) {
[int]$r = 0; [int]$g = 0; [int]$b = 0
$realToByteValue = { param($real) 255 * [double]$real }
foreach ($node in $colourNode.ChildNodes) {
if ($node.InnerText -match 'red') {
$r = &$realToByteValue $node.NextSibling.InnerText
}
if ($node.InnerText -match 'green') {
$g = &$realToByteValue $node.NextSibling.InnerText
}
if ($node.InnerText -match 'blue') {
$b = &$realToByteValue $node.NextSibling.InnerText
}
}
"#{0:X2}{1:X2}{2:X2}" -f $r, $g, $b
}
Export-ModuleMember -Function ConvertFrom-ItermColoursToHex