Which method allows convert ARGB to string as 'int, int, int'
? Example. The $Main
background color is set as a string '0, 100, 200'
. Return $Main.BackColor
gives ARGB but not string '0, 100, 200':
$Main = [System.Windows.Forms.Form] @{
BackColor = '0, 100, 200'
}
$Main.BackColor
--------------
R : 0
G : 100
B : 200
A : 255
IsKnownColor : False
IsEmpty : False
IsNamedColor : False
IsSystemColor : False
Name : ff0064c8
The To.String()
method returns the result Color [A=255, R=0, G=100, B=200]
. This is not what is expected.
At the moment I am doing this:
('R', 'G', 'B').ForEach({ $Main.BackColor.$_ }) -join ', '
--------------
0, 100, 200
However, I hope that there are special methods for converting as ARGB
to string
, as string
to ARGB
. What are these methods? Thanks
However, I hope that there are special methods for converting as argb to string, as string to argb. What are these methods?
You'd think so, but to my knowledge there isn't a builtin two-way convertion for this, unfortunately.
Here are a couple of things you can do instead:
ConvertFrom-Color
function:function ConvertFrom-Color
{
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[System.Drawing.Color[]]$InputColor
)
process {
foreach($color in $InputColor){
$color.R,$color.G,$color.B -join ', '
}
}
}
And use when needed:
PS C:\> $colorString = $Main.BackColor |ConvertFrom-Color
PS C:\> $colorString
0, 100, 200
[int]
An ARGB
color consists of 4 1-byte channels, which fits perfectly into a 32-bit integer. If you need to communicate a distinct color using a single argument, this is already supported:
PS C:\> $color = [System.Drawing.Color]'0,100,100'
PS C:\> $color.ToArgb()
-16751516
PS C:\> $argbValue = $color.ToArgb()
PS C:\> [System.Drawing.Color]::FromArgb($argbValue)
R : 0
G : 100
B : 100
A : 255
IsKnownColor : False
IsEmpty : False
IsNamedColor : False
IsSystemColor : False
Name : ff006464
Just like with your initial string representation, it's worth highlighting that converting from the [int]
representation works both with explicit casts and implicit conversions when assigning to typed properties:
$argbValue = -16751516
# This works just fine
[System.Drawing.Color]$argbValue
# As does this
[System.Windows.Forms.Form]@{ BackColor = $argbValue }
Color.ToString()
Since you're interested in changing the behavior of the default string representation of System.Drawing.Color
, you might as well override the ToString()
method implementation:
Update-TypeData -TypeName System.Drawing.Color -MemberType ScriptMethod -MemberName ToString -Value {
$this.psobject.Properties['R','G','B'].Value -join ', '
} -Force
Now all instances of [System.Drawing.Color]
will use your custom ToString()
method when converted to a string:
PS C:\> "$($Main.BackColor)"
0, 100, 200
There are some nice and initiative answers for this question, but it looks like all have overlooked the correct way, using type converters. There is a ColorConverter
responsible for ConvertTo
and ConvertFrom
String/Color:
$Color = [System.Drawing.Color]'0, 100, 200'
[System.Drawing.ColorConverter]::new().ConvertToString($Color)
# Result: 0, 100, 200
This is the same class which is used by PropertyGrid
to convert the string to Color
or Color
and vice versa.
It will take care of A
element of the color as well, for example if you create the color like 100, 0, 100, 200
. This solution automatically include or ignore A
portion whenever is required:
$Color = [System.Drawing.Color]'100, 0, 100, 200'
[System.Drawing.ColorConverter]::new().ConvertToString($Color)
# Result: 100, 0, 100, 200
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With