Advertisement
jdelano

Untitled

Mar 21st, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. Private Sub btnConvert_Click()
  2.  
  3. ' convert the RGB values to HSL using the formula from
  4. ' from https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
  5.  
  6. Dim redColor As Double
  7. Dim greenColor As Double
  8. Dim blueColor As Double
  9. Dim min As Double
  10. Dim max As Double
  11. Dim luminanceValue As Double
  12. Dim saturationValue As Double
  13. Dim hueValue As Double
  14.  
  15. ' convert to 0-1
  16. redColor = Format(Val(txtR.Text) / 255, "#.#0")
  17. greenColor = Format(Val(txtG.Text) / 255, "#.#0")
  18. blueColor = Format(Val(txtB.Text) / 255, "#.#0")
  19.  
  20. ' find min and max of the three
  21. min = 2
  22. If redColor < min Then min = redColor
  23. If greenColor < min Then min = greenColor
  24. If blueColor < min Then min = blueColor
  25.  
  26. max = 0
  27. If redColor > max Then max = redColor
  28. If greenColor > max Then max = greenColor
  29. If blueColor > max Then max = blueColor
  30.  
  31. ' calculate lumanince value
  32. luminanceValue = Format((min + max) / 2, "#.#0")
  33.  
  34. ' calculate saturation value
  35. If min = max Then
  36. saturationValue = 0
  37. Else
  38. If luminanceValue <= 0.5 Then
  39. saturationValue = (max - min) / (max + min)
  40. Else
  41. saturationValue = (max - min) / (2 - max - min)
  42. End If
  43. saturationValue = Format(saturationValue, "#.#0")
  44. End If
  45.  
  46. ' calculate hue
  47. If redColor = max Then hueValue = (greenColor - blueColor) / (max - min)
  48. If greenColor = max Then hueValue = 2 + (blueColor - redColor) / (max - min)
  49. If blueColor = max Then hueValue = 4 + (redColor - greenColor) / (max - min)
  50.  
  51. If (hueValue * 60) < 0 Then hueValue = (hueValue * 60) + 360 Else hueValue = (hueValue * 60)
  52.  
  53. hueValue = Format(hueValue, "#.#0")
  54. txtH.Text = Format(hueValue, "###")
  55. txtS.Text = Format(saturationValue, "###%")
  56. txtL.Text = Format(luminanceValue, "###%")
  57.  
  58. End Sub
  59.  
  60. Private Sub txtB_Change()
  61. ChangeRGBColor
  62. End Sub
  63.  
  64. Private Sub txtG_Change()
  65. ChangeRGBColor
  66. End Sub
  67.  
  68. Private Sub txtR_Change()
  69. ChangeRGBColor
  70. End Sub
  71.  
  72. Private Sub ChangeRGBColor()
  73.  
  74. Dim redColor As Integer
  75. Dim greenColor As Integer
  76. Dim blueColor As Integer
  77.  
  78. redColor = Val(txtR.Text)
  79. greenColor = Val(txtG.Text)
  80. blueColor = Val(txtB.Text)
  81.  
  82. If redColor > 255 Then
  83. redColor = 255
  84. txtR.Text = redColor
  85. End If
  86.  
  87. If greenColor > 255 Then
  88. greenColor = 255
  89. txtG.Text = greenColor
  90. End If
  91.  
  92. If blueColor > 255 Then
  93. blueColor = 255
  94. txtB.Text = blueColor
  95. End If
  96.  
  97. lblColor.BackColor = RGB(redColor, greenColor, blueColor)
  98.  
  99. End Sub
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement