Re: manipulating hex values
- From: "Kevin Spencer" <kevin@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 2 Mar 2006 06:59:34 -0500
Hi darrell,
Use the Int32.Parse(String, System.Globalization.NumberStyles) overload to
parse the string, and perform math on it. Then use the
Int32.ToString(String) method to translate it back. Example:
string color = "#6699cc";
int r, g, b;
double rr, gg, bb;
// Parse string to Int32 values
r = Int32.Parse(color.Substring(1, 2),
System.Globalization.NumberStyles.AllowHexSpecifier);
g = Int32.Parse(color.Substring(3, 2),
System.Globalization.NumberStyles.AllowHexSpecifier);
b = Int32.Parse(color.Substring(5, 2),
System.Globalization.NumberStyles.AllowHexSpecifier);
rr = (double)r/255D;
gg = (double)g/255D;
bb = (double)b/255D;
// perform some math on the percentages, then convert back
r = (int)(rr * 255D);
g = (int)(gg * 255D);
b = (int)(bb * 255D);
color = "#" + r.ToString("X2") + g.ToString("X2") + b.ToString("X2");
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"darrel" <notreal@xxxxxxxxxxx> wrote in message
news:%23%23uEOFYPGHA.2704@xxxxxxxxxxxxxxxxxxxxxxx
(apologies if I've asked this before in here...I thought I had, but can't
seem to find my original post).
I'm trying to automate the process of picking some colors. I want to
select one color, then programtically generate some different values of
the same color...both lighter and darker.
I'd like to start and end with a hex value.
Can that be done in VB.net? It looks like I can, but the hex values I see
in examples don't quite match the typical hex notation of color values in
CSS.
For instance, I'd like to take this:
#6699cc
Split it into 3 values (easy enough) and convert to a percentage value
(not sure on that step, but basically 'hex converted to base 10/255'):
66 = .4
99 = .6
cc = .8
Then do some math on each (like multiply by 1.2 to make a ligher color)
and then convert back to hex.
It looks like hex is a type of LONG, but I'm not exactly sure how to
translate between the two. Is ctype the answer?
-Darrel
.
- Follow-Ups:
- Re: manipulating hex values
- From: darrel
- Re: manipulating hex values
- References:
- manipulating hex values
- From: darrel
- manipulating hex values
- Prev by Date: Re: Postback Problem
- Next by Date: Re: Help with calculation
- Previous by thread: manipulating hex values
- Next by thread: Re: manipulating hex values
- Index(es):
Relevant Pages
|