TGC Codebase Backup



HSB to RGB by dontaskme

12th Dec 2021 20:03
Summary

Function: get an rgb value from hsb values



Description

If you prefer using hsb to rgb values for your colors you might give this a try.
The function takes hue (0 - 360), saturation (0.0 - 1.0) and brightness (0.0 - 1.0) and returns an integer rgb color value. I added a color correction for the green range, which often is too bright and unnatural. Feel free to toy around with the parameters for a result that meets your taste.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    /* 	HSB
---------------------------------------------------
	convert HSB -> RGB 
	
	hue         = 0 - 360
	saturation# = 0.0 - 1.0
	brightness# = 0.0 - 1.0
	calibrate   = 0 / 1 : apply color correction on green range
		
	return: int RGB value with MakeColor(red, green, blue)
	get components with GetColorRed(), getColorGreen(), getColorBlue()
	
	*make sure to pass float values for saturation# and brightness#
*/

function HSB(hue, saturation#, brightness#, calibrate)
	// limit the input vars 
	if hue < 0 or hue >= 360 then hue = 0
	if saturation# < 0.0 then saturation# = 0.0
	if saturation# > 1.0 then saturation# = 1.0
	if brightness# < 0.0 then brightness# = 0.0
	if brightness# > 1.0 then brightness# = 1.0
	
	if calibrate
		// brightness correction ('gamma' on green range)
		brightnessShift# = 0.25		// amount of brightness correction
		rangeStart#      = 77.0		// start of green range
		rangeCenter#     = 136.0	// center of green range
		rangeEnd#  		 = 184.0	// end of green range
		
		if rangeStart# < hue and hue <= rangeCenter#
			f# = ((hue - rangeStart#) / (rangeCenter# - rangeStart#)) * brightnessShift#
			brightness# = brightness# * ( 1.0 - (f# * saturation#))
		endif
		if rangeCenter# < hue and hue < rangeEnd#
			f# = ((rangeEnd# - hue) / (rangeEnd# - rangeCenter#)) * brightnessShift#
			brightness# = brightness# * ( 1.0 - (f# * saturation#))
		endif
		
		// hue correction (narrow green range)
		corrHueAmount# = 0.75 
		greenshift#    = 0.66666667
		
		corrHue# = hue
		if hue < 90
			corrHue# = hue * greenshift#
		endif
		if 90 <= hue and hue < 120
			corrHue# = (90.0 * greenshift#) + ((hue - 90.0) / greenshift#)
		endif
		hue = hue + (corrHue# - hue) * corrHueAmount#
	endif
	
	// preparing the hue sectors 60°
	Hi = mod((hue / 60), 6)
	f# = (hue / 60.0) - (Hi * 1.0)	
	// depending on the Hi sector, write rgb components
	select Hi
	case 0
		r# = brightness#
		g# = brightness# * (1.0 - ((1.0 - f#) * saturation#))
		b# = brightness# * (1.0 - saturation#)
	endcase
	case 1
		r# = brightness# * (1.0 - (f# * saturation#))
		g# = brightness#
		b# = brightness# * (1.0 - saturation#)
	endcase
	case 2
		r# = brightness# * (1.0 - saturation#)
		g# = brightness#
		b# = brightness# * (1.0 - ((1.0 - f#) * saturation#))
	endcase
	case 3
		r# = brightness# * (1.0 - saturation#)
		g# = brightness# * (1.0 - (f# * saturation#))
		b# = brightness#
	endcase
	case 4
		r# = brightness# * (1.0 - ((1.0 - f#) * saturation#))
		g# = brightness# * (1.0 - saturation#)
		b# = brightness#
	endcase
	case 5
		r# = brightness#
		g# = brightness# * (1.0 - saturation#)
		b# = brightness# * (1.0 - (f# * saturation#))
	endcase
	endselect
	
	rgb = MakeColor(r# * 255, g# * 255, b# * 255)
endfunction rgb