我找到了用于PHP和Javascript的代码片段,但我想知道它是否可以在经典的asp中使用?这里有一整篇关于这个主题的文章,供大家参考。
http://24ways.org/2010/calculating-color-contrast/
PHP代码
function getContrast50($hexcolor){
return (hexdec($hexcolor) > 0xffffff/2) ? 'black':'white';
}发布于 2013-02-27 08:26:24
语言里没有内置的东西。将十六进制转换为十进制和CLng("&H" & hexValue)一样简单,但是在PHP手册中快速查看时,我发现hexdec()方法忽略了任何无效字符,而VBScript CLng()只会崩溃。
因此,这里有一个工作函数,据我所知,做同样的事情:
Function GetContrast50(hexColor)
Const strValidChars = "1234567890abcdef"
Dim maxValue, decValue, sanitizedColor
Dim x, curChar
sanitizedColor = ""
For x=1 To Len(hexColor)
curChar = LCase(Mid(hexColor, x, 1))
If InStr(strValidChars, curChar)>0 Then
sanitizedColor = sanitizedColor & curChar
End If
Next
If Len(sanitizedColor)=0 Then
GetContrast50 = "invalid color string"
Exit Function
End If
maxValue = CLng("&H" & "ffffff")
decValue = CLng("&H" & sanitizedColor)
If decValue > (maxValue / 2) Then
GetContrast50 = "black"
Else
GetContrast50 = "white"
End If
End Function很容易扩展验证以检查给定的字符串是否在有效范围内。
https://stackoverflow.com/questions/15096848
复制相似问题