0

PHP と Javascript のこのコード スニペットを見つけましたが、従来の ASP で動作させることができるかどうか疑問に思っていましたか? 参照用のトピックに関する記事全体を次に示します。

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

PHP コード

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

1 に答える 1

1

まあ、言語には何も組み込まれていません。16 進数を 10 進数に変換するのはCLng("&H" & hexValue)簡単ですが、PHP のマニュアルをざっと見てみると、hexdec()メソッドが無効な文字を無視し、VBScriptCLng()がクラッシュするだけであることがわかりました。

したがって、これは私が知る限り、同じことを行う機能です。

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

検証を拡張して、指定された文字列が有効な範囲内にあるかどうかを確認するのは非常に簡単です。

于 2013-02-27T08:26:24.817 に答える