Glossy buttons

Dear folks,

I wrote an ad-hoc script to generate buttons that have the ‘glossy’ effect. The implementation is fairly straightforward and did work – the buttons themselves are rectangles with rounded corners so I call a polygon patch. The light is simulated using a white, transparent ellipse. But there is one problem though, the ellipse would mask anything that’s above the buttons as well.

Any people interested to take a look and suggest a solution?

Haibao

···

from pylab import *

from matplotlib.patches import *

fig = figure(1,(8,8))
root = fig.add_axes([0,0,1,1])

def plot_cap(center, t, r):
x, y = center
return zip(x+rcos(t), y+rsin(t))

def round_rect(ax, xy, width, height, shrink=.33, label=None, **kwargs):

shrink *= height
x,y=xy
pts = []
# plot the four rounded cap one by one
pts += plot_cap((x+width-shrink, y+height-shrink), array([radians(j) for j in range(0,90)]), shrink)
pts += [[x+width-shrink, y+height], [x+shrink, y+height]]

pts += plot_cap((x+shrink, y+height-shrink), array([radians(j) for j in range(90,180)]), shrink)
pts += [[x, y+height-shrink], [x, y+shrink]]
pts += plot_cap((x+shrink, y+shrink), array([radians(j) for j in range(180,270)]), shrink)

pts += [[x+shrink, y], [x+width-shrink, y]]
pts += plot_cap((x+width-shrink, y+shrink), array([radians(j) for j in range(270,360)]), shrink)
pts += [[x+width, y+shrink], [x+width, y+height-shrink]]
ax.add_patch(Polygon(pts, **kwargs))

# add a white transparency ellipse filter
# CAUTION -- things immediately above the button will be masked too!!!
ax.add_patch(Ellipse((x+width/2.,y+height),1.6*width,height*.8,fc='w',alpha=.3,lw=0))

if label: root.text(x+width/2.,y+height/2.,label,size=10,horizontalalignment="center",verticalalignment="center",color="w")

round_rect(root,(.45,.4),.1,.04,label=“Button”,lw=0,fc=‘k’)

round_rect(root,(.15,.4),.2,.08,label=“Download\nFirefox”,lw=0,fc=‘r’)
round_rect(root,(.65,.4),.2,.08,label=“Google\nChrome”,lw=0,fc=‘g’)

root.set_xlim(0,1)
root.set_ylim(0,1)

root.set_axis_off()

savefig(“glossy.pdf”)