首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP色差的计算

ASP色差的计算
EN

Stack Overflow用户
提问于 2013-02-26 18:43:52
回答 1查看 254关注 0票数 0

我找到了用于PHP和Javascript的代码片段,但我想知道它是否可以在经典的asp中使用?这里有一整篇关于这个主题的文章,供大家参考。

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

PHP代码

代码语言:javascript
复制
function getContrast50($hexcolor){
    return (hexdec($hexcolor) > 0xffffff/2) ? 'black':'white';
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-27 08:26:24

语言里没有内置的东西。将十六进制转换为十进制和CLng("&H" & hexValue)一样简单,但是在PHP手册中快速查看时,我发现hexdec()方法忽略了任何无效字符,而VBScript CLng()只会崩溃。

因此,这里有一个工作函数,据我所知,做同样的事情:

代码语言:javascript
复制
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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15096848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档