conversion of color to hex string

E

expertware

To conver a color to its hexadecimal string I have devised
this way:

Function HexStringFromColor(ByVal Colore As Color) As String
Return "#" & Colore.FromArgb(Colore.A,
Colore).Name.Substring(2).ToUpper
End Function

but it seems a little bit ackward. I would like to ask if there
is a better way (colortranslator does not seem to have this function,
but I may be missing it).

-Pamela
 
K

Kevin Spencer

Not bad. Perhaps:

Function HexStringFromColor(ByVal Colore As Color) As String
Return "#" & Colore.ToArgb().ToString("X").Substring(2)
End Function

The ToArgb() method returns a 32-bit number containing the A,R,G, and B
components of the color. The Int32.ToString(string) method converts an Int32
to a string using the format string provided. The "X" format string converts
the Int32 to a string representation of hexadecimal notation for the number.
And the Substring() removes the Alpha hex value from the string.

But it isn't much more succinct than yours. I don't think it can be.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.
 
K

Kevin Spencer

Darn! I should have thought of that!

--
:p,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.
 
E

expertware

This one also nice, but still feel ackward.

I can't believe there isn't a straight way to do such
a common task.

-Pamela

PS. no would be too easy! ColorTranslator.ToHtml
is NOT the same . It yelds the color name if it exists

* I always want the HEX *

eehi mpv !
 
K

Kevin Spencer

Good point!

--

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top