I have a function for drawing gradient text which works great on mac, but isn’t working properly on windows.
I put it together in a runnable example:
import wx
def get_gradient_text_solid_bg(text,point_size,top_colour,bottom_colour,bg_colour_tuple):
dc=wx.MemoryDC()
f= dc.GetFont()
f.SetPointSize(point_size)
dc.SetFont(f)
dc.SelectObject(wx.EmptyBitmap(0,0))
w,h= dc.GetTextExtent(text)
template_bmp= wx.EmptyBitmap(w,h)
dc.SelectObject(template_bmp)
dc.SetBackground(wx.Brush(wx.WHITE))
dc.Clear()
dc.DrawText(text,0,0)
dc.SelectObject(wx.NullBitmap)
template_image= template_bmp.ConvertToImage()
template_image.ConvertColourToAlpha(*bg_colour_tuple)
template_bmp= template_image.ConvertToBitmap()
text_bmp= wx.EmptyBitmap(w,h)
dc.SelectObject(text_bmp)
dc.GradientFillLinear((0,0,w,h),top_colour,bottom_colour,wx.SOUTH)
dc.DrawBitmap(template_bmp,0,0)
dc.SelectObject(wx.NullBitmap)
return text_bmp
class Frame(wx.Frame):
def __init__(self,*args,**kwargs):
wx.Frame.__init__(self,*args,**kwargs)
panel= wx.Panel(self)
bg_colour= (237,237,237)
panel.SetBackgroundColour(wx.Colour(*bg_colour))
wx.StaticBitmap(panel,bitmap=get_gradient_text_solid_bg("Testing",30,(255,80,80),(255,215,215),bg_colour))
self.Show()
if __name__ == "__main__":
app= wx.App(False)
Frame(None)
app.MainLoop()
I’m not sure exactly why it isn’t working, I thought it was because the wxImage doesn’t seem to have an alpha channel on windows but you can see some of the reds come through in the text so I think there must be some alpha in the Image on the edges (anti-aliased parts) so maybe this is a problem with ConvertColourToAlpha on windows? How can I get this to appear the same on windows?


I found the solution to this with the help of Robin.
It is a bug with C++, I’ve created a tracker on the wxpython bug tracker but a work around is to set the bit depth to 24
text_bmp= wx.EmptyBitmap(w,h,24)