Created
July 23, 2012 02:56
-
-
Save nico/3161796 to your computer and use it in GitHub Desktop.
Creates retina icons for some PSMTabBarControl icons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Foundation import * | |
from AppKit import * | |
from Quartz import * | |
def mkimg(w, h): | |
img = NSImage.alloc().initWithSize_((w, h)) | |
return img | |
def saverep(rep, path): | |
rep.representationUsingType_properties_(NSPNGFileType, None) \ | |
.writeToFile_atomically_(path, True) | |
def saveimg(img, path): | |
s = img.size() | |
img.lockFocus() | |
bitmapRep = NSBitmapImageRep.alloc().initWithFocusedViewRect_(((0, 0), s)) | |
img.unlockFocus() | |
saverep(bitmapRep, path) | |
def strokegradient(path, gradient, bounds): | |
context = NSGraphicsContext.currentContext().graphicsPort() | |
CGContextBeginTransparencyLayerWithRect(context, | |
NSRectToCGRect(bounds), | |
None) | |
NSColor.whiteColor().set() | |
path.stroke() | |
CGContextSetBlendMode(context, kCGBlendModeSourceIn) | |
gradient.drawInRect_angle_(bounds, 90) | |
CGContextEndTransparencyLayer(context); | |
def invertpath(p, w, h): | |
pout = NSBezierPath.bezierPathWithRect_(((0, 0), (w, h))) | |
pout.appendBezierPath_(p.bezierPathByReversingPath()) | |
return pout | |
def setshadow(offset, col, r): | |
sh = NSShadow.alloc().init() | |
sh.setShadowOffset_(offset) | |
sh.setShadowColor_(col) | |
sh.setShadowBlurRadius_(r) | |
sh.set() | |
def disk(fillcol, strokecol): | |
p = NSBezierPath.bezierPathWithOvalInRect_(((0.5, 2.5), (23, 23))) | |
pout = invertpath(p, 24, 26) | |
# Circle | |
fillcol.set() | |
p.fill() | |
# Bright bottom highlight | |
NSGraphicsContext.saveGraphicsState() | |
setshadow((0, -2), NSColor.whiteColor(), 0) | |
pout.addClip() | |
p.fill() | |
NSGraphicsContext.restoreGraphicsState() | |
# Dark top highlight | |
NSGraphicsContext.saveGraphicsState() | |
r = ((0, 10), (24, 16)) | |
NSBezierPath.bezierPathWithRect_(r).addClip() | |
gradient = NSGradient.alloc().initWithStartingColor_endingColor_( | |
NSColor.clearColor(), strokecol) | |
p.addClip() | |
p.setLineWidth_(2) | |
strokegradient(p, gradient, r) | |
NSGraphicsContext.restoreGraphicsState() | |
def normdisk(): | |
c = NSColor.colorWithCalibratedWhite_alpha_(0.1, 0.4) | |
disk(c, NSColor.colorWithCalibratedWhite_alpha_(0.03, 0.4)) | |
def hoverdisk(): | |
c = NSColor.colorWithCalibratedWhite_alpha_(0.0, 0.4) | |
disk(c, NSColor.colorWithCalibratedWhite_alpha_(0.0, 0.4)) | |
def presseddisk(): | |
hoverdisk() | |
def plus(pos, w, l, rot=0): | |
p = NSBezierPath.bezierPath() | |
p.appendBezierPathWithRect_(NSMakeRect(-l/2.0, -w/2.0, l, w)) | |
p.appendBezierPathWithRect_(NSMakeRect(-w/2.0, -l/2.0, w, l)) | |
transform = NSAffineTransform.transform() | |
transform.rotateByDegrees_(rot) | |
p.transformUsingAffineTransform_(transform) | |
transform = NSAffineTransform.transform() | |
transform.translateXBy_yBy_(pos[0], pos[1]) | |
p.transformUsingAffineTransform_(transform) | |
return p | |
def p(c): | |
xPath = plus((12, 14), 16, 4) | |
c.set() | |
xPath.fill() | |
def normnew(): | |
normdisk() | |
p(NSColor.colorWithCalibratedWhite_alpha_(0.77, 1)) | |
def hovernew(): | |
hoverdisk() | |
p(NSColor.colorWithCalibratedWhite_alpha_(0.96, 1)) | |
def pressednew(): | |
presseddisk() | |
p(NSColor.colorWithCalibratedWhite_alpha_(0.55, 1)) | |
def x(c, sc): | |
xPath = plus((12, 14), 14, 2, rot=45) | |
c.set() | |
setshadow((0, -1), sc, 1) | |
xPath.fill() | |
def normclose(): | |
normdisk() | |
x(NSColor.colorWithCalibratedWhite_alpha_(0.72, 1), | |
NSColor.colorWithCalibratedWhite_alpha_(0.0, 0.4)) | |
def hoverclose(): | |
hoverdisk() | |
x(NSColor.colorWithCalibratedWhite_alpha_(0.69, 1), | |
NSColor.colorWithCalibratedWhite_alpha_(0.0, 0.4)) | |
def pressedclose(): | |
presseddisk() | |
x(NSColor.colorWithCalibratedWhite_alpha_(0.6, 1), | |
NSColor.colorWithCalibratedWhite_alpha_(0.0, 0.4)) | |
def chevronpath(): | |
p = NSBezierPath.alloc().init() | |
for dx in [8, 20]: | |
p.moveToPoint_((dx + 0, 1)) | |
p.lineToPoint_((dx + 6, 1)) | |
p.lineToPoint_((dx + 15, 10)) | |
p.lineToPoint_((dx + 6, 20)) | |
p.lineToPoint_((dx + 0, 20)) | |
p.lineToPoint_((dx + 9, 10)) | |
p.closePath() | |
return p | |
def chevron(c1, c2, sc, sc2): | |
p = chevronpath() | |
# based on http://stackoverflow.com/questions/7137705/how-to-draw-a-nsimage-like-images-in-nsbuttons-with-a-deepness | |
NSGraphicsContext.saveGraphicsState() | |
# Draw image and white drop shadow: | |
setshadow((0, -2), sc, 0) | |
p.fill() | |
# Clip drawing to mask: | |
p.addClip() | |
# Draw gradient: | |
gradient = NSGradient.alloc().initWithStartingColor_endingColor_(c1, c2) | |
gradient.drawInRect_angle_(((0, 0), (40, 20)), 90.0) | |
# Draw inner shadow with inverted mask: | |
setshadow((0, -2), sc2, 4) | |
pout = invertpath(p, 40, 20) | |
pout.fill() | |
NSBezierPath.strokeLineFromPoint_toPoint_((0, 21), (40, 21)) # top shadow | |
NSGraphicsContext.restoreGraphicsState() | |
def normchevron(): | |
chevron(NSColor.colorWithDeviceWhite_alpha_(0.5, 1.0), | |
NSColor.colorWithDeviceWhite_alpha_(0.25, 1.0), | |
NSColor.colorWithDeviceWhite_alpha_(0.7, 1.0), | |
NSColor.colorWithDeviceWhite_alpha_(0.25, 1.0)) | |
def pressedchevron(): | |
chevron(NSColor.colorWithDeviceWhite_alpha_(0.4, 1.0), | |
NSColor.colorWithDeviceWhite_alpha_(0.15, 1.0), | |
NSColor.colorWithDeviceWhite_alpha_(0.7, 1.0), | |
NSColor.colorWithDeviceWhite_alpha_(0.25, 1.0)) | |
def mkbutton(draw, w=24, h=26): | |
img = mkimg(w, h) | |
img.lockFocus() | |
draw() | |
img.unlockFocus() | |
return img | |
def main(): | |
NSApplicationLoad() | |
saveimg(mkbutton(normnew), "[email protected]") | |
saveimg(mkbutton(hovernew), "[email protected]") | |
saveimg(mkbutton(pressednew), "[email protected]") | |
saveimg(mkbutton(normclose), "[email protected]") | |
saveimg(mkbutton(hoverclose), "[email protected]") | |
saveimg(mkbutton(pressedclose), "[email protected]") | |
saveimg(mkbutton(normchevron, w=40, h=20), "[email protected]") | |
saveimg(mkbutton(pressedchevron, w=40, h=20), "[email protected]") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment