-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathConvert-RGBToHex.ps1
166 lines (150 loc) · 5.44 KB
/
Convert-RGBToHex.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
function Convert-RGBToHex {
<#
.SYNOPSIS
Converts an RGB color string to hex equivalent
.DESCRIPTION
Converts an RGB color string to hex equivalent. Input should be in the form 'A,R,G,B'
.PARAMETER RGB
An RGB color string in the form '#,#,#,#' where each number is between 0 and 255.
.PARAMETER Prefix
A switch indicating whether hex string should be preceded by a hash symbol #.
.PARAMETER IncludeInput
A switch indicating whether to include the original decimal RGB input in the output, aliased to 'IncludeOriginal'
.NOTES
Inspired by ISEColorThemeCmdlets.ps1 Script by Jeff Pollock
http://gallery.technet.microsoft.com/ISE-Color-Theme-Cmdlets-24905f9e
Modified:
a) Changed name of parameter to RGB
b) Added -Prefix parameter to make # optional.
c) Minor tweaking so that it passes Invoke-ScriptAnalyzer
d) Added parameter validation on RGB.
e) Added .LINK entries for related items
.EXAMPLE
Convert-RGBToHex -RGB '128,64,32'
Would return
804020
.EXAMPLE
Convert-RGBToHex -RGB '255,0,0' -Prefix
Would return
#FF0000
.EXAMPLE
convert-RGBtohex '64,128,255' -Prefix
Would return
#4080FF
.EXAMPLE
'128,128,92' | convert-RGBtohex
Would return
80805C
.EXAMPLE
Convert-RGBToHex -RGB @('255,0,0','128,80,80') -Prefix -IncludeInput
Would return
DecimalRGB HexRGB
--------- -------
255,0,0 #00FF0000
128,80,80 #FF805050
.LINK
about_ISE-Color-Theme-Cmdlets
.LINK
Convert-HexToRGB
.OUTPUTS
[string]
#>
#region Parameters
[CmdletBinding(DefaultParameterSetName = 'String',ConfirmImpact = 'None')]
[OutputType('string')]
Param(
[parameter(Mandatory, Position = 0, ParameterSetName = 'String', ValueFromPipeline)]
[string[]] $RGB,
[parameter(ParameterSetName = 'Colors', ValueFromPipelineByPropertyName)]
[ValidateRange(0,255)]
[Alias('R')]
[int] $Red,
[parameter(ParameterSetName = 'Colors', ValueFromPipelineByPropertyName)]
[ValidateRange(0,255)]
[Alias('G')]
[int] $Green,
[parameter(ParameterSetName = 'Colors', ValueFromPipelineByPropertyName)]
[ValidateRange(0,255)]
[Alias('B')]
[int] $Blue,
[parameter(ParameterSetName = 'String')]
[parameter(ParameterSetName = 'Colors')]
[switch] $Prefix,
[parameter(ParameterSetName = 'String')]
[parameter(ParameterSetName = 'Colors')]
[switch] $IncludeInput
)
#endregion Parameters
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
if ($PsCmdlet.ParameterSetName -eq 'String') {
foreach ($currentRGB in $RGB) {
$currentRGB = $currentRGB -replace ' ', ''
$currentError = $false
try {
[int[]] $ColorArray = $currentRGB -split ','
} catch {
Write-Error -Message "[$currentRGB] is not a character string of 3 integers separated by a comma"
$currentError = $true
}
if ($ColorArray.count -ne 3) {
Write-Error -Message "[$currentRGB] should be 3 integers separated by a comma"
$currentError = $true
} else {
if ($ColorArray[0] -lt 0 -or $ColorArray[0] -gt 255) {
Write-Error -Message 'Red should be between 0 and 255'
$currentError = $true
}
if ($ColorArray[1] -lt 0 -or $ColorArray[1] -gt 255) {
Write-Error -Message 'Green should be between 0 and 255'
$currentError = $true
}
if ($ColorArray[2] -lt 0 -or $ColorArray[2] -gt 255) {
Write-Error -Message 'Blue should be between 0 and 255'
$currentError = $true
}
if (-not $currentError) {
$RedHex = '{0:X2}' -f ($ColorArray[0])
$GreenHex = '{0:X2}' -f ($ColorArray[1])
$BlueHex = '{0:X2}' -f ($ColorArray[2])
$Hex = $RedHex + $GreenHex + $BlueHex
if ($Prefix) {
$Hex = '#' + $Hex
}
if ($IncludeInput) {
New-Object -TypeName psobject -Property ([ordered] @{
RGB = $currentRGB
Hex = $Hex
})
} else {
Write-Output -InputObject $Hex
}
}
}
}
} else {
$RedHex = '{0:X2}' -f $Red
$GreenHex = '{0:X2}' -f $Green
$BlueHex = '{0:X2}' -f $Blue
$Hex = $RedHex + $GreenHex + $BlueHex
if ($Prefix) {
$Hex = '#' + $Hex
}
if ($IncludeInput) {
New-Object -TypeName psobject -Property ([ordered] @{
Red = $Red
Green = $Green
Blue = $Blue
Hex = $Hex
})
} else {
Write-Output -InputObject $Hex
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}