Forum Moderators: open
Users can configure certain elements within pages and one of these is the color of rows returned in a dataset. I'd like to automatically be able to figure out what to set the TEXT color in that row to make it contrast well.
The conversion functions are largely from different sites where I found the information, but here's something that works with their help. The basic idea is convert hex to binary, flip the binary digits, then convert back to hex.
Hope this helps..
<%@ Language="VBScript" %>
<% Option Explicit %>
<%
dim originalColor
dim oppositeColor
originalColor = "000080"
Response.Write "Original Color: " & originalColor & "<br>"
oppositeColor = GetOppositeHexValue(originalColor)
Response.Write "Opposite Color: " & oppositecolor
Function GetOppositeHexValue(color)
dim part1, part2, part3, part4, part5, part6
part1 = Mid(color, 1,1)
part2 = Mid(color, 2,1)
part3 = Mid(color, 3,1)
part4 = Mid(color, 4,1)
part5 = Mid(color, 5,1)
part6 = Mid(color, 6,1)
part1 = Hex(BinToDec(FlipBits(DecToBin(HexToDec(part1)))))
part2 = Hex(BinToDec(FlipBits(DecToBin(HexToDec(part2)))))
part3 = Hex(BinToDec(FlipBits(DecToBin(HexToDec(part3)))))
part4 = Hex(BinToDec(FlipBits(DecToBin(HexToDec(part4)))))
part5 = Hex(BinToDec(FlipBits(DecToBin(HexToDec(part5)))))
part6 = Hex(BinToDec(FlipBits(DecToBin(HexToDec(part6)))))
GetOppositeHexValue = part1 & part2 & part3 & part4 & part5 & part6
End Function
Function FlipBits(num)
Dim out, x
out = ""
For x = 1 to Len(num)
If Mid(num, x, 1) = 0 Then
out = out & 1
Else
out = out & 0
End If
Next
FlipBits = out
End Function
Function DecToBin(ByVal d)
Dim st
st = ""
Do While d > 0
If d Mod 2 > 0 Then
st = "1" & st
Else
st = "0" & st
End If
d = Int(d / 2)
Loop
DecToBin = Right("0000" & st, 4)
End Function
Function HexToDec(strHex)
dim lngResult
dim intIndex
dim strDigit
dim intDigit
dim intValue
lngResult = 0
for intIndex = len(strHex) to 1 step -1
strDigit = mid(strHex, intIndex, 1)
intDigit = instr("0123456789ABCDEF", ucase(strDigit))-1
if intDigit >= 0 then
intValue = intDigit * (16 ^ (len(strHex)-intIndex))
lngResult = lngResult + intValue
else
lngResult = 0
intIndex = 0 ' stop the loop
end if
next
HexToDec = lngResult
End Function
Function BinToDec(strBin)
dim lngResult
dim intIndex
dim strDigit
lngResult = 0
for intIndex = len(strBin) to 1 step -1
strDigit = mid(strBin, intIndex, 1)
select case strDigit
case "0"
' do nothing
case "1"
lngResult = lngResult + (2 ^ (len(strBin)-intIndex))
case else
' invalid binary digit, so the whole thing is invalid
lngResult = 0
intIndex = 0 ' stop the loop
end select
next
BinToDec = lngResult
End Function
%>
<html>
<head>
<title>Opposite Colors</title>
</head>
<body>
<table>
<tr>
<td bgcolor="<% = "#" & originalColor %>">
<font size="5" color="<% = "#" & oppositeColor %>">original</font>
</td>
</tr>
<tr>
<td bgcolor="<% = "#" & oppositeColor %>">
<font size="5" color="<% = "#" & originalColor %>">opposite</font>
</td>
</tr>
</table>
</body>
</html>
There's probably an easier/cleaner way to do it, but this should get you started in the right direction.
-Mark