Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub btnConvert_Click()
- ' convert the RGB values to HSL using the formula from
- ' from https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
- Dim redColor As Double
- Dim greenColor As Double
- Dim blueColor As Double
- Dim min As Double
- Dim max As Double
- Dim luminanceValue As Double
- Dim saturationValue As Double
- Dim hueValue As Double
- ' convert to 0-1
- redColor = Format(Val(txtR.Text) / 255, "#.#0")
- greenColor = Format(Val(txtG.Text) / 255, "#.#0")
- blueColor = Format(Val(txtB.Text) / 255, "#.#0")
- ' find min and max of the three
- min = 2
- If redColor < min Then min = redColor
- If greenColor < min Then min = greenColor
- If blueColor < min Then min = blueColor
- max = 0
- If redColor > max Then max = redColor
- If greenColor > max Then max = greenColor
- If blueColor > max Then max = blueColor
- ' calculate lumanince value
- luminanceValue = Format((min + max) / 2, "#.#0")
- ' calculate saturation value
- If min = max Then
- saturationValue = 0
- Else
- If luminanceValue <= 0.5 Then
- saturationValue = (max - min) / (max + min)
- Else
- saturationValue = (max - min) / (2 - max - min)
- End If
- saturationValue = Format(saturationValue, "#.#0")
- End If
- ' calculate hue
- If redColor = max Then hueValue = (greenColor - blueColor) / (max - min)
- If greenColor = max Then hueValue = 2 + (blueColor - redColor) / (max - min)
- If blueColor = max Then hueValue = 4 + (redColor - greenColor) / (max - min)
- If (hueValue * 60) < 0 Then hueValue = (hueValue * 60) + 360 Else hueValue = (hueValue * 60)
- hueValue = Format(hueValue, "#.#0")
- txtH.Text = Format(hueValue, "###")
- txtS.Text = Format(saturationValue, "###%")
- txtL.Text = Format(luminanceValue, "###%")
- End Sub
- Private Sub txtB_Change()
- ChangeRGBColor
- End Sub
- Private Sub txtG_Change()
- ChangeRGBColor
- End Sub
- Private Sub txtR_Change()
- ChangeRGBColor
- End Sub
- Private Sub ChangeRGBColor()
- Dim redColor As Integer
- Dim greenColor As Integer
- Dim blueColor As Integer
- redColor = Val(txtR.Text)
- greenColor = Val(txtG.Text)
- blueColor = Val(txtB.Text)
- If redColor > 255 Then
- redColor = 255
- txtR.Text = redColor
- End If
- If greenColor > 255 Then
- greenColor = 255
- txtG.Text = greenColor
- End If
- If blueColor > 255 Then
- blueColor = 255
- txtB.Text = blueColor
- End If
- lblColor.BackColor = RGB(redColor, greenColor, blueColor)
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement