指定色で塗りつぶしたNSImageを用意し、そこにNSBezierPathでclearColorで塗りつぶし(=切り抜き)を行うAppleScriptです。
2005/8/18にCocoa-dev mailing list に対してStefan Schüßler氏が投稿した内容をもとにしています。いろいろ探して回りましたが、ヒットしたのはこの情報だけでした。彼に感謝を。
Re: [NSColor clearColor] and NSBezierPath: not compatible? NSBezierPath uses the NSCompositeSourceOver operation, therefore clearColor does not do anything. You could change the graphics state in order to clear the path: NSGraphicsContext *context; context = [NSGraphicsContext currentContext]; [context saveGraphicsState]; [context setCompositingOperation:NSCompositeClear]; [yourBezierPath fill]; [context restoreGraphicsState]; Hope this helps. Stefan
AppleScript名:指定のNSImageにNSBezierPathでclearColor塗りつぶし.scptd |
— Created 2020-02-14 by Takaaki Naganoya — 2020 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "CoreImage" property NSColor : a reference to current application’s NSColor property NSImage : a reference to current application’s NSImage property NSZeroRect : a reference to current application’s NSZeroRect property NSBezierPath : a reference to current application’s NSBezierPath property NSCompositeCopy : a reference to current application’s NSCompositeCopy property NSGraphicsContext : a reference to current application’s NSGraphicsContext property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep property NSCalibratedRGBColorSpace : a reference to current application’s NSCalibratedRGBColorSpace set {rCol, gCol, bCol} to choose color set aNSImage to makeColoredNSImage({rCol, gCol, bCol}, 400, 400) of me set aPath to generateCircle(200, 100, 100) of me set bImage to my fillImage:aNSImage withTransparentPathFilling:aPath set aFile to POSIX path of (choose file name) set sRes to my saveNSImageAtPathAsPNG(bImage, aFile) on fillImage:aSourceImg withTransparentPathFilling:aPath set aSize to aSourceImg’s |size|() set aWidth to (aSize’s width) set aHeight to (aSize’s height) set aRep to NSBitmapImageRep’s alloc()’s initWithBitmapDataPlanes:(missing value) pixelsWide:aWidth pixelsHigh:aHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(NSCalibratedRGBColorSpace) bytesPerRow:0 bitsPerPixel:0 NSGraphicsContext’s saveGraphicsState() NSGraphicsContext’s setCurrentContext:(NSGraphicsContext’s graphicsContextWithBitmapImageRep:aRep) aSourceImg’s drawInRect:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) fromRect:(NSZeroRect) operation:(NSCompositeCopy) fraction:(1.0) NSGraphicsContext’s currentContext()’s setCompositingOperation:(current application’s NSCompositeClear) aPath’s fill() NSGraphicsContext’s restoreGraphicsState() set newImg to NSImage’s alloc()’s initWithSize:(aSize) newImg’s addRepresentation:aRep return newImg end fillImage:withTransparentPathFilling: on generateCircle(theRadius, x, y) set aRect to current application’s NSMakeRect(x, y, theRadius, theRadius) set aCirCle to NSBezierPath’s bezierPath() aCirCle’s appendBezierPathWithOvalInRect:aRect return aCirCle end generateCircle on makeColoredNSImage(colList, aWidth, aHeight) copy colList to {rCol, gCol, bCol} set aColor to makeNSColorFromRGBA65535val(rCol, gCol, bCol, 1.0) of me set aColoredImage to fillColorWithImage(aColor, aWidth, aHeight) of me return aColoredImage end makeColoredNSImage on fillColorWithImage(aColor, aWidth, aHeight) set colordImage to makeNSImageWithFilledWithColor(aWidth, aHeight, aColor, aWidth, aHeight) of me colordImage’s lockFocus() colordImage’s drawAtPoint:{0, 0} fromRect:(current application’s NSZeroRect) operation:(current application’s NSCompositeDestinationIn) fraction:1.0 colordImage’s unlockFocus() return colordImage end fillColorWithImage on makeNSImageWithFilledWithColor(aWidth, aHeight, fillColor) set anImage to NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight)) anImage’s lockFocus() — set theRect to {{x:0, y:0}, {height:aHeight, width:aWidth}} set theNSBezierPath to NSBezierPath’s bezierPath theNSBezierPath’s appendBezierPathWithRect:theRect — fillColor’s |set|() theNSBezierPath’s fill() — anImage’s unlockFocus() — return anImage end makeNSImageWithFilledWithColor on makeNSColorFromRGBA65535val(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer) set aRedCocoa to (redValue / 65535) as real set aGreenCocoa to (greenValue / 65535) as real set aBlueCocoa to (blueValue / 65535) as real set aAlphaCocoa to 1.0 as real set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa return aColor end makeNSColorFromRGBA65535val on saveNSImageAtPathAsPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to current application’s NSBitmapImageRep’s imageRepWithData:imageRep set pathString to current application’s NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (aRawimg’s representationUsingType:(current application’s NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes end saveNSImageAtPathAsPNG |
More from my site
(Visited 254 times, 1 visits today)
画像の指定エリアを透明色塗りつぶし(矩形切り抜き) – AppleScriptの穴 says:
[…] NSBezierPathで切り抜き(透明色で塗りつぶし)できるといいのになーという感じではあります。 […]