zoukankan      html  css  js  c++  java
  • RGB、HSB、HSL 互相转换算法

    引用: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

  • 相关阅读:
    在VMWare中增加Linux文件系统空间
    linux shell 字符串操作(长度,查找,替换)详解
    linux chmod命令参数及用法详解文件文件夹权限设定命令
    Linux分割日志计划任务(原创)
    写日志C#程序
    2011年底,数家大型网站数据库被窃取分析报告(原创)
    ThinkPad SL400全驱动
    东辰信竞学子——从今天开始重新出发!
    CentOS7下安装mysql8027
    arcgis基础
  • 原文地址:https://www.cnblogs.com/sode/p/2892944.html
Copyright © 2011-2022 走看看