As requested, here are my versions.
I converted these from C code I found in a Foley & Van Dam graphics book. However, the original C code uses double precision floats - which we don't have - so there will probably be a small amount of inaccuracy sometimes. Not much I can do about that. :)
I converted these from C code I found in a Foley & Van Dam graphics book. However, the original C code uses double precision floats - which we don't have - so there will probably be a small amount of inaccuracy sometimes. Not much I can do about that. :)
; RGB/HSL function return values. Global result_r#, result_g#, result_b# Global result_h#, result_s#, result_l# ; ; Converts an RGB color to HSL. ; ; Params: ; ir,ig,ib - Color's red, green and blue components (0-255). ; ; Returns: ; The converted color's HSL values via the following globals: ; result_h - Hue component (0-360). ; result_s - Saturation component (0-1). ; result_l - Luminance component (0-1). ; Function rgb_to_hsl(ir%, ig%, ib%) ; Scale RGB down to unit range (0-1). r# = ir/255.0 g# = ig/255.0 b# = ib/255.0 If r > g max_is = 0 max_color# = r min_color# = g Else max_is = 1 max_color# = g min_color# = r EndIf If b > max_color max_is = 2 max_color = b ElseIf b < min_color min_color = b EndIf ; Luminance. result_l = (max_color + min_color) / 2.0 If max_color = min_color ; Color is grey. result_s = 0 ;result_h = 0 Else delta# = (max_color - min_color) ; Saturation. If result_l < 0.5 result_s = delta / (max_color + min_color) Else result_s = delta / (2.0 - max_color - min_color) EndIf ; Hue. Select max_is Case 0 ; Red. result_h = (g - b) / delta Case 1 ; Green. result_h = 2.0 + (b - r) / delta Case 2 ; Blue. result_h = 4.0 + (r - g) / delta End Select result_h = result_h * 60.0 If result_h < 0 Then result_h = result_h + 360.0 EndIf End Function ; ; Converts an HSL color to RGB. ; ; Params: ; h,s,l - Color's hue(0-360), luminance(0-1) and saturation(0-1) components. ; ; Returns: ; The converted color's RGB values via the following globals: ; result_r - Hue component (0-255). ; result_g - Saturation component (0-255). ; result_b - Luminance component (0-255). ; Function hsl_to_rgb(h#, s#, l#) If s = 0 result_r# = l result_g# = l result_b# = l Else If l < 0.5 temp2# = l * (1.0 + s) Else temp2# = (l + s) - (l * s) EndIf temp1# = 2.0 * l - temp2 h = h / 360.0 rtemp3# = h + 1.0 / 3.0 If rtemp3 < 0 rtemp3 = rtemp3 + 1.0 ElseIf rtemp3 > 1 rtemp3 = rtemp3 - 1.0 EndIf gtemp3# = h If gtemp3 < 0 gtemp3 = gtemp3 + 1.0 ElseIf gtemp3 > 1 gtemp3 = gtemp3 - 1.0 EndIf btemp3# = h - 1.0 / 3.0 If btemp3 < 0 btemp3 = btemp3 + 1.0 ElseIf btemp3 > 1 btemp3 = btemp3 - 1.0 EndIf ; Set red. If (6.0 * rtemp3) < 1 result_r# = temp1 + (temp2 - temp1) * 6.0 * rtemp3 ElseIf (2.0 * rtemp3) < 1 result_r# = temp2 ElseIf (3.0 * rtemp3) < 2 result_r# = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - rtemp3) * 6.0 Else result_r# = temp1 EndIf ; Set green. If (6.0 * gtemp3) < 1 result_g# = temp1 + (temp2 - temp1) * 6.0 * gtemp3 ElseIf (2.0 * gtemp3) < 1 result_g# = temp2 ElseIf (3.0 * gtemp3) < 2 result_g# = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - gtemp3) * 6.0 Else result_g# = temp1 EndIf ; Set blue. If (6.0 * btemp3) < 1 result_b# = temp1 + (temp2 - temp1) * 6.0 * btemp3 ElseIf (2.0 * btemp3) < 1 result_b# = temp2 ElseIf (3.0 * btemp3) < 2 result_b# = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - btemp3) * 6.0 Else result_b# = temp1 EndIf EndIf ; Scale RGB back to 0-255 range. ; Remove this if you want to keep RGB in the range 0-1! result_r = result_r * 255 result_g = result_g * 255 result_b = result_b * 255 End Function