【问题标题】:Calculating Colour Contrast for ASP计算 ASP 的颜色对比度
【发布时间】:2013-02-26 18:43:52
【问题描述】:

我为 PHP 和 Javascript 找到了这段代码 sn-p,但我想知道它是否可以在经典 asp 中工作?这是有关该主题的整篇文章以供参考。

http://24ways.org/2010/calculating-color-contrast/

PHP 代码

function getContrast50($hexcolor){
    return (hexdec($hexcolor) > 0xffffff/2) ? 'black':'white';
}

【问题讨论】:

    标签: php asp-classic colors


    【解决方案1】:

    好吧,语言中没有内置任何内容。将十六进制转换为十进制就像 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
    

    扩展验证以检查给定字符串是否在有效范围内非常容易。

    【讨论】:

    • 我注意到它正在执行的计算存在问题。使用像#FFE6E6(浅粉色)这样的值,我最终会返回白色,它太亮而无法显示白色文本。我不确定计算是否有问题。
    • @jamEs 哦,天哪,对不起!由于我的代码中的 n00bish 错误,“#FFE6E6”实际上被视为“66”,因为大写字母被认为是非法字符。现在修复它,看看我的编辑。
    • 我想补充一点,我对 maxValue "If decValue > (maxValue / 1.3) Then" 做了这个调整,所以它会比它更早地翻转为白色值。我发现在某些值上,黑色文本的值与它所在的背景有点太接近了,因此调整使它的功能更好一些。
    猜你喜欢
    • 2013-01-28
    • 2023-03-14
    • 2019-02-05
    • 1970-01-01
    • 2011-10-19
    • 2012-04-01
    • 1970-01-01
    • 2011-01-23
    相关资源
    最近更新 更多