まあ、言語には何も組み込まれていません。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
検証を拡張して、指定された文字列が有効な範囲内にあるかどうかを確認するのは非常に簡単です。