當前位置:編程學習大全網 - 源碼下載 - VB中HSL怎麽轉換為RGB?

VB中HSL怎麽轉換為RGB?

這裏有代碼 HSL和RGB互轉的 (註:HSB就是HSL,B和L都有亮度的意思)

Public?Type?HSB

Hue?As?Integer

Saturation?As?Integer

Brightness?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

把H,S,L當成參數調用,返回值就是壹個類型為“RGB”的用戶定義類型

示例:

Dim?aa?As?RGB

aa=HSB2RGB(1,?2,?3)

Print?"R:"?&?aa.Red?&?"?G:"?&?aa.Green?&?"?B:"?&?aa.Bue

回答得很晚,不知道能否幫到題主,請您設為最佳答案把 謝謝!

  • 上一篇:全國計算機等級考試二級office高級應用 多少分過了可以拿到證書?
  • 下一篇:android開發怎麽實現拍照上傳
  • copyright 2024編程學習大全網