指定画像を任意の色で単色化し、指定色名をファイル名に反映させてファイル出力するAppleScriptです。
もとは、「色付き単色画像を作成する」AppleScriptに、
・choose colorによるユーザー選択色を反映
・choose colorで指定した色のざっくりとした色名検出
などの機能をつなぎ合わせたものです。
日本地図の白地図からカラーバリエーション画像を出力する際に作成しました。線が黒で書かれている白地図(白地図というのはそういうものですが)から、さまざまな色のバリエーションを作成。その際に、どのような色を指定したかをファイル名に反映させておいたほうが便利だったので、そのようにしてみました。
GPUImage.frameworkを利用しています。
–> GPUImage.framework (To ~/Library/Frameworks/)
色名推定のロジックもけっこうしっくりきていますが、明度も反映して、明るいものは「light-」、暗いものは「dark-」と出力させてもいいような気がします。ただし、「dark-orange」はBrownのことですし、ほかにも色名を別途振り直したほうがいいパターンもあるように思われます。
AppleScript名:色付き単色画像を作成する(自由色指定&色名推定) |
— Created 2017-02-11 by Takaaki Naganoya — Modified 2018-07-15 by Takaaki Naganoya — 2017-2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "GPUImage" –https://github.com/BradLarson/GPUImage property NSUUID : a reference to current application’s NSUUID property NSColor : a reference to current application’s NSColor property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property NSBezierPath : a reference to current application’s NSBezierPath property NSPNGFileType : a reference to current application’s NSPNGFileType property GPUImagePicture : a reference to current application’s GPUImagePicture property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep –Select Image File set aFile to POSIX path of (choose file of type {"public.image"} with prompt "Select image") –Select Color set {rCol, gCol, bCol} to choose color set fillColorR to makeNSColorFromRGBAval(rCol, gCol, bCol, 65535, 65535) of me set fillColorRName to retColorDomainNameFromNSColor(fillColorR) of me –だいたいの色名称を計算 set imgRes to makeMonoColoredImage(aFile, fillColorR) of me set fRes to retUUIDandKeyfilePath(aFile, fillColorRName, "png") of me set bRes to saveNSImageAtPathAsPNG(imgRes, fRes) of me — return "UUID_aKey.aEXT" full path on retUUIDandKeyfilePath(aPath, aKey, aEXT) set aUUIDstr to ((NSUUID’s UUID()’s UUIDString()) as string) & "_" & aKey set aPath to ((NSString’s stringWithString:aPath)’s stringByDeletingLastPathComponent()’s stringByAppendingPathComponent:aUUIDstr)’s stringByAppendingPathExtension:aEXT return aPath end retUUIDandKeyfilePath on makeMonoColoredImage(aFile, NScolorObj) set aImage to NSImage’s alloc()’s initWithContentsOfFile:aFile return makeColoredNSImageWithColor(aImage, NScolorObj) of me –色付き単色画像を作成する end makeMonoColoredImage on makeColoredNSImageWithColor(aImage, fillColor) set aSize to aImage’s |size|() set aWidth to width of aSize set aHeight to height of aSize set newNSImage to makeNSImageWithFilledWithColor(aWidth, aHeight, fillColor) set grayImage to filterWithNSImage(aImage, "GPUImageGrayscaleFilter") of me set compImage to composeImageWithBlendFilter(grayImage, newNSImage, "GPUImageScreenBlendFilter") of me return compImage end makeColoredNSImageWithColor on filterWithNSImage(aNSImage, filterName as string) set aClass to current application’s NSClassFromString(filterName) set aImageFilter to aClass’s alloc()’s init() set aProcImg to (aImageFilter’s imageByFilteringImage:aNSImage) return aProcImg end filterWithNSImage on composeImageWithBlendFilter(aImage, bImage, filterName) set aClass to current application’s NSClassFromString(filterName) set blendFilter to aClass’s alloc()’s init() set pictureA to GPUImagePicture’s alloc()’s initWithImage:aImage pictureA’s addTarget:blendFilter pictureA’s processImage() set imgRes to blendFilter’s imageByFilteringImage:bImage return imgRes end composeImageWithBlendFilter –指定サイズの画像を作成し、指定色で塗ってNSImageで返す on makeNSImageWithFilledWithColor(aWidth, aHeight, fillColor) –Imageの作成 set curSize to current application’s NSMakeSize(aWidth, aHeight) set anImage to NSImage’s alloc()’s initWithSize:curSize 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() –描画ここまで –生成した画像のRaw画像を作成 set imageRep to anImage’s TIFFRepresentation() set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep set newImg to NSImage’s alloc()’s initWithSize:curSize newImg’s addRepresentation:aRawimg return newImg end makeNSImageWithFilledWithColor –NSImageを指定パスにPNG形式で保存 on saveNSImageAtPathAsPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep set pathString to NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (aRawimg’s representationUsingType:(NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes –true/false end saveNSImageAtPathAsPNG –NSColor(内容はRGBAカラー)からだいたいの色名を推測 on retColorDomainNameFromNSColor(aCol) set hueVal to aCol’s hueComponent() set satVal to aCol’s saturationComponent() set brightVal to aCol’s brightnessComponent() if satVal ≤ 0.01 then set satVal to 0.0 set colName to "" if satVal = 0.0 then if brightVal ≤ 0.2 then set colName to "black" else if (brightVal > 0.95) then set colName to "white" else set colName to "gray" end if else if hueVal ≤ (15.0 / 360) or hueVal ≥ (330 / 360) then set colName to "red" else if hueVal ≤ (45.0 / 360) then set colName to "orange" else if hueVal < (70.0 / 360) then set colName to "yellow" else if hueVal < (150.0 / 360) then set colName to "green" else if hueVal < (190.0 / 360) then set colName to "green(cyan)" else if (hueVal < 250.0 / 360.0) then set colName to "blue" else if (hueVal < 290.0 / 360.0) then set colName to "purple" else set colName to "red(magenta)" end if end if return colName end retColorDomainNameFromNSColor on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer) set aRedCocoa to (redValue / aMaxVal) as real set aGreenCocoa to (greenValue / aMaxVal) as real set aBlueCocoa to (blueValue / aMaxVal) as real set aAlphaCocoa to (alphaValue / aMaxVal) as real set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa return aColor end makeNSColorFromRGBAval |
More from my site
(Visited 47 times, 1 visits today)