Forum Moderators: open
I have a web app that is served remotely and lets the remote webmaster specify a background color and I need to automatically determince an appropriate forground color which can be used for text.
For example, if the webmaster specifies #FFFFFF for the background color, the asp page should decide something like #000000 for a good color to use to display text.
Any suggestions?
load all of the colors into a table then then assign an opposite color that makes sense to a human in that table? You'll have to load it once but after that it should be fine...
then just have your app go to the db for the compliment.
actual_color ¦ compliment_color
green ¦ white
have you tried a translation table?
Well, looking up the color in a table is easy enough, but where would I get the data for the table? 000000 - FFFFFF is 16 million colors, though they could probably be rounded down to 255 different colors I still wouldn't know where to start finding good contrasts, besides doing it manually. I'll have to think that over..
Hmmm. I just don't feel like thinking about this right now.
usage is: /ColorContrast.asp?oColor=3399FF
There is NO error checking.. I'll leave that to you.
------ColorContrast.asp--------------------
<%@ Language="VBScript" %>
<% Option Explicit %>
<%dim originalColor
dim oppositeColor
originalColor = "0000FF"
If(Len(Request.QueryString("oColor")) > 0 ) Then
originalColor = Request.QueryString("oColor")
End If
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>
HTH,
Mark
Though this does fall apart (as ASPDaddy mentioned)when converging toward the median values... like #787878, which maps out to #878787... the two colors are still distinguishable, but not really contrasting.. perhaps as was suggested earlier, a color shift in addition to this 'bit-flipping' would remedy this ... or you could simply disallow a certain range of values in the mid range...
Mark