【VB.NET】 解析HTML、CSS的颜色文本为 Color 对象
在 .NET 中设置颜色非常简单,只需要
而当你想要从HTML或者CSS格式文本转换为颜色数据时,其实只需要使用
但如果是
为此,我自己写了一个可以完全解析 html css 颜色文本的方法,顺便也分享出来。
支持解析CSS格式的文本内容到
Imports
Dim c As Color = Color.FromArgb(255, 1, 2, 3)
即可设置 透明度、红、绿、蓝
等颜色信息。而当你想要从HTML或者CSS格式文本转换为颜色数据时,其实只需要使用
ColorTranslator.FromHtml(...)
这个方法就可以解析了。但如果是
rgb rgba hsl hsla
这类子函数的话,就需要自己写解析方法了。为此,我自己写了一个可以完全解析 html css 颜色文本的方法,顺便也分享出来。
支持解析CSS格式的文本内容到
Color
对象中,使用方法很简单Dim c As Color = ToColorEx("#FF00CC")
c = ToColorEx("#ccc")
c = ToColorEx("rgba(204, 232, 207, 0.5)")
c = ToColorEx("hsla(84, 91, 205, 50%)")
c = ToColorEx("red")
支持的格式有#fff
#FFFFFF
rgb(0,123,50)
rgba(0,123,50,0.5)
hsl(84,91,205)
hsla(84,91,205,50%)
red\blue\green\yellow 等常规描述性文本
Imports
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Runtime.InteropServices
Imports System.Drawing
Methods''' <summary>从字符串分析出颜色</summary>
Shared Function ToColorEx(colorText As String, Optional throwError As Boolean = False) As Color
colorText = colorText.Trim()
Dim p As String
Dim m As Match
p = "^(#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}))$"
m = Regex.Match(colorText, p)
If m.Success Then
Return ColorTranslator.FromHtml(m.Groups(1).Value)
End If
p = "^(rgba?)\s*\((.+?)\)$"
m = Regex.Match(colorText, p, RegexOptions.IgnoreCase)
If m.Success Then
Dim csn As String() = m.Groups(2).Value.Split(New String() {",", " "}, StringSplitOptions.RemoveEmptyEntries)
If csn.Length < 3 OrElse csn.Length > 4 Then
If throwError Then
Throw New Exception("数值参数设定错误(过多或过少)")
Else
Return Nothing
End If
End If
Dim ca, cr, cg, cb As Integer
ca = 255
cr = StrToByte(csn(0))
cg = StrToByte(csn(1))
cb = StrToByte(csn(2))
If m.Groups(1).Value = "rgba" AndAlso csn.Length = 4 Then
ca = StrToByte(csn(3), True)
End If
Return Color.FromArgb(ca, cr, cg, cb)
End If
p = "^(hsla?)\s*\((.+?)\)$"
m = Regex.Match(colorText, p, RegexOptions.IgnoreCase)
If m.Success Then
Dim csn As String() = m.Groups(2).Value.Split(New String() {",", " "}, StringSplitOptions.RemoveEmptyEntries)
If csn.Length < 3 OrElse csn.Length > 4 Then
If throwError Then
Throw New Exception("数值参数设定错误(过多或过少)")
Else
Return Nothing
End If
End If
' css = hsl, win = hls
Dim ca, ch, cs, cl As Integer
ca = 255
ch = StrToByte(csn(0))
cs = StrToByte(csn(1))
cl = StrToByte(csn(2))
' 利用系统转换
Dim cwin As Integer = ColorHLSToRGB(ch, cl, cs)
' 从win32数值转换为color
Dim cr As Color = ColorTranslator.FromWin32(cwin)
If m.Groups(1).Value = "hsla" AndAlso csn.Length = 4 Then
ca = StrToByte(csn(3), True)
End If
Return Color.FromArgb(ca, cr) '叠加透明度
End If
' 尝试自动解析
If throwError Then
Return ColorTranslator.FromHtml(colorText)
Else
Try
Return ColorTranslator.FromHtml(colorText)
Catch ex As Exception
Return Nothing
End Try
End If
End Function
<DllImport("shlwapi.dll")>
Private Shared Function ColorHLSToRGB(H As Integer, L As Integer, S As Integer) As Integer
End Function
Private Shared Function StrToByte(s As String, Optional isDbl As Boolean = False) As Integer
's = s.Trim
Dim p As String
Dim m As Match
Dim v As Double = -1
' 如果是浮点数
p = "^\d+\.\d+$"
m = Regex.Match(s, p)
If m.Success Then
v = CDbl(s)
End If
' 如果是百分比
p = "^(\d+)%$"
m = Regex.Match(s, p)
If m.Success Then
v = CDbl(m.Groups(1).Value) * 0.01
End If
' 强制转换为浮点数,面对设置为0或者1的情况
If isDbl Then
p = "^(\d+)$"
m = Regex.Match(s, p)
If m.Success Then
v = CDbl(s)
End If
End If
' 处理浮点数
If v >= 0 Then
If v > 1 Then v = 1
Return CInt(255 * v)
End If
Dim i As Integer
Try
i = CInt(s)
Catch ex As Exception
i = 0
End Try
If i < 0 Then i = 0
If i > 255 Then i = 255
Return i
End Function
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。