You can allow for color selection in your PowerShell GUI with the System.Windows.Forms.ColorDialog. You can have access to select from millions of colors. The ColorDialog allows selection of 48 Basic Colors or expand the dialog and select from any custom color, depending on your system.

Colors

In a WinForms form I added Labels and TextBoxes for R,G,B,A and the Color Name.

Add-Type -AssemblyName System.Windows.Forms

# Main Form
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Font = "Comic Sans MS,10"
$mainForm.Text = " Color Dialog Demo"
$mainForm.ForeColor = "White"
$mainForm.BackColor = "DarkBlue"
$mainForm.Width = 450
$mainForm.Height = 330

# Red Label
$redLabel = New-Object System.Windows.Forms.Label
$redLabel.Location = "45,50"
$redLabel.Height = 22
$redLabel.Width = 100
$redLabel.Text = "Red"
$mainForm.Controls.Add($redLabel)

# Red TextBox
$redTextBox = New-Object System.Windows.Forms.TextBox
$redTextBox.Location = "180,50"
$redTextBox.Size = "100,20"
$redTextBox.ForeColor = "MediumBlue"
$redTextBox.BackColor = "White"
$mainForm.Controls.Add($redTextBox)

I added a TextBox to display some sample text in a large font which I’ll change the color of to match the color selected.

# Color Demo TextBox
$colorDemoTextBox = New-Object System.Windows.Forms.TextBox
$colorDemoTextBox.Font = "Comic Sans MS,28"
$colorDemoTextBox.Location = "130,200"
$colorDemoTextBox.Size = "200,200"
$colorDemoTextBox.ForeColor = "MediumBlue"
$colorDemoTextBox.BackColor = "White"
$colorDemoTextBox.Text = " Color 1234"
$mainForm.Controls.Add($colorDemoTextBox)

When you click the “Pick Color” button the PickColor function is called. I define the ColorDialog object, set it to allow the dialog to be expanded for custom colors and display it. I collect the R,G,B,and A values as well as the Color Name. The Alpha (A) value represents transparency. The Color Name us usually the concatenated hex value of the combined ARGB values, but for the approximately 150 Named Colors the function returns the colors name. See the .Net Color Structure for those Color Names. I convert the returned hex value to Int32 to feed into the ForeColor of the sample TextBox, but the Named Colors will fail so I capture the error and use the Colors Name for those values. The TextBox ForeColor will accept either value.

# Lookup Button
$lookupButton = New-Object System.Windows.Forms.Button
$lookupButton.Location = "300,20"
$lookupButton.Size = "85,28"
$lookupButton.ForeColor = "Green"
$lookupButton.BackColor = "White"
$lookupButton.Text = "Pick Color"
$lookupButton.add_Click({PickColor})
$mainForm.Controls.Add($lookupButton)

function PickColor {
    $colorDialog = new-object System.Windows.Forms.ColorDialog
    $colorDialog.AllowFullOpen = $true
    $colorDialog.ShowDialog()
    $alphaTextBox.Text = $colordialog.color.A
    $redTextBox.Text = $colordialog.color.R
    $greenTextBox.Text = $colordialog.color.G
    $blueTextBox.Text = $colordialog.color.B
    $colorNameTextBox.Text = $colorDialog.Color.Name
    $error.clear()
    $colorDemoTextBox.ForeColor = [Convert]::ToInt32($colorDialog.Color.Name, 16)
    If($error.count -gt 0) {
        $colorDemoTextBox.ForeColor = $colorDialog.Color.Name
    }
}

Note – In order to pick up a custom colors value, it needs to be added to the Custom Colors list in the ColorDialog. See the ColorDialog image above as an example.

ColorDialog

ColorDialog.ps1

Advertisements