Working with Colors in ASP

# Thread: Working with Colors in ASP

1. Member
Join Date
Dec 1969
Posts
91

## Working with Colors in ASP

Is there a way to script a function that will take a color, say #99bbff, and darken it? I don&#039;t understand the mathematics of HEX but I&#039;m looking for a way to give me a lighter and darker version of the currently selected color..<BR><BR>Any ideas?<BR><BR>Thanks.<BR>Geoff

2. Senior Member
Join Date
Dec 1969
Posts
5,955

## It should be rather straightforward. All...

...you have to do is split the value into its RGB components (red, green and blue), divide them by the relevant number (for exmpale 2 for 50%) and then recombine them.<BR><BR>In your example:<BR><BR>1. Split: Red = #99, Green = #BB, Blue = #FF<BR>2. Do the division (50%): Red = #4C, Green = #5D, Blue = #7F<BR>3. Recombine: #4C5D7F<BR><BR>Hey presto, the original value darkened to 50%.<BR><BR>Oliver.

3. Senior Member
Join Date
Dec 1969
Posts
1,212

## hey, Oliver...

... do you fancy doing a quick hex masterclass? Or point us clueless types in the direction of a decent guide to how it works?<BR><BR>Ta<BR><BR>Rob

4. Senior Member
Join Date
Dec 1969
Posts
733

## Not a master class but..

not sure that there is much to undestand really. Colours in hex format contain 3 values, one each for Red, Green, Blue. 0 is none of that colour, FF (equivalent to 255 decimal) is 100% of that colour.<BR><BR>So black is #000000 with none of any colour and white is #FFFFFF 100% of all three colours. Pure red would be #FF0000 etc. To make a darker shade ofa colour you need to move each component towards zero by the same proportion, to make a lighter version move each component closer to FF by the same proportion.<BR><BR>A nice thing to remember is that the 216 &#039;web safe&#039; colours are defined by 00, 33, 66, CC, FF in each component. So, if your colour is something like #CC6633 it will be websafe, but #CC6636 will not be.

5. Senior Member
Join Date
Dec 1969
Posts
1,212

## so what's CC? <eop>

.

6. Senior Member
Join Date
Dec 1969
Posts
733

## CC in hex = 204 in dec

1=1<BR>2=2<BR>3=3...<BR>9=9<BR>A=10<BR>B=11<BR>C=1 2<BR>D=13<BR>E=14<BR>F=15<BR>10=16<BR>11=17...<BR> 19=25<BR>1A=26...<BR>1F=31<BR>20=32<BR><BR>etc

7. Member
Join Date
Dec 1969
Posts
91

## RE: CC in hex = 204 in dec

So how do you get #4C when you halve #99, etc?<BR><BR>This is helpful, thanks.<BR>Geoff

8. Senior Member
Join Date
Dec 1969
Posts
5,955

## Just convert the hex to dec...

...then divide it by two, then back to hex:<BR><BR>#99 = 9 * 16 + 9 = 144 + 9 = 153<BR>153 / 2 = 76.5 = 76<BR>76 / 16 = 4 - remainder: 12<BR>76 = #4C (C = 12)<BR><BR>Quite simple really. :-)<BR><BR>Anyway, if you have a hex colour value, all you need to do is make sure it&#039;s six characters long, then split it, so that you end up with three sets of two characters. Convert each set into decimal, divide it by two, then convert it back to hex. Then re-combine the new sets, again making sure you have three sets of exactly TWO hex digits, otherwise you get crap, i.e.:<BR><BR>#040404 -&#062; 4 / 4 / 4 -&#062; 2 / 2 / 2 -&#062; #2 / #2 / #2 -&#062; #222 -&#062; which is wrong.<BR><BR>Oliver.

9. Senior Member
Join Date
Dec 1969
Posts
5,955

## How do I convert hex to dec? ...

... Well, that&#039;s easy.<BR><BR>Say you have a six digit hex colour, e.g.: FFFFFF<BR><BR>To convert that into three decimal values, split it into three sets of two digits:<BR><BR>Dim sHex, sR, sG, sB<BR><BR>sHex = "FFFFFF"<BR>sR = Left(sHex, 2)<BR>sG = Mid(sHex, 3, 2)<BR>sB = Right(sHex, 2)<BR><BR>Then convert those into dec. Not sure whether VBScript provides a relevant function for you - CInt() might work? If not, do this:<BR><BR>Function HexToDec(sHex)<BR>&nbsp;&nbsp;HexToDec = 0<BR><BR>&nbsp;&nbsp;Dim nIdx<BR>&nbsp;&nbsp;For nIdx = 1 To Len(sHex)<BR>&nbsp;&nbsp;&nbsp;&nbsp;HexToDec = HexToDec * 16<BR>&nbsp;&nbsp;&nbsp;&nbsp;If Mid(sHex, nIdx, 1) &#062;= "A" And Mid(sHex, nIdx, 1) &#060;= "F" Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HexToD ec = HexToDec + Asc(Mid(sHex, nIdx, 1) - Asc("F") + 16<BR>&nbsp;&nbsp;&nbsp;&nbsp;Else<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;HexToDec = Val(Mid(sHex, nIdx, 1))<BR>&nbsp;&nbsp;&nbsp;&nbsp;End If<BR>&nbsp;&nbsp;Next<BR>End Function<BR><BR>Now, do this:<BR><BR>Dim nR, nG, nB<BR><BR>nR = HexToDec(sR)<BR>nG = HexToDec(sG)<BR>nB = HexToDec(sB)<BR><BR>Now divide each one by 2:<BR><BR>nR = nR / 2<BR>nG = nG / 2<BR>nB = nB / 2<BR><BR>Now convert each back to a hex value, making sure they&#039;re always two digits long, i.e. pad with zeros on the right if too short:<BR><BR>sR = Hex(nR)<BR>If Len(sR) &#060; 2 Then<BR>&nbsp;&nbsp;sR = "0" & sR<BR>End If<BR>sG = Hex(nG)<BR>If Len(sG) &#060; 2 Then<BR>&nbsp;&nbsp;sG = "0" & sG<BR>End If<BR>sB = Hex(nB)<BR>If Len(sB) &#060; 2 Then<BR>&nbsp;&nbsp;sB = "0" & sB<BR>End If<BR><BR>Now recombine it to the new hex colour:<BR><BR>sHex = sR & sG & sB<BR><BR>This should result in something like sHex = "123456". Now you can use that, whichever way you want.<BR><BR>Oliver.

10. Senior Member
Join Date
Dec 1969
Posts
7,686

## Couldn't you just use CDBL??

.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•