Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Translating colors to hex and back

Reply
 
Old 03-08-2007   #1 (permalink)
Marco Shaw


 
 

Translating colors to hex and back

PowerShell/.NET/COM have anything that can take a 'human readable' color
like "red" and convert it to hex, and vice-versa?

Marco



My System SpecsSystem Spec
Old 03-08-2007   #2 (permalink)
Keith Hill [MVP]


 
 

Re: Translating colors to hex and back

"Marco Shaw" <marcoDOTshaw_@_gmailDOTcom> wrote in message news:%23Mz37NaYHHA.2436@TK2MSFTNGP06.phx.gbl...
> PowerShell/.NET/COM have anything that can take a 'human readable' color
> like "red" and convert it to hex, and vice-versa?
>


42# ipasm System.Drawing # from PSCX 1.1

GAC Version Location
--- ------- --------
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b...


43# [drawing.Color]::FromName('red')


R : 255
G : 0
B : 0
A : 255
IsKnownColor : True
IsEmpty : False
IsNamedColor : True
IsSystemColor : False
Name : Red

44# "0x{0:X}" -f [drawing.Color]::FromName('red').R
0xFF

To go the other way is a bit harder:

57# [Enum]::GetValues([drawing.KnownColor]) | %{$c = [drawing.color]::FromKnownC
olor($_); if ($c.ToArgb() -eq 0xffff0000) { "The color is $_" }}
The color is Red

Note that the hex format is AARRGGBB where AA is the alpha channel and is normally set to FF.

--
Keith
My System SpecsSystem Spec
Old 03-08-2007   #3 (permalink)
/\\/\\o\\/\\/ [MVP]


 
 

Re: Translating colors to hex and back

PoSH> [System.Drawing.Color]'red'


R : 255
G : 0
B : 0
A : 255
IsKnownColor : True
IsEmpty : False
IsNamedColor : True
IsSystemColor : False
Name : Red

PoSH> ([System.Drawing.Color]'red' ).ToArgb()
-65536

Greetings /\/\o\/\/

"Marco Shaw" <marcoDOTshaw_@_gmailDOTcom> wrote in message
news:%23Mz37NaYHHA.2436@TK2MSFTNGP06.phx.gbl...
> PowerShell/.NET/COM have anything that can take a 'human readable' color
> like "red" and convert it to hex, and vice-versa?
>
> Marco
>


My System SpecsSystem Spec
Old 03-08-2007   #4 (permalink)
RichS


 
 

RE: Translating colors to hex and back

just out of curiosity what does red translate to in hex
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Marco Shaw" wrote:

> PowerShell/.NET/COM have anything that can take a 'human readable' color
> like "red" and convert it to hex, and vice-versa?
>
> Marco
>
>
>

My System SpecsSystem Spec
Old 03-08-2007   #5 (permalink)
Keith Hill [MVP]


 
 

Re: Translating colors to hex and back

"RichS" <RichS@discussions.microsoft.com> wrote in message
news:6E3D0DAE-E819-4CE1-B645-F10B91E5D447@microsoft.com...
> just out of curiosity what does red translate to in hex


0xFFFF0000

--
Keith


My System SpecsSystem Spec
Old 03-08-2007   #6 (permalink)
/\\/\\o\\/\\/ [MVP]


 
 

Re: Translating colors to hex and back

PoSH> $c = [drawing.color]'red'
PoSH> "{0:x}" -f $c.ToArgb()
ffff0000

;-)

gr /\/\o\/\/

"RichS" <RichS@discussions.microsoft.com> wrote in message
news:6E3D0DAE-E819-4CE1-B645-F10B91E5D447@microsoft.com...
> just out of curiosity what does red translate to in hex
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "Marco Shaw" wrote:
>
>> PowerShell/.NET/COM have anything that can take a 'human readable' color
>> like "red" and convert it to hex, and vice-versa?
>>
>> Marco
>>
>>
>>


My System SpecsSystem Spec
Old 03-08-2007   #7 (permalink)
/\\/\\o\\/\\/ [MVP]


 
 

Re: Translating colors to hex and back

Ok, but ( curiosity killed the cat ) what is white ?

PoSH> [enum]::GetNames([System.ConsoleColor]) |% {$_;"{0:x}" -f
([drawing.Color]$_).ToArgb()}
Black
ff000000
DarkBlue
ff00008b
DarkGreen
ff006400
DarkCyan
ff008b8b
DarkRed
ff8b0000
DarkMagenta
ff8b008b
DarkYellow
Cannot convert value "DarkYellow" to type "System.Drawing.Color". Error:
"DarkYellow is not a valid value for Int32."
At line:1 char:77
+ [enum]::GetNames([System.ConsoleColor]) |% {$_;"{0:x}" -f
([drawing.Color]$_) <<<< .ToArgb()}
Gray
ff808080
DarkGray
ffa9a9a9
Blue
ff0000ff
Green
ff008000
Cyan
ff00ffff
Red
ffff0000
Magenta
ffff00ff
Yellow
ffffff00
White
ffffffff

For DarkYellow your on your own , but .. (more than you did want to know )

PoSH> [enum]::GetNames([drawing.KnownColor]) |% {$_;"{0:x}" -f
([drawing.Color]$_).ToArgb()}

but got to pack, plane leaves soon ;-)

PoSH> [datetime]'Friday, March 09, 2007 01:15 PM' - (get-date) |% {"$_"}
16:05:16.2980055

enjoy,

Greetings /\/\o\/\/

"/\/\o\/\/ [MVP]" <mow001@hotmail.NoSpam> wrote in message
news:222FDFD3-E735-4B99-9A53-E31E67D4F0B3@microsoft.com...
> PoSH> $c = [drawing.color]'red'
> PoSH> "{0:x}" -f $c.ToArgb()
> ffff0000
>
> ;-)
>
> gr /\/\o\/\/
>
> "RichS" <RichS@discussions.microsoft.com> wrote in message
> news:6E3D0DAE-E819-4CE1-B645-F10B91E5D447@microsoft.com...
>> just out of curiosity what does red translate to in hex
>> --
>> Richard Siddaway
>> Please note that all scripts are supplied "as is" and with no warranty
>> Blog: http://richardsiddaway.spaces.live.com/
>> PowerShell User Group: http://www.get-psuguk.org.uk
>>
>>
>> "Marco Shaw" wrote:
>>
>>> PowerShell/.NET/COM have anything that can take a 'human readable' color
>>> like "red" and convert it to hex, and vice-versa?
>>>
>>> Marco
>>>
>>>
>>>

>


My System SpecsSystem Spec
Old 03-08-2007   #8 (permalink)
Thomas Lee


 
 

Re: Translating colors to hex and back

In message <222FDFD3-E735-4B99-9A53-E31E67D4F0B3@microsoft.com>,
"/\\/\\o\\/\\/ [MVP]" <mow001@hotmail.NoSpam> writes
>PoSH> $c = [drawing.color]'red'
>PoSH> "{0:x}" -f $c.ToArgb()
>ffff0000
>
>;-)



Thanks VERY much.

BTW: where is this format documented?

The calling seqence is a bit off, I sort of expected
"{0:x} in hex`n{1} in decimal" -f $c.ToArgb() $c.ToArgb()

To work. But it generated an error, whereas:

I was sort of surprised to find this worked:
PSH [D:\foo]: "{0:x} in hex`n{1} in decimal" -f $c.ToArgb(),$c.ToArgb()
ffff0000 in hex
-65536 in decimal

I expected the paramaters to the right of '-f' to be space, not comma
delimited.

Thomas
--
Thomas Lee
doctordns@gmail.com
MVP - Admin Frameworks and Security
My System SpecsSystem Spec
Old 03-08-2007   #9 (permalink)
Keith Hill [MVP]


 
 

Re: Translating colors to hex and back

"Thomas Lee" <tfl@psp.co.uk> wrote in message
news:mKgF6DRjyJ8FFArR@mail.psp.co.uk...
> In message <222FDFD3-E735-4B99-9A53-E31E67D4F0B3@microsoft.com>,
> "/\\/\\o\\/\\/ [MVP]" <mow001@hotmail.NoSpam> writes
>>PoSH> $c = [drawing.color]'red'
>>PoSH> "{0:x}" -f $c.ToArgb()
>>ffff0000
>>
>>;-)

>
>
> Thanks VERY much.
>
> BTW: where is this format documented?

http://msdn2.microsoft.com/en-us/lib...or.toargb.aspx

>
> The calling seqence is a bit off, I sort of expected
> "{0:x} in hex`n{1} in decimal" -f $c.ToArgb() $c.ToArgb()
>
> To work. But it generated an error, whereas:
>
> I was sort of surprised to find this worked:
> PSH [D:\foo]: "{0:x} in hex`n{1} in decimal" -f $c.ToArgb(),$c.ToArgb()
> ffff0000 in hex
> -65536 in decimal
>
> I expected the paramaters to the right of '-f' to be space, not comma
> delimited.


Yeah I've been bit by that before. I just had to wire it into my memory
that -f expects an array arguments.

--
Keith


My System SpecsSystem Spec
Old 03-08-2007   #10 (permalink)
Marco Shaw


 
 

Re: Translating colors to hex and back


> "Marco Shaw" <marcoDOTshaw_@_gmailDOTcom> wrote in message
> news:%23Mz37NaYHHA.2436@TK2MSFTNGP06.phx.gbl...
>> PowerShell/.NET/COM have anything that can take a 'human readable' color
>> like "red" and convert it to hex, and vice-versa?
>>
>> Marco


/\/\o\/\/,

You're not allowed to make fun of my color choices...

Chocolate... Really now!


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Translating English to Polish .NET General
Translating Japanese to English In a browser Browsers & Mail
help translating winrm authendication to powershell PowerShell
VISTA feels like going back to the Stone Age (in nice colors) [Edited] Vista General
Windows Mail Stationery Stopped Working, Also Font colors and background colors... Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

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