• 按键公众号 :
按键精灵电脑版
立即下载

软件版本:2014.06
软件大小:22.9M
更新时间:2021-12-03

按键精灵安卓版
立即下载

软件版本:3.7.2
软件大小:46.2M
更新时间:2023-05-10

按键精灵iOS版
立即下载

软件版本:1.8.0
软件大小:29.2M
更新时间:2023-03-21

按键手机助手
立即下载

软件版本:3.8.0
软件大小:262M
更新时间:2023-05-30

快捷导航

登录 后使用快捷导航
没有帐号? 注册

发新话题 回复该主题

【VB源码】RGB、HSB、HSL互相转换 [复制链接]

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

  1. Public Type HSB
  2. Hue As Integer
  3. Saturation As Integer
  4. Brightness As Integer
  5. End Type
  6. Public Type HSL
  7. Hue As Integer
  8. Saturation As Integer
  9. Luminance As Integer
  10. End Type
  11. Public Type RGB
  12. Red As Integer
  13. Green As Integer
  14. Bue As Integer
  15. End Type
  16. ' 转换RGB到HSB
  17. Public Function RGB2HSB(ByVal R As Integer, ByVal G As Integer, ByVal B As Integer) As HSB
  18. Dim nH As Single, nS As Single, nV As Single
  19. Dim nR As Single, nG As Single, nB As Single
  20. Dim ndelR As Single, ndelG As Single, ndelB As Single
  21. Dim nmax As Single, nmin As Single, ndelMax As Single

  22. nR = R / 255
  23. nG = G / 255
  24. nB = B / 255
  25. nmax = Max(Max(nR, nG), nB)
  26. nmin = Min(Min(nR, nG), nB)
  27. ndelMax = nmax - nmin
  28. nV = nmax
  29. If (ndelMax = 0) Then
  30. nH = 0
  31. nS = 0

  32. Else
  33. nS = ndelMax / nmax
  34. ndelR = (((nmax - nR) / 6) + (ndelMax / 2)) / ndelMax
  35. ndelG = (((nmax - nG) / 6) + (ndelMax / 2)) / ndelMax
  36. ndelB = (((nmax - nB) / 6) + (ndelMax / 2)) / ndelMax
  37. If (nR = nmax) Then
  38. nH = ndelB - ndelG
  39. ElseIf (nG = nmax) Then
  40. nH = (1 / 3) + ndelR - ndelB
  41. ElseIf (nB = nmax) Then
  42. nH = (2 / 3) + ndelG - ndelR
  43. End If
  44. If (nH < 0) Then nH = nH + 1
  45. If (nH > 1) Then nH = nH - 1

  46. End If
  47. RGB2HSB.Hue = nH * 360
  48. RGB2HSB.Saturation = nS * 100
  49. RGB2HSB.Brightness = nV * 100

  50. End Function
  51. ' 转换HSB到RGB
  52. Public Function HSB2RGB(ByVal H As Integer, ByVal S As Integer, ByVal B As Integer) As RGB
  53. Dim nH As Single, nS As Single, nV As Single
  54. Dim nR As Single, nG As Single, nB As Single
  55. Dim hi As Single, f As Single, p As Single, q As Single, t As Single

  56. nH = H / 360
  57. nS = S / 100
  58. nV = B / 100
  59. If (S = 0) Then
  60. nR = nV * 255
  61. nG = nV * 255
  62. nB = nV * 255
  63. Else
  64. hi = nH * 6
  65. If (hi = 6) Then hi = 0
  66. f = Int(hi)
  67. p = nV * (1 - nS)
  68. q = nV * (1 - nS * (hi - f))
  69. t = nV * (1 - nS * (1 - (hi - f)))
  70. If (f = 0) Then
  71. nR = nV
  72. nG = t
  73. nB = p
  74. ElseIf (f = 1) Then
  75. nR = q
  76. nG = nV
  77. nB = p
  78. ElseIf (f = 2) Then
  79. nR = p
  80. nG = nV
  81. nB = t
  82. ElseIf (f = 3) Then
  83. nR = p
  84. nG = q
  85. nB = nV
  86. ElseIf (f = 4) Then
  87. nR = t
  88. nG = p
  89. nB = nV
  90. Else
  91. nR = nV
  92. nG = p
  93. nB = q
  94. End If
  95. End If
  96. HSB2RGB.Red = nR * 255
  97. HSB2RGB.Green = nG * 255
  98. HSB2RGB.Bue = nB * 255

  99. End Function
复制代码
' 转换RGB到HSL


  1. Public Function RGB2HSL(ByVal R As Integer, ByVal G As Integer, ByVal B As Integer) As HSL
  2. Dim nH As Single, nS As Single, nL As Single
  3. Dim nR As Single, nG As Single, nB As Single
  4. Dim ndelR As Single, ndelG As Single, ndelB As Single
  5. Dim nmax As Single, nmin As Single, ndelMax As Single

  6. nR = (R / 255)
  7. nG = (G / 255)
  8. nB = (B / 255)
  9. nmax = Max(Max(nR, nG), nB)
  10. nmin = Min(Min(nR, nG), nB)
  11. ndelMax = nmax - nmin
  12. nL = (nmax + nmin) / 2

  13. If (ndelMax = 0) Then
  14. nH = 0
  15. nS = 0
  16. RGB2HSL.Hue = 160'这句是我添加修改的
  17. Else
  18. If (nL < 0.5) Then
  19. nS = ndelMax / (nmax + nmin)
  20. Else
  21. nS = ndelMax / (2 - nmax - nmin)
  22. End If
  23. ndelR = (((nmax - nR) / 6) + (ndelMax / 2)) / ndelMax
  24. ndelG = (((nmax - nG) / 6) + (ndelMax / 2)) / ndelMax
  25. ndelB = (((nmax - nB) / 6) + (ndelMax / 2)) / ndelMax
  26. If (nR = nmax) Then
  27. nH = ndelB - ndelG
  28. ElseIf (nG = nmax) Then
  29. nH = (1 / 3) + ndelR - ndelB
  30. ElseIf (nB = nmax) Then
  31. nH = (2 / 3) + ndelG - ndelR
  32. End If
  33. If (nH < 0) Then nH = nH + 1
  34. If (nH > 1) Then nH = nH - 1
  35. RGB2HSL.Hue = nH * 240 '是我添加修改的
  36. End If

  37. 'RGB2HSL.Hue = nH * 240 '这句可以废除,不符合微软算法
  38. RGB2HSL.Saturation = nS * 240
  39. RGB2HSL.Luminance = nL * 240

  40. End Function
  41. ' 转换HSL到RGB
  42. Public Function HSL2RGB(ByVal H As Integer, ByVal S As Integer, ByVal L As Integer) As RGB
  43. Dim nH As Single, nS As Single, nL As Single
  44. Dim nR As Single, nG As Single, nB As Single
  45. Dim p1 As Single, p2 As Single

  46. nH = H / 240
  47. nS = S / 240
  48. nL = L / 240
  49. If (nS = 0) Then
  50. nR = nL * 255
  51. nG = nL * 255
  52. nB = nL * 255
  53. Else
  54. If (nL < 0.5) Then
  55. p2 = Round(nL * (1 + nS), 2)
  56. Else
  57. p2 = Round((nL + nS) - (nS * nL), 2)
  58. End If
  59. p1 = Round(2 * nL - p2, 2)
  60. nR = 255 * Hue2RGB(p1, p2, nH + (1 / 3))
  61. nG = 255 * Hue2RGB(p1, p2, nH)
  62. nB = 255 * Hue2RGB(p1, p2, nH - (1 / 3))
  63. End If
  64. HSL2RGB.Red = nR
  65. HSL2RGB.Green = nG
  66. HSL2RGB.Bue = nB

  67. End Function
复制代码

  1. Private Function Hue2RGB(ByVal p1 As Single, ByVal p2 As Single, ByVal Hue As Single) As Single

  2. If (Hue < 0) Then Hue = Hue + 1
  3. If (Hue > 1) Then Hue = Hue - 1
  4. If ((6 * Hue) < 1) Then
  5. Hue2RGB = (p1 + (p2 - p1) * 6 * Hue)
  6. ElseIf ((2 * Hue) < 1) Then
  7. Hue2RGB = p2
  8. ElseIf ((3 * Hue) < 2) Then
  9. Hue2RGB = p1 + (p2 - p1) * ((2 / 3) - Hue) * 6
  10. Else
  11. Hue2RGB = p1
  12. End If

  13. End Function
复制代码

    已有1评分我要评分查看所有评分

    2#

    好东西

    3#

    学习下啊学习

    4#

    。。。。。。。。。。。。。。。

    5#

    好東西要跟好朋友分享!

    6#

    】RGB、HSB、HSL互相转换修改

    7#

    222222

    8#


    9#


    发新话题 回复该主题