qb64-include/hsb.bm
2023-11-23 11:37:41 +01:00

48 lines
1.9 KiB
Text

$If HSB = UNDEFINED Then
$Let HSB = DEFINED
Function hsb~& (huein As Integer, satin As Integer, brightin As Integer)
Dim hue, sat, bright
Dim redout, greenout, blueout
Dim hueprimary, huesecondary
Dim satprimary, satsecondary, sattertiary
hue = huein Mod 360
If hue < 0 Then hue = hue + 360
sat = satin / 100
bright = brightin / 100
If sat > 1 Then s = 1
If bright > 1 Then b = 1
If bright <= 0 Then hsb32 = _RGB(0, 0, 0): Exit Function
If sat <= 0 Then hsb32 = _RGB(b * 255, b * 255, b * 255): Exit Function
If (hue >= 0) And (hue < 120) Then
hueprimary = 1 - (hue / 120)
huesecondary = hue / 120
satprimary = (1 - hueprimary) * (1 - sat)
satsecondary = (1 - huesecondary) * (1 - sat)
sattertiary = 1 - sat
redout = (bright * 255) * (hueprimary + satprimary)
greenout = (bright * 255) * (huesecondary + satsecondary)
blueout = (bright * 255) * sattertiary
ElseIf (hue >= 120) And (hue < 240) Then
hueprimary = 1 - ((hue - 120) / 120)
huesecondary = (hue - 120) / 120
satprimary = (1 - hueprimary) * (1 - sat)
satsecondary = (1 - huesecondary) * (1 - sat)
sattertiary = 1 - sat
redout = (bright * 255) * sattertiary
greenout = (bright * 255) * (hueprimary + satprimary)
blueout = (bright * 255) * (huesecondary + satsecondary)
ElseIf (hue >= 240) And (hue <= 360) Then
hueprimary = 1 - ((hue - 240) / 120)
huesecondary = (hue - 240) / 120
satprimary = (1 - hueprimary) * (1 - sat)
satsecondary = (1 - huesecondary) * (1 - sat)
sattertiary = 1 - sat
redout = (bright * 255) * (huesecondary + satsecondary)
greenout = (bright * 255) * sattertiary
blueout = (bright * 255) * (hueprimary + satprimary)
End If
hsb = _RGB(redout, greenout, blueout)
End Function
$End If