RGB、HSB、HSL互相转换

最近在做一个取色工具,要实现 RGB、HSB、HSL互相转换,可能你正在寻找这方面的东东,希望有所帮助。
源码用VB写的,参考:http://www.easyrgb.com/math.html,如果你想要更多其他转换算法,请参考该网站。源码如下所示:

Public Type HSB
    Hue As Integer          
    Saturation As Integer   
    Brightness As Integer   
End Type

Public Type HSL
    Hue As Integer          
    Saturation As Integer   
    Luminance As Integer   
End Type

Public Type RGB
    Red As Integer          
    Green As Integer       
    Bue As Integer
End Type

' 转换RGB到HSB
Public Function RGB2HSB(ByVal R As Integer, ByVal G As Integer, ByVal B As Integer) As HSB
    Dim nH As Single, nS As Single, nV As Single
    Dim nR As Single, nG As Single, nB As Single
    Dim ndelR As Single, ndelG As Single, ndelB As Single
    Dim nmax As Single, nmin As Single, ndelMax As Single
 
    nR = R / 255
    nG = G / 255
    nB = B / 255
    nmax = Max(Max(nR, nG), nB)
    nmin = Min(Min(nR, nG), nB)
    ndelMax = nmax - nmin
    nV = nmax
    If (ndelMax = 0) Then
        nH = 0
        nS = 0
    Else
        nS = ndelMax / nmax
        ndelR = (((nmax - nR) / 6) + (ndelMax / 2)) / ndelMax
        ndelG = (((nmax - nG) / 6) + (ndelMax / 2)) / ndelMax
        ndelB = (((nmax - nB) / 6) + (ndelMax / 2)) / ndelMax
        If (nR = nmax) Then
            nH = ndelB - ndelG
        ElseIf (nG = nmax) Then
            nH = (1 / 3) + ndelR - ndelB
        ElseIf (nB = nmax) Then
            nH = (2 / 3) + ndelG - ndelR
        End If
        If (nH < 0) Then nH = nH + 1
        If (nH > 1) Then nH = nH - 1
    End If
    RGB2HSB.Hue = nH * 360
    RGB2HSB.Saturation = nS * 100
    RGB2HSB.Brightness = nV * 100
   
End Function

' 转换HSB到RGB
Public Function HSB2RGB(ByVal H As Integer, ByVal S As Integer, ByVal B As Integer) As RGB
    Dim nH As Single, nS As Single, nV As Single
    Dim nR As Single, nG As Single, nB As Single
    Dim hi As Single, f As Single, p As Single, q As Single, t As Single
   
    nH = H / 360
    nS = S / 100
    nV = B / 100
    If (S = 0) Then
        nR = nV * 255
        nG = nV * 255
        nB = nV * 255
    Else
        hi = nH * 6
        If (hi = 6) Then hi = 0
        f = Int(hi)
        p = nV * (1 - nS)
        q = nV * (1 - nS * (hi - f))
        t = nV * (1 - nS * (1 - (hi - f)))

        If (f = 0) Then
            nR = nV
            nG = t
            nB = p
        ElseIf (f = 1) Then
            nR = q
            nG = nV
            nB = p
        ElseIf (f = 2) Then
            nR = p
            nG = nV
            nB = t
        ElseIf (f = 3) Then
            nR = p
            nG = q
            nB = nV
        ElseIf (f = 4) Then
            nR = t
            nG = p
            nB = nV
        Else
            nR = nV
            nG = p
            nB = q
        End If
    End If
    HSB2RGB.Red = nR * 255
    HSB2RGB.Green = nG * 255
    HSB2RGB.Bue = nB * 255
   
End Function

' 转换RGB到HSL
Public Function RGB2HSL(ByVal R As Integer, ByVal G As Integer, ByVal B As Integer) As HSL
    Dim nH As Single, nS As Single, nL As Single
    Dim nR As Single, nG As Single, nB As Single
    Dim ndelR As Single, ndelG As Single, ndelB As Single
    Dim nmax As Single, nmin As Single, ndelMax As Single
   
    nR = (R / 255)
    nG = (G / 255)
    nB = (B / 255)
    nmax = Max(Max(nR, nG), nB)
    nmin = Min(Min(nR, nG), nB)
    ndelMax = nmax - nmin
    nL = (nmax + nmin) / 2
   
    If (ndelMax = 0) Then
        nH = 0
        nS = 0
    Else
        If (nL < 0.5) Then
            nS = ndelMax / (nmax + nmin)
        Else
            nS = ndelMax / (2 - nmax - nmin)
        End If
        ndelR = (((nmax - nR) / 6) + (ndelMax / 2)) / ndelMax
        ndelG = (((nmax - nG) / 6) + (ndelMax / 2)) / ndelMax
        ndelB = (((nmax - nB) / 6) + (ndelMax / 2)) / ndelMax
        If (nR = nmax) Then
            nH = ndelB - ndelG
        ElseIf (nG = nmax) Then
            nH = (1 / 3) + ndelR - ndelB
        ElseIf (nB = nmax) Then
            nH = (2 / 3) + ndelG - ndelR
        End If
        If (nH < 0) Then nH = nH + 1
        If (nH > 1) Then nH = nH - 1
    End If
    RGB2HSL.Hue = nH * 240
    RGB2HSL.Saturation = nS * 240
    RGB2HSL.Luminance = nL * 240
       
End Function

' 转换HSL到RGB
Public Function HSL2RGB(ByVal H As Integer, ByVal S As Integer, ByVal L As Integer) As RGB
    Dim nH As Single, nS As Single, nL As Single
    Dim nR As Single, nG As Single, nB As Single
    Dim p1 As Single, p2 As Single
   
    nH = H / 240
    nS = S / 240
    nL = L / 240
    If (nS = 0) Then
        nR = nL * 255
        nG = nL * 255
        nB = nL * 255
    Else
        If (nL < 0.5) Then
            p2 = Round(nL * (1 + nS), 2)
        Else
            p2 = Round((nL + nS) - (nS * nL), 2)
        End If
        p1 = Round(2 * nL - p2, 2)
        nR = 255 * Hue2RGB(p1, p2, nH + (1 / 3))
        nG = 255 * Hue2RGB(p1, p2, nH)
        nB = 255 * Hue2RGB(p1, p2, nH - (1 / 3))
    End If
    HSL2RGB.Red = nR
    HSL2RGB.Green = nG
    HSL2RGB.Bue = nB
   
End Function

Private Function Hue2RGB(ByVal p1 As Single, ByVal p2 As Single, ByVal Hue As Single) As Single
   
    If (Hue < 0) Then Hue = Hue + 1
    If (Hue > 1) Then Hue = Hue - 1
    If ((6 * Hue) < 1) Then
        Hue2RGB = (p1 + (p2 - p1) * 6 * Hue)
    ElseIf ((2 * Hue) < 1) Then
        Hue2RGB = p2
    ElseIf ((3 * Hue) < 2) Then
        Hue2RGB = p1 + (p2 - p1) * ((2 / 3) - Hue) * 6
    Else
        Hue2RGB = p1
    End If
   
End Function
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RGB(Red, Green, Blue)和HSB(Hue, Saturation, Brightness)都是常用的颜色模型。RGB模型是基于颜色的三个基本分量,即红色、绿色和蓝色,而HSB模型则是基于颜色的色相、饱和度和亮度。 RGB转换HSB的数学原理如下: 1. 首先需要将RGB转换为0-1的标准化值。假设原始颜色的RGB值为(r, g, b),则对应的标准化值为: r' = r / 255 g' = g / 255 b' = b / 255 2. 计算色相H。色相是指颜色在色轮上的位置,取值范围为0-360度。具体计算方式如下: - 如果最大值和最小值相等,那么色相H为0。 - 如果最大值是红色分量r,那么色相H的计算公式为:H = (g' - b') / (max(r', g', b') - min(r', g', b')) * 60 - 如果最大值是绿色分量g,那么色相H的计算公式为:H = ((b' - r') / (max(r', g', b') - min(r', g', b')) + 2) * 60 - 如果最大值是蓝色分量b,那么色相H的计算公式为:H = ((r' - g') / (max(r', g', b') - min(r', g', b')) + 4) * 60 需要注意的是,由于色相的取值范围是0-360度,所以如果计算出来的H值小于0,需要加上360度。 3. 计算饱和度S。饱和度是指颜色的纯度,取值范围为0-100%。具体计算方式如下: S = (max(r', g', b') - min(r', g', b')) / max(r', g', b') * 100 4. 计算亮度B。亮度是指颜色的明亮程度,取值范围为0-100%。具体计算方式可以参考我之前回答问题中的内容。 通过以上计算,可以得到HSB模型中的色相H、饱和度S和亮度B值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值