473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

conversion of color to hex string


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

Nov 19 '05 #1
5 1254
ColorTranslator.ToHtml should do the trick.

Nov 19 '05 #2
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.

<ex********@libero.it> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...

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

Nov 19 '05 #3
Darn! I should have thought of that!

--
:-P,

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

"Phillip Ian" <ph****@comcast.net> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
ColorTranslator.ToHtml should do the trick.

Nov 19 '05 #4
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 !

Nov 19 '05 #5
Good point!

--

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

<ex********@libero.it> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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 !

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ken | last post by:
I've been looking for a solution to a string to long conversion problem that I've run into >>> x = 'e10ea210' >>> print x e10ea210 >>> y=long(x) Traceback (most recent call last): File...
3
by: praba kar | last post by:
Dear All, I have doubt regarding date string to time conversion function. In Python I cannot find flexible date string conversion function like php strtotime. I try to use following type...
2
by: Thomas Matthews | last post by:
Hi, I'm working with Borland C++ Builder 6.2. My project uses the std::string class. However, Borland in its infinite wisdom has its own string class, AnsiString. To make my life easier, I...
14
by: ES Kim | last post by:
Consider: #include <string> #include <iostream> using namespace std; struct S { const char* ps_; operator string();
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
16
by: frs | last post by:
See example below: Why does the output of 'a' work and the output of 'b' fails to compile? Is there a way to write class 'something' so that 'b' converts correctly by default? (include iostream,...
4
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
2
by: Sankar Nemani | last post by:
Why does the following code throw an exception? object o = new object{"sankar", "sankar2"}; string s; s =(string)o; The C# language spec says something different on MSDN: For any two...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.