Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > manipulating hex values

Reply
Thread Tools

manipulating hex values

 
 
darrel
Guest
Posts: n/a
 
      03-01-2006
(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



 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      03-02-2006
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.AllowHexSpecifie r);
g = Int32.Parse(color.Substring(3, 2),
System.Globalization.NumberStyles.AllowHexSpecifie r);
b = Int32.Parse(color.Substring(5, 2),
System.Globalization.NumberStyles.AllowHexSpecifie r);

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" <> wrote in message
news:%23%...
> (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
>
>
>



 
Reply With Quote
 
 
 
 
darrel
Guest
Posts: n/a
 
      03-02-2006
> 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:


Kevin:

Excellent! That worked wonderfully.

Alas, I'm finding that subtle color shifting really isn't best handled by
simple math. Looks like I'm going to want to adjust each shade differently
depending on the source file.

So, seemed great in concept, but not so great in practice.

However, I now have a nice script for future reference. Thanks!

-Darrel


 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      03-02-2006
Hi Darrell,

Bob Powell has some nice code samples for modifying colors. Check out:
http://www.bobpowell.net/gdiplus_faq.htm

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.


"darrel" <> wrote in message
news:Ot$...
>> 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:

>
> Kevin:
>
> Excellent! That worked wonderfully.
>
> Alas, I'm finding that subtle color shifting really isn't best handled by
> simple math. Looks like I'm going to want to adjust each shade differently
> depending on the source file.
>
> So, seemed great in concept, but not so great in practice.
>
> However, I now have a nice script for future reference. Thanks!
>
> -Darrel
>



 
Reply With Quote
 
darrel
Guest
Posts: n/a
 
      03-03-2006
> Bob Powell has some nice code samples for modifying colors. Check out:
> http://www.bobpowell.net/gdiplus_faq.htm


Thanks, Kevin.

I actually got things working based on your sample. The issue was that I was
incrementing each color channel equally, when I really needed to increase
(or decrease) each channel using an equal percentage.

Here's what I ended up with:

=====================================
'this particular example creates 3 value variations from one initial color
input

dim siteColor1 as string = "cd7f32"
dim siteColor2 as string
dim siteColor3 as string
dim siteColor4 as string

Dim loopCount As Integer

'for the original hex values
Dim r As Integer
Dim g As Integer
Dim b As Integer

'for the converted to decimal values (one for each color variation)
Dim rr(3) As Double
Dim gg(3) As Double
Dim bb(3) As Double

' Parse string to Int32 values
r = Int32.Parse(siteColor1.Substring(0, 2),
System.Globalization.NumberStyles.AllowHexSpecifie r)
g = Int32.Parse(siteColor1.Substring(2, 2),
System.Globalization.NumberStyles.AllowHexSpecifie r)
b = Int32.Parse(siteColor1.Substring(4, 2),
System.Globalization.NumberStyles.AllowHexSpecifie r)

'convert to decimal
loopCount = 0
While loopCount < 4
rr(loopCount) = CType(r, Double) / 255D
gg(loopCount) = CType(g, Double) / 255D
bb(loopCount) = CType(b, Double) / 255D
loopCount = loopCount + 1
End While

'now the colors are in decimal format
Dim colorMultiplier(3) As Decimal

'lighten or darken each color.
'positive decimals up to 1 = ligher (0-100%)
'negative numbers down to -1 = darker (0-100%)

colorMultiplier(0) = 0
colorMultiplier(1) = .7
colorMultiplier(2) = .6
colorMultiplier(3) = -.8

'multiply everything
loopCount = 0
While loopCount < 4
If colorMultiplier(loopCount) > 0 Then
'lighten color
rr(loopCount) = rr(loopCount) + ((1 - rr(loopCount)) *
colorMultiplier(loopCount))
gg(loopCount) = gg(loopCount) + ((1 - gg(loopCount)) *
colorMultiplier(loopCount))
bb(loopCount) = bb(loopCount) + ((1 - bb(loopCount)) *
colorMultiplier(loopCount))
ElseIf colorMultiplier(loopCount) < 0 Then
'darken color
rr(loopCount) = rr(loopCount) + (rr(loopCount) *
colorMultiplier(loopCount))
gg(loopCount) = gg(loopCount) + (gg(loopCount) *
colorMultiplier(loopCount))
bb(loopCount) = bb(loopCount) + (bb(loopCount) *
colorMultiplier(loopCount))
Else
'do nothing, as multiplier = 0 which means keep color as-is.
End If
loopCount = loopCount + 1
End While

'convert back to hex and create the color variables
r = CType(rr(1) * 255D, Integer)
g = CType(gg(1) * 255D, Integer)
b = CType(bb(1) * 255D, Integer)
siteColor2 = r.ToString("X2") + g.ToString("X2") + b.ToString("X2")

r = CType(rr(2) * 255D, Integer)
g = CType(gg(2) * 255D, Integer)
b = CType(bb(2) * 255D, Integer)
siteColor3 = r.ToString("X2") + g.ToString("X2") + b.ToString("X2")

r = CType(rr(3) * 255D, Integer)
g = CType(gg(3) * 255D, Integer)
b = CType(bb(3) * 255D, Integer)
siteColor4 = r.ToString("X2") + g.ToString("X2") + b.ToString("X2")
=====================================

Thanks for the help!

-Darrel




 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      03-03-2006
> I actually got things working based on your sample. The issue was that I
> was incrementing each color channel equally, when I really needed to
> increase (or decrease) each channel using an equal percentage.


Yup. I believe Bob Powell talks about the difference in luminosity of the 3
values. At least I read it somewhere....

Great work, darrel!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"darrel" <> wrote in message
news:...
>> Bob Powell has some nice code samples for modifying colors. Check out:
>> http://www.bobpowell.net/gdiplus_faq.htm

>
> Thanks, Kevin.
>
> I actually got things working based on your sample. The issue was that I
> was incrementing each color channel equally, when I really needed to
> increase (or decrease) each channel using an equal percentage.
>
> Here's what I ended up with:
>
> =====================================
> 'this particular example creates 3 value variations from one initial color
> input
>
> dim siteColor1 as string = "cd7f32"
> dim siteColor2 as string
> dim siteColor3 as string
> dim siteColor4 as string
>
> Dim loopCount As Integer
>
> 'for the original hex values
> Dim r As Integer
> Dim g As Integer
> Dim b As Integer
>
> 'for the converted to decimal values (one for each color variation)
> Dim rr(3) As Double
> Dim gg(3) As Double
> Dim bb(3) As Double
>
> ' Parse string to Int32 values
> r = Int32.Parse(siteColor1.Substring(0, 2),
> System.Globalization.NumberStyles.AllowHexSpecifie r)
> g = Int32.Parse(siteColor1.Substring(2, 2),
> System.Globalization.NumberStyles.AllowHexSpecifie r)
> b = Int32.Parse(siteColor1.Substring(4, 2),
> System.Globalization.NumberStyles.AllowHexSpecifie r)
>
> 'convert to decimal
> loopCount = 0
> While loopCount < 4
> rr(loopCount) = CType(r, Double) / 255D
> gg(loopCount) = CType(g, Double) / 255D
> bb(loopCount) = CType(b, Double) / 255D
> loopCount = loopCount + 1
> End While
>
> 'now the colors are in decimal format
> Dim colorMultiplier(3) As Decimal
>
> 'lighten or darken each color.
> 'positive decimals up to 1 = ligher (0-100%)
> 'negative numbers down to -1 = darker (0-100%)
>
> colorMultiplier(0) = 0
> colorMultiplier(1) = .7
> colorMultiplier(2) = .6
> colorMultiplier(3) = -.8
>
> 'multiply everything
> loopCount = 0
> While loopCount < 4
> If colorMultiplier(loopCount) > 0 Then
> 'lighten color
> rr(loopCount) = rr(loopCount) + ((1 - rr(loopCount)) *
> colorMultiplier(loopCount))
> gg(loopCount) = gg(loopCount) + ((1 - gg(loopCount)) *
> colorMultiplier(loopCount))
> bb(loopCount) = bb(loopCount) + ((1 - bb(loopCount)) *
> colorMultiplier(loopCount))
> ElseIf colorMultiplier(loopCount) < 0 Then
> 'darken color
> rr(loopCount) = rr(loopCount) + (rr(loopCount) *
> colorMultiplier(loopCount))
> gg(loopCount) = gg(loopCount) + (gg(loopCount) *
> colorMultiplier(loopCount))
> bb(loopCount) = bb(loopCount) + (bb(loopCount) *
> colorMultiplier(loopCount))
> Else
> 'do nothing, as multiplier = 0 which means keep color as-is.
> End If
> loopCount = loopCount + 1
> End While
>
> 'convert back to hex and create the color variables
> r = CType(rr(1) * 255D, Integer)
> g = CType(gg(1) * 255D, Integer)
> b = CType(bb(1) * 255D, Integer)
> siteColor2 = r.ToString("X2") + g.ToString("X2") + b.ToString("X2")
>
> r = CType(rr(2) * 255D, Integer)
> g = CType(gg(2) * 255D, Integer)
> b = CType(bb(2) * 255D, Integer)
> siteColor3 = r.ToString("X2") + g.ToString("X2") + b.ToString("X2")
>
> r = CType(rr(3) * 255D, Integer)
> g = CType(gg(3) * 255D, Integer)
> b = CType(bb(3) * 255D, Integer)
> siteColor4 = r.ToString("X2") + g.ToString("X2") + b.ToString("X2")
> =====================================
>
> Thanks for the help!
>
> -Darrel
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Re: manipulating hex values Stephen Cattaneo Python 1 04-03-2008 02:31 PM
manipulating hex values Stephen Cattaneo Python 0 04-01-2008 05:11 PM
manipulating hex in ASP.net darrel ASP .Net 2 10-06-2005 07:03 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments
 



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 47 48 49 50 51 52 53 54 55 56 57