Convert Foxpro RGB value to ARGB for GDI+

May 19, 2006 @ 2:25 pm by walter — Filed under: Visual FoxPro

I can’t remember if there is a method for this buried in _GDIPLUS.VCX or not, but if you don’t want to use a GpColor object just to convert colours, the following tiny function would do:

function FoxRGBToARGB( tnRGB as integer, tnAlpha as integer )
   return  ;
      bitor( bitlshift(bitand(iif(vartype(m.tnAlpha)=='N', m.tnAlpha, 0xFF), 0xFF ),24) ;
	, bitlshift(bitand(m.tnRGB,0xFF),16) ;
	, bitand(m.tnRGB,0x0000FF00) ;
	, bitrshift(bitand(m.tnRGB,0x00FF0000),16) )

Example use:

m.foxrgb = getcolor()
m.argb = FoxRGBToARGB( m.foxrgb, 0xFF )
? transform(argb,'@0')

This technique is basically lifted from the foxrgb_assign method from the GpColor class, plus adding in the alpha value