Color Conversions
In this issue:
Last time, we started looking a variety of ways you might use color in your PowerShell work. You might need a value for a cmdlet parameter. Or maybe you are creating your own tool that takes advantage of color. How do you want to specify the color? That might depend on what technology or technique you are using to express the color.
Using console colors with Write-Host
is pretty straightforward. But what if you are using values from System.Drawing.Color
? Or need to work with hex values like #FF5733
? Let's look at some ways to convert between different color representations. This might make your scripting work a little easier.
System.Drawing.Color
For our work today, you'll need to import the System.Drawing
assembly if you haven't already. This assembly contains the Color
class, which provides methods for creating and manipulating colors.
Add-Type -AssemblyName System.Drawing
Most of the time, converting color formats comes down to the RGB (Red, Green, Blue) color model. This model represents colors as combinations of red, green, and blue light. Each component can have a value from 0 to 255. Fortunately, the System.Drawing.Color
class provides methods to work with RGB values as well as exposing the RGB values.
You might have a brush object.
PS C:\> [system.Drawing.Brushes]::SkyBlue
Color
-----
Color [SkyBlue]
Or the color object itself.
PS C:\> [system.Drawing.Color]::SkyBlue
R : 135
G : 206
B : 235
A : 255
IsKnownColor : True
IsEmpty : False
IsNamedColor : True
IsSystemColor : False
Name : SkyBlue
Here, you can see the RGB values for the color SkyBlue
. The red component is 135, the green component is 206, and the blue component is 235.
Color Names
Depending on how you are working with color values, you might find it easier to use the FromName()
method.
PS C:\> [System.Drawing.Color]::FromName("SkyBlue")
R : 135
G : 206
B : 235
A : 255
IsKnownColor : True
IsEmpty : False
IsNamedColor : True
IsSystemColor : False
Name : SkyBlue
I didn't mention last time, but all of the named colors are also defined as static properties of the System.Drawing.KnownColor
class.

> Get-TypeMember
is part of the PSScriptTools module.
PS C:\> [System.Drawing.KnownColor]::Snow
Snow
If using known colors, you could also the the FromKnownColor()
method.
PS C:\> [System.Drawing.Color]::FromKnownColor("SkyBlue")
R : 135
G : 206
B : 235
A : 255
IsKnownColor : True
IsEmpty : False
IsNamedColor : True
IsSystemColor : False
Name : SkyBlue
One reason you might want to use the FromName()
method is that it will return a color object even if the name isn't valid. The FromKnownColor()
method will return black if the name isn't valid.
PS C:\> [System.Drawing.Color]::FromName("Blizzard")
R : 0
G : 0
B : 0
A : 0
IsKnownColor : False
IsEmpty : False
IsNamedColor : True
IsSystemColor : False
Name : Blizzard
If you need to write error handling, this might make a difference.
Using RGB
If you have RGB values from some source, you can create a color using those values. For example:
PS C:\> $color = [System.Drawing.Color]::FromArgb(255, 87, 51)
PS C:\> $color
R : 255
G : 87
B : 51
A : 255
IsKnownColor : False
IsEmpty : False
IsNamedColor : False
IsSystemColor : False
Name : ffff5733
This doesn't match a predefined color name, but you can see the RGB values and you could use it. However, we're more interested in working with known colors like SkyBlue
or Cornsilk
.
Since we know how to get the RGB values, we can use those values to convert to other formats. To simplify the process, here's a simple function that takes a color name and returns the RGB values.
function Get-RGB {
[cmdletbinding()]
[OutputType('RGB')]
Param(
[Parameter(Mandatory, HelpMessage = 'Enter the name of a system color like Tomato')]
[ValidateNotNullOrEmpty()]
[string]$Name
)
Try {
$color = [System.Drawing.Color]::FromKnownColor($Name)
[PSCustomObject]@{
PSTypeName = 'RGB'
Name = $color.Name
Red = $color.R
Green = $color.G
Blue = $color.B
}
}
Catch {
Write-Error "$Name is not a valid known color choice."
}
}
We can use the output of this function to convert to other formats.
PS C:\> Get-Rgb foo
Get-RGB: foo is not a valid known color choice.
PS C:\> Get-Rgb Cornsilk
Name Red Green Blue
---- --- ----- ----
Cornsilk 255 248 220