Hue Shift (like photoshop) Effect

Blitz3D Forums/Blitz3D Programming/Hue Shift (like photoshop) Effect

hmm... well, i integrated the HSL into the code and made it calculate the way to color each sprite using the hue instead, and it is better. But the colors still get blown out when they are cycled instead of staying at the correct luminance. Strange.

This is definitely simpler and closer to working right, but it's not quite there. any ideas?

 
Graphics3D 640,480,0,2

pivot = CreatePivot()
cam = CreateCamera(pivot)
PositionEntity cam,0,0,30
PointEntity cam,pivot

AmbientLight 0,0,0

Global result_r#, result_g#, result_b#		
Global result_h#, result_s#, result_l#

Type col
	Field image
	Field red
	Field green
	Field blue
	Field state
	
	Field h#
	Field s#
	Field l#
End Type

Function makecol(file$,red,green,blue,state)
	c.col = New col
	c\image = LoadSprite(file)
	ScaleSprite c\image,20,20
	EntityFX c\image,1
	EntityBlend c\image,3

	c\red = red
	c\green = green
	c\blue = blue
	c\state = state
	
	rgb_to_hsl(c\red,c\green,c\blue)
	c\h = result_h
	c\s = result_s
	c\l = result_l
	
End Function

makecol("red.jpg",255,0,0,1)
makecol("green.jpg",0,255,0,3)
makecol("blue.jpg",0,0,255,5)

speed = 5

While Not KeyHit(1)


	For c.col = Each col
	
		hsl_to_rgb(c\h,c\s,c\l)
		c\red = result_r
		c\green = result_g
		c\blue = result_b
		
		If c\h < 360
			c\h = c\h+speed
		Else
			c\h = 0
		EndIf
		
		EntityColor c\image,c\red,c\green,c\blue	
	
	
	Next

RenderWorld()

Flip

Wend
End



; RGB/HSL function return values.


;
; 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

	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