Modify Hue on an image

BlitzMax Forums/BlitzMax Programming/Modify Hue on an image

Hey ghost dancer, thanks man, it's exactly what I need. Now only if I could get it working ;). I'm not too sharp on BlitzPlus (never used it), but I tried to convert it to bmax anyway. It runs but gives a weird problem, noted in my comments in the first few lines.

I think I'm either calling your functions incorrectly, or I misunderstood something about the temp pointer in rgb2hsv(). I'm still trying to learn about pointers, so it's pretty exotic for me :p

Can you see what I did wrong you think?

Strict


Local myrgb = rgb(100,150,200)
Print "Red color of 'rgb' should be 100, it is: "+getred(myrgb)		'prints out: 255
Local myhsv:hsv = New hsv
myhsv = rgb2hsv(myrgb,myhsv)
Print "HSV values from myrgb: "+myhsv.h+" "+myhsv.s+" "+myhsv.v		'prints out: -1.#IND0000 0.000000000 1.00000000
Local newrgb = hsv2rgb(myhsv.h,myhsv.s,myhsv.v)
Print "myrgb converted to hsv, and back to rgb: "+newrgb			'prints out: 16777215



'-----------begin ghost dancer's code, converted to bmax-------------------

'create a custom Type For HSV colour
Type hsv
	Field h#, s#, v#
End Type



'-------------------------------------------------------------------
Function getRed(rgb)
'-------------------------------------------------------------------
'Get red component of RGB colour value
'
'Parameters:
'rgb	- RGB value that you want the red component from
'
'Return value:
'red value (0 To 255)
'-------------------------------------------------------------------
	rgb = rgb Shr 16 And $ff
	Return rgb' Shr 16 And $ff
End Function


'-------------------------------------------------------------------
Function getGreen(rgb)
'-------------------------------------------------------------------
'Get green component of RGB colour value
'
'Parameters:
'rgb	- RGB value that you want the green component from
'
';Return value:
'green value (0 To 255)
'-------------------------------------------------------------------

	Return (rgb Shr 8) And $ff
End Function


'-------------------------------------------------------------------
Function getBlue(rgb)
'-------------------------------------------------------------------
'Get blue component of rgb colour value
'
'Parameters:
'rgb	- RGB value that you want the blue component from
'
'Return value:
'blue value (0 To 255)
'-------------------------------------------------------------------
	Return rgb And $ff
End Function


'-------------------------------------------------------------------
Function rgb(r, g, b)
'-------------------------------------------------------------------
'Convert R,G,B components To single RGB value
'
'Parameters:
'r	- red value
'g	- green value
'b	- blue value
'
'Return value:
'rgb value
'-------------------------------------------------------------------

	Return (r Shl 16) + (g Shl 8) + b
End Function


'-------------------------------------------------------------------
Function rgb2hsv:hsv(rgb, temphsv:hsv)
'-------------------------------------------------------------------
'Convert RGB To HSV
'
'Parameters:
'rgb			- RGB value that you want convert
'temphsv.hsv	- Added in V2.01, it is required To prevent the
'				  memory leakage problem. This needs To be the same
'				  hsv Type pointer that the Function returns to.
'				  e.g.	myhsv.hsv = New hsv
'				  		myhsv = rgb2hsv($ff0000, myhsv)
'
'Return value:
'custom hsv Type defined in this library
'-------------------------------------------------------------------

'	;RGB components in  range 0 To 1
	Local r# = getRed(rgb) / 255.0
	Local g# = getGreen(rgb) / 255.0
	Local b# = getBlue(rgb) / 255.0

'	;Min value	
	Local minVal#
	If r < g Then minVal# = r Else minVal# = g
	If b < minVal Then minVal = b

'	;Max value
	Local maxVal#		
	If r > g Then maxVal# = r Else maxVal# = g
	If b > maxVal Then maxVal = b

'	;calculate difference
	Local diff# = maxVal - minVal
	
	temphsv.v = maxVal
	
	If maxVal = 0 Then
		temphsv.s = 0
		temphsv.h = -1		';h is undefined
	Else
		temphsv.s = diff / maxVal
	
		If r = maxVal Then
			temphsv.h = (g - b) / diff
		ElseIf g = maxVal Then
			temphsv.h = 2 + (b - r) / diff
		Else
			temphsv.h = 4 + (r - g) / diff
		EndIf
	
		temphsv.h = temphsv.h * 60
		If temphsv.h < 0 Then temphsv.h = temphsv.h + 360
	EndIf

	Return temphsv
End Function


';-------------------------------------------------------------------
Function hsv2rgb(h#, s#, v#)
'-------------------------------------------------------------------
'Convert HSV To RGB
'
'Parameters:
'h#	- hue (in degrees, 0 To 350)
's#	- saturation (0.0 To 1.0)
'v#	- value (brightness, 0.0 To 1.0)
'
'Return value:
'rgb value
'-------------------------------------------------------------------
	Local r#,g#,b#,f#,p#,q#,t#,i#

	If s = 0 Then
'		;saturation of 0 = grey
		r# = v ; g# = v ; b# = v
	Else
		h = h / 60
		i = Floor(h)
		f# = h - i
		p# = v * (1 - s)
		q# = v * (1 - s * f)
		t# = v * (1 - s * (1 - f))

		Select i
		Case 0
			r# = v
			g# = t
			b# = p
		Case 1
			r# = q
			g# = v
			b# = p
		Case 2
			r# = p
			g# = v
			b# = t
		Case 3
			r# = p
			g# = q
			b# = v
		Case 4
			r# = t
			g# = p
			b# = v
		Default
			r# = v
			g# = p
			b# = q
		End Select		
	EndIf

	r = r * 255
	g = g * 255
	b = b * 255

	Return rgb(r, g, b)
End Function