Functions/New-ColorPicker.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
function New-ColorPicker {
    <#
.SYNOPSIS
    Present a dialog to the user and allow them to select or define a color. User can choose how the color will be returned.
.DESCRIPTION
    Present a dialog to the user and allow them to select or define a color. User can choose how the color will be returned.
.PARAMETER R
    The Red color value. Defaults to '0'. Valid range 0-255. Can be passed from the pipeline by property name.
.PARAMETER G
    The Green color value. Defaults to '0'. Valid range 0-255. Can be passed from the pipeline by property name.
.PARAMETER B
    The Blue color value. Defaults to '0'. Valid range 0-255. Can be passed from the pipeline by property name.
.PARAMETER AsHashTable
    Switch that will present the results of 'R,G,B' as a hashtable as opposed to a psobject
.EXAMPLE
    New-ColorPicker -R 20 -G 45 -B 255
 
    Will present the user with a color dialog pre-populated with 23, 45, 255
.EXAMPLE
    New-ColorPicker -verbose
 
    VERBOSE: Starting [New-ColorPicker]
    VERBOSE: R [0] G [0] B [0]
 
    VERBOSE: Ending [New-ColorPicker]
    R G B
    - - -
    0 128 192
 
    Assuming that they entered the color 0, 128, 192 and selected 'Rgb' as the output type.
.NOTES
    The user can select how the result will be returned to them:
 
    * Hex - where the color will be returned as 6 hexadecimal characters in the form: 'RRGGBB'
 
    * RgbString - where the color will be returned as 3 integers separated by a comman 'R,G,B'
 
    * Rgb - where the color will returned as a psobject with 3 properties: R, G, B
 
    If the user selects 'Cancel' from the dialog box no value is returned.
#>


    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseOutputTypeCorrectly', '')]
    param (
        [Parameter(Position = 0, ValueFromPipelineByPropertyName)]
        [ValidateRange(0, 255)]
        [Alias('Red')]
        [int] $R = 0,

        [Parameter(Position = 1, ValueFromPipelineByPropertyName)]
        [ValidateRange(0, 255)]
        [Alias('Green')]
        [int] $G = 0,

        [Parameter(Position = 2, ValueFromPipelineByPropertyName)]
        [ValidateRange(0, 255)]
        [Alias('Blue')]
        [int] $B = 0,

        [switch] $AsHashTable
    )

    begin {
        Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
        #region Form Creation
        #Warning: It is recommended that changes inside this region be handled using the ScriptForm Designer.
        #When working with the ScriptForm designer this region and any changes within may be overwritten.
        #~~< ColorPicker >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $ColorPicker = New-Object -TypeName System.Windows.Forms.Form
        $ColorPicker.ClientSize = New-Object -TypeName System.Drawing.Size -ArgumentList (420, 400)
        $ColorPicker.Text = 'Select color'
        #~~< OKButton >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $OKButton = New-Object -TypeName System.Windows.Forms.Button
        $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
        $OKButton.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (300, 73)
        $OKButton.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (100, 23)
        $OKButton.TabIndex = 5
        $OKButton.Text = 'OK'
        $OKButton.UseVisualStyleBackColor = $true
        $ColorPicker.AcceptButton = $OKButton
        #~~< CancelButton >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $CancelButton = New-Object -TypeName System.Windows.Forms.Button
        $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
        $CancelButton.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (300, 131)
        $CancelButton.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (100, 23)
        $CancelButton.TabIndex = 6
        $CancelButton.Text = 'Cancel'
        $CancelButton.UseVisualStyleBackColor = $true
        $ColorPicker.CancelButton = $CancelButton
        #~~< AsHashCheck >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $AsHashCheck = New-Object -TypeName System.Windows.Forms.CheckBox
        $AsHashCheck.Enabled = $false
        $AsHashCheck.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (180, 100)
        $AsHashCheck.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (104, 24)
        $AsHashCheck.TabIndex = 11
        $AsHashCheck.Text = 'As hashtable'
        $AsHashCheck.UseVisualStyleBackColor = $true
        #~~< PictureBox >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $PictureBox = New-Object -TypeName System.Windows.Forms.PictureBox
        $PictureBox.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
        $PictureBox.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (232, 289)
        $PictureBox.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (75, 75)
        $PictureBox.TabIndex = 10
        $PictureBox.TabStop = $false
        $PictureBox.Text = ''
        $PictureBox.BackColor = [System.Drawing.Color]::FromArgb(([System.Int32](([System.Byte](224)))), ([System.Int32](([System.Byte](224)))), ([System.Int32](([System.Byte](224)))))
        #~~< ResultLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $ResultLabel = New-Object -TypeName System.Windows.Forms.Label
        $ResultLabel.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (12, 294)
        $ResultLabel.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (197, 23)
        $ResultLabel.TabIndex = 9
        $ResultLabel.Text = 'Resulting color expression'
        #~~< ColorTextBox >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $ColorTextBox = New-Object -TypeName System.Windows.Forms.TextBox
        $ColorTextBox.Enabled = $false
        $ColorTextBox.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (13, 320)
        $ColorTextBox.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (199, 20)
        $ColorTextBox.TabIndex = 8
        $ColorTextBox.Text = ''
        #~~< DescriptionLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $DescriptionLabel = New-Object -TypeName System.Windows.Forms.Label
        $DescriptionLabel.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (12, 13)
        $DescriptionLabel.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (200, 23)
        $DescriptionLabel.TabIndex = 5
        $DescriptionLabel.Text = 'Please select the color result type'
        #~~< ColorButton >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $ColorButton = New-Object -TypeName System.Windows.Forms.Button
        $ColorButton.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (300, 19)
        $ColorButton.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (100, 23)
        $ColorButton.TabIndex = 0
        $ColorButton.Text = 'Color Dialog'
        $ColorButton.UseVisualStyleBackColor = $true
        #~~< ColorResult >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $ColorResult = New-Object -TypeName System.Windows.Forms.GroupBox
        $ColorResult.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (13, 39)
        $ColorResult.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (149, 203)
        $ColorResult.TabIndex = 4
        $ColorResult.TabStop = $false
        $ColorResult.Text = 'Select Color Result'
        #~~< HexRb >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $HexRb = New-Object -TypeName System.Windows.Forms.RadioButton
        $HexRb.Checked = $true
        $HexRb.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (15, 30)
        $HexRb.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (104, 24)
        $HexRb.TabIndex = 1
        $HexRb.TabStop = $true
        $HexRb.Text = 'Hex'
        $HexRb.UseVisualStyleBackColor = $true
        #~~< RgbStringRB >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $RgbStringRB = New-Object -TypeName System.Windows.Forms.RadioButton
        $RgbStringRB.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (15, 110)
        $RgbStringRB.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (104, 24)
        $RgbStringRB.TabIndex = 3
        $RgbStringRB.Text = 'RGB String'
        $RgbStringRB.UseVisualStyleBackColor = $true
        #~~< ColorObjectRb >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $ColorObjectRb = New-Object -TypeName System.Windows.Forms.RadioButton
        $ColorObjectRb.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (15, 150)
        $ColorObjectRb.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (104, 24)
        $ColorObjectRb.TabIndex = 5
        $ColorObjectRb.TabStop = $true
        $ColorObjectRb.Text = 'Color object'
        $ColorObjectRb.UseVisualStyleBackColor = $true
        #~~< RgbRb >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $RgbRb = New-Object -TypeName System.Windows.Forms.RadioButton
        $RgbRb.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (15, 70)
        $RgbRb.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (104, 24)
        $RgbRb.TabIndex = 2
        $RgbRb.Text = 'R,G,B'
        $RgbRb.UseVisualStyleBackColor = $true
        $ColorResult.Controls.Add($HexRb)
        $ColorResult.Controls.Add($RgbStringRB)
        $ColorResult.Controls.Add($ColorObjectRb)
        $ColorResult.Controls.Add($RgbRb)
        $ColorPicker.Controls.Add($AsHashCheck)
        $ColorPicker.Controls.Add($PictureBox)
        $ColorPicker.Controls.Add($ResultLabel)
        $ColorPicker.Controls.Add($ColorTextBox)
        $ColorPicker.Controls.Add($CancelButton)
        $ColorPicker.Controls.Add($OKButton)
        $ColorPicker.Controls.Add($DescriptionLabel)
        $ColorPicker.Controls.Add($ColorButton)
        $ColorPicker.Controls.Add($ColorResult)
        #~~< ColorDialog >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $ColorDialog = New-Object -TypeName System.Windows.Forms.ColorDialog
        $ColorDialog.ShowHelp = $true

        #endregion

        #region myCustomFormCode
        $ColorPicker.AutoSize = $true
        $ColorPicker.AutoSizeMode = 'GrowOnly'
        $ColorPicker.Font = [System.Drawing.SystemFonts]::get_MessageBoxFont()
        if ($Script:ModulePath) {
            $ColorPicker.Icon = (Join-Path -Path $Script:ModulePath -ChildPath 'Resources\PoshFunctions.ico')
        }
        $ColorPicker.add_Load( { $ColorTextBox.Text = '{0:X2}{1:X2}{2:X2}' -f $R, $G, $B })
        $ColorButton.add_Click( { Select-ColorButton -R ([ref] $R) -G ([ref] $G) -B ([ref] $B) })
        $RgbStringRb.add_Click( { Show-Result -object ($RgbStringRb) })
        $ColorObjectRb.add_Click( { Show-Result -object ($ColorObjectRb) })
        $RgbRb.add_Click( { Show-Result -object ($RgbRb) })
        $HexRb.add_Click( { Show-Result -object ($HexRb) })
        $AsHashCheck.Checked = $AsHashTable
        #endregion

        function Show-Result {
            [CmdletBinding()]
            [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
            param (
                $object
            )
            if ($HexRb.Checked) {
                $AsHashCheck.Enabled = $false
                $ColorTextBox.Text = '{0:X2}{1:X2}{2:X2}' -f $R, $G, $B
            } elseif ($RgbRb.Checked) {
                $AsHashCheck.Enabled = $true
                $ColorTextBox.Text = 'R={0}, G={1}, B={2}' -f $R, $G, $B
            } elseif ($RgbStringRB.Checked) {
                $AsHashCheck.Enabled = $false
                $ColorTextBox.Text = '{0},{1},{2}' -f $R, $G, $B
            } else {
                $AsHashCheck.Enabled = $false
                $ColorTextBox.Text = 'Color object'
            }
        }

        function Select-ColorButton {
            [CmdletBinding()]
            param
            (
                [ref] $R,

                [ref] $G,

                [ref] $B
            )
            $ColorDialog.Color = [System.Drawing.Color]::FromArgb($R.Value, $G.Value, $B.Value)
            $ColorResult = $ColorDialog.ShowDialog()
            if ($ColorResult -eq 'OK') {
                $R.Value = $ColorDialog.Color.R
                $G.Value = $ColorDialog.Color.G
                $B.Value = $ColorDialog.Color.B
                $PictureBox.BackColor = [System.Drawing.Color]::FromArgb($R.Value, $G.Value, $B.Value)
                if ($HexRb.Checked) {
                    $ColorTextBox.Text = '{0:X2}{1:X2}{2:X2}' -f $R.Value, $G.Value, $B.Value
                } elseif ($RgbRb.Checked) {
                    $ColorTextBox.Text = 'R={0}, G={1}, B={2}' -f $R.Value, $G.Value, $B.Value
                } elseif ($RgbStringRB.Checked) {
                    $ColorTextBox.Text = '{0},{1},{2}' -f $R.Value, $G.Value, $B.Value
                } else {
                    $ColorTextBox.Text = 'Color Object'
                }
            }
        }
    }

    process {
        Write-Verbose -Message "R [$R] G [$G] B [$B] AsHashTable [$AsHashTable]"
        [System.Windows.Forms.Application]::EnableVisualStyles()
        $PictureBox.BackColor = [System.Drawing.Color]::FromArgb($R, $G, $B)
        $Result = $ColorPicker.ShowDialog()
        if ($Result -eq 'OK') {
            if ($HexRb.Checked) {
                Write-Output -InputObject ('{0:X2}{1:X2}{2:X2}' -f $R, $G, $B)
            } elseif ($RgbRb.Checked) {
                if ($AsHashCheck.Checked) {
                    ([ordered] @{
                            R = $R
                            G = $G
                            B = $B
                        })
                } else {
                    New-Object -TypeName psobject -Property ([ordered] @{
                            R = $R
                            G = $G
                            B = $B
                        })
                }
            } elseif ($RgbStringRB.Checked) {
                Write-Output -InputObject ('{0},{1},{2}' -f $R, $G, $B)
            } else {
                Write-Output -InputObject $ColorDialog.Color
            }
        }
    }

    end {
        Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
    }
}