Modify Hue on an image

BlitzMax Forums/BlitzMax Programming/Modify Hue on an image

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Just make the parts of the image grey which have to be colorized...
If the eyes are totally white (255,255,255) and the pupils black (0,0,0) there will be no problems... or just make them like (255,255,250) - so you will not see a difference to perfect white.

The function below will colorize all grey parts to the given rgb-values. You can easily change it to color pixmaps and so on.

Function ARGB_Alpha:Int(ARGB:Int)
 Return Int((ARGB & $FF000000:Int) / $1000000:Int)
End Function

Function ARGB_Red:Int(ARGB:Int)
 Return Int((ARGB & $00FF0000:Int) / $10000:Int)
End Function

Function ARGB_Green:Int(ARGB:Int)
 Return Int((ARGB & $0000FF00:Int) / $100:Int)
End Function

Function ARGB_Blue:Int(ARGB:Int)
 Return (ARGB & $000000FF:Int)
End Function

Function ARGB_Color:Int(alpha:Int,red:Int,green:Int,blue:Int)
 Return (Int(alpha*$1000000)+Int(blue*$10000)+Int(green*$100)+Int(RED))
End Function 
             
'returns true if the given pixel is monochrome (grey)						   
Function isMonochrome(argb:Int)
Try
	Local alpha:Int = ARGB_Alpha(argb)
	Local red:Int = ARGB_Red(argb)
	Local green:Int = ARGB_Green(argb)
	Local blue:Int = ARGB_Blue(argb)
	                                                        '250
	If (red = green) And (red = blue)And(alpha <> 0)And(red<250) Then Return green
	Return 0
Catch a$
	Print "ismonochrome exception:"+a$
EndTry

End Function
			   
'colorizes an TImage (may be an AnimImage when given cell_width and height)
Function ColorizeTImage:TImage(_image:TImage, r:Int,g:Int,b:Int, cell_width:Int=0,cell_height:Int=0,first_cell:Int=0,cell_count:Int=1, flag:Int=0)
If _image <> Null
  Local mypixmap:TPixmap=LockImage(_image)
  Local mypixmap2:TPixmap = TPixmap.Create(_image.width,_image.height, mypixmap.format,1)
  mypixmap2 = mypixmap.Copy()
  Local mypixelptr2:Int Ptr = Int Ptr(mypixmap2.PixelPtr(0,0))
  Local mypixelptr2backup:Int Ptr = mypixelptr2
  For Local my_x=0 To ((mypixmap2.width)*(mypixmap2.height))
 '   If Mypixelptr2[0] = Null Then Exit
     Local colortone:Int = isMonochrome(mypixelptr2[0])
     If colortone > 0 And mypixelptr2[0] <> 0          
       mypixelptr2[0] = ARGB_Color(ARGB_Alpha(mypixelptr2[0]),Int(colortone*r/255), Int(colortone*g/255), Int(colortone*b/255))
     EndIf
     mypixelptr2:+1
     If mypixelptr2 = mypixelptr2backup+(mypixmap2.pitch Shr 2)
         mypixelptr2backup=mypixelptr2
     EndIf
  Next        
  UnlockImage(_image)
  If cell_width > 0 And cell_count > 0 Then Return LoadAnimImage(mypixmap2, cell_width, cell_height, first_cell,cell_count, flag)
  Return LoadImage(mypixmap2)
EndIf
End Function



Hope this works for you.

bye
mB