I got it in the end although you have to pass a background colour to the function. I’ve also written one which loops over all the pixels setting alpha values as well if I ever want to draw the text on a background that isn’t a solid colour but probably not as it is roughly 40-50 times slower
I’ll paste them here just in case anyone else might find them useful
def get_gradient_text_solid_bg(text,font,point_size,top_colour,bottom_colour,bg_colour_tuple):
dc=wx.MemoryDC()
f=wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
f.SetFaceName(font)
f.SetPointSize(point_size)
dc.SetFont(f)
dc.SelectObject(wx.EmptyBitmap(0,0)) #Cant GetTextExtent without a bitmap selected
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
def get_gradient_text(text,font,point_size,top_colour,bottom_colour):
dc=wx.MemoryDC()
f=wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
f.SetFaceName(font)
f.SetPointSize(point_size)
dc.SetFont(f)
bitmap_1x1= wx.EmptyBitmap(1,1)
dc.SelectObject(bitmap_1x1)
w,h= dc.GetTextExtent(text)
template_bmp= wx.EmptyBitmap(w,h)
dc.SelectObject(template_bmp)
dc.SetBackground(wx.Brush(wx.BLACK))
dc.Clear()
dc.SetTextForeground(wx.WHITE)
dc.DrawText(text,0,0)
dc.SelectObject(wx.NullBitmap)
text_bmp= wx.EmptyBitmap(w,h)
dc.SelectObject(text_bmp)
rect= wx.Rect(0,0,w,h)
dc.GradientFillLinear(rect,top_colour,bottom_colour,wx.SOUTH)
dc.SelectObject(wx.NullBitmap)
template_image= template_bmp.ConvertToImage()
text_image= text_bmp.ConvertToImage()
for x in xrange(w):
for y in xrange(h):
text_image.SetAlpha(x,y,template_image.GetRed(x,y))
text_bmp= text_image.ConvertToBitmap()
return text_bmp
···
On 17 May 2012 00:20, Paul Wiseman poalman@gmail.com wrote:
On 16 May 2012 21:20, Tim Roberts timr@probo.com wrote:
Paul Wiseman wrote:
Hm interesting I’ll see if I can find a way to do this in wx. Do you
know if clipping regions can have alpha edges for the anti aliasing of
the text?
No, I think you’re going to find they are hard-edged. It’s not clear
that anti-aliasing is really going to serve any purpose with
gradient-colored text – you’re already giving up on maximizing readability.
–
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
–
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
Yea, it would only be a slight gradient on some large text though, 30 or so point size. Its the client who wants it, I think it’s a bad idea too 