日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java rgb hsl_RGB、HSB、HSL 互相转换算法

發布時間:2025/4/5 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java rgb hsl_RGB、HSB、HSL 互相转换算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Public TypeHSB

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 TypeRGB

Red As Integer

Green As Integer

Bue As Integer

End Type

' 轉換RGB到HSB

Public FunctionRGB2HSB(ByVal R As Integer, ByVal G As Integer, ByVal B As Integer) AsHSB

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 FunctionHSB2RGB(ByVal H As Integer, ByVal S As Integer, ByVal B As Integer) AsRGB

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 FunctionRGB2HSL(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) AsRGB

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

總結

以上是生活随笔為你收集整理的java rgb hsl_RGB、HSB、HSL 互相转换算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。