RGB、HSB、HSL 互相转换算法

简介: 引用:http://hi.baidu.com/zhjw8086/item/1fcf680d03fbb28b02ce1b45 Public Type HSB    Hue As Integer               Saturation As Integer        Brightness...

引用:http://hi.baidu.com/zhjw8086/item/1fcf680d03fbb28b02ce1b45

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

' 转换RGBHSB
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

' 转换HSBRGB
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

相关文章
|
5月前
|
编解码 算法 图形学
同一路RTSP|RTMP流如何同时回调YUV和RGB数据实现渲染和算法分析
我们播放RTSP|RTMP流,如果需要同时做渲染和算法分析的话,特别是渲染在上层实现(比如Unity),算法是python这种情况,拉两路流,更耗费带宽和性能,拉一路流,同时回调YUV和RGB数据也可以,但是更灵活的是本文提到的按需转算法期望的RGB数据,然后做算法处理
|
6月前
|
编解码 算法 Linux
Linux平台下RTSP|RTMP播放器如何跟python交互投递RGB数据供视觉算法分析
在对接Linux平台的RTSP播放模块时,需将播放数据同时提供给Python进行视觉算法分析。技术实现上,可在播放时通过回调函数获取视频帧数据,并以RGB32格式输出。利用`SetVideoFrameCallBackV2`接口设定缩放后的视频帧回调,以满足算法所需的分辨率。回调函数中,每收到一帧数据即保存为bitmap文件。Python端只需读取指定文件夹中的bitmap文件,即可进行视频数据的分析处理。此方案简单有效,但应注意控制输出的bitmap文件数量以避免内存占用过高。
101 3
|
8月前
|
机器学习/深度学习 算法
五种基于RGB色彩空间统计的皮肤检测算法
五种基于RGB色彩空间统计的皮肤检测算法
59 0
|
分布式计算 算法 异构计算
m基于FPGA的RGB转ycrcb颜色空间转换算法实现,包含testbench,对比三种转换方法
m基于FPGA的RGB转ycrcb颜色空间转换算法实现,包含testbench,对比三种转换方法
230 1
ML之SSIM:基于输入图片RGB的三维向量利用SSIM(结构相似性度量)算法进行判别
ML之SSIM:基于输入图片RGB的三维向量利用SSIM(结构相似性度量)算法进行判别
ML之SSIM:基于输入图片RGB的三维向量利用SSIM(结构相似性度量)算法进行判别
|
算法 计算机视觉
ML之Cosin:基于输入图片RGB均值化转为单向vector利用Cosin(余弦相似度)算法进行判别
ML之Cosin:基于输入图片RGB均值化转为单向vector利用Cosin(余弦相似度)算法进行判别
ML之Cosin:基于输入图片RGB均值化转为单向vector利用Cosin(余弦相似度)算法进行判别
|
算法 计算机视觉
ML之Hog_HammingDistance:基于Hog特征提取“RGB”图像的768个值的单向vector利用汉明距离算法进行判别
ML之Hog_HammingDistance:基于Hog特征提取“RGB”图像的768个值的单向vector利用汉明距离算法进行判别
ML之Hog_HammingDistance:基于Hog特征提取“RGB”图像的768个值的单向vector利用汉明距离算法进行判别
|
算法 数据可视化 前端开发
记一次HEX和RGB互换算法的思考及应用
由于笔者最近在开发可视化平台,所以对动态编辑器这块做了一段时间的研究, 发现其中有个小模块的知识点比较有意思,所以在这里分享一下.
182 0
ML之SSIM:基于输入图片RGB的三维向量利用SSIM(结构相似性度量)算法进行判别
ML之SSIM:基于输入图片RGB的三维向量利用SSIM(结构相似性度量)算法进行判别
ML之SSIM:基于输入图片RGB的三维向量利用SSIM(结构相似性度量)算法进行判别
ML之Cosin:基于输入图片RGB均值化转为单向vector利用Cosin(余弦相似度)算法进行判别
ML之Cosin:基于输入图片RGB均值化转为单向vector利用Cosin(余弦相似度)算法进行判别
ML之Cosin:基于输入图片RGB均值化转为单向vector利用Cosin(余弦相似度)算法进行判别