Archive for the 'colorsKit' Category

2018/01/13 TextEditで本文色をポスタライズ v1

TextEdit書類の本文色をポスタライズするAppleScriptです。

複数のユーザーでRTFに対して色をつけてチェックを行なったような場合に、指定色が微妙にゆらいでしまっても色のゆらぎを吸収するためのものです。

TextEditでオープン中の最前面のRTF書類中の文字色のゆらぎを吸収します。

オープンソースの「Colours」(By Ben Gordon)をフレームワーク化したcolorsKitを呼び出して、RGB→HSBA変換を行なっています。2D List中のリスト項目検索のためにShane StanleyのBridge Plus AppleScript Librariesを利用しています。

実行には、Bridge PlusおよびcolorsKit.frameworkのインストールを必要とします。colorsKit.frameworkについてはアーカイブをダウンロードして展開し、~/Library/Frameworksフォルダに入れてください(あくまで自己責任で)。

–> Download Framework Binary

te1png.png
▲処理前(緑色の文字の色にゆらぎがある)

t22.png
▲処理後(文字色が12色程度の原色に統一される)

AppleScript名:TextEditで本文色をポスタライズ v1
– Created 2018-01-08 by Takaaki Naganoya
– 2018 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”
use framework “colorsKit” –https://github.com/bennyguitar/Colours
use bPlus : script “BridgePlus” –https://www.macosxautomation.com/applescript/apps/BridgePlus.html
–http://piyocast.com/as/archives/5097

property NSColor : a reference to current application’s NSColor
property NSArray : a reference to current application’s NSArray
property NSSortDescriptor : a reference to current application’s NSSortDescriptor

script spd
  property colList : {}
  
property attList : {}
end script

load framework

set cList to retTextEditColors() of me
set dList to {}

repeat with i in cList
  copy i to {rVal, gVal, bVal}
  
set newColList to posterizeColor(rVal, gVal, bVal, 65535) of me
  
repTextEditColor(i, newColList) of me
end repeat

on repTextEditColor(targColor, newColor)
  set hitIndex to {}
  
  
tell application “TextEdit”
    tell text of front document
      set colList to color of every attribute run
    end tell
  end tell
  
  
set hitIndex to (current application’s SMSForder’s indexesOfItem:targColor inArray:(colList) inverting:false) as list
  
  
tell application “TextEdit”
    tell text of front document
      repeat with i in hitIndex
        ignoring application responses
          set color of attribute run (i + 1) to newColor –0 based index to 1 based index conversion
        end ignoring
      end repeat
    end tell
  end tell
end repTextEditColor

on posterizeColor(rCol as integer, gCol as integer, bCol as integer, aColorMax)
  set aRes to makeNSColorFromRGBAval(rCol, gCol, bCol, aColorMax, aColorMax) of me
  
set aDic to aRes’s hsbaDictionary()
  
  
set hueVal to (|HSBA-h| of aDic) as real
  
set satVal to (|HSBA-s| of aDic) as real
  
set brightVal to (|HSBA-b| of aDic) as real
  
set alphaVal to (|HSBA-a| of aDic) as real
  
  
if satVal 0.01 then set satVal to 0.0
  
  
if satVal = 0.0 then
    if brightVal 0.2 then
      set colVal to {0, 0, 0} –Black
    else if (brightVal > 0.95) then
      set colVal to {65535, 65535, 65535} –White
    else
      set colVal to {32768, 32768, 32768} –Gray
    end if
  else
    if hueVal (15.0 / 360) or hueVal (330 / 360) then
      set colVal to {65535, 0, 0} –red
    else if hueVal (45.0 / 360) then
      set colVal to {65535, 32768, 0} –orange
    else if hueVal < (70.0 / 360) then
      set colVal to {65533, 63639, 2654} –yellow
    else if hueVal < (150.0 / 360) then
      set colVal to {4626, 35488, 17789} –green
    else if hueVal < (190.0 / 360) then
      set colVal to {0, 60802, 65535} –cyan, light blue
    else if (hueVal < 250.0 / 360.0) then
      set colVal to {0, 0, 65535} –blue
    else if (hueVal < 290.0 / 360.0) then
      set colVal to {32768, 0, 32768} –purple
    else
      set colVal to {65535, 0, 65535} –magenta, pink
    end if
  end if
  
  
return colVal
end posterizeColor

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 aCocoaList to {aRedCocoa, aGreenCocoa, aBlueCocoa, aAlphaCocoa}
  
set aColor to NSColor’s colorFromRGBAArray:aCocoaList
  
return aColor
end makeNSColorFromRGBAval

on retTextEditColors()
  tell application “TextEdit”
    set dCount to count every document
    
if dCount = 0 then return
    
tell text of front document
      set aList to color of every character
    end tell
  end tell
  
  
set ap1List to uniquify1DList(aList, true) of me
  
set cList to {}
  
repeat with i in ap1List
    set the end of cList to contents of i
  end repeat
  
  
return cList
end retTextEditColors

–1D Listをユニーク化
on uniquify1DList(theList as list, aBool as boolean)
  set aArray to NSArray’s arrayWithArray:theList
  
set bArray to aArray’s valueForKeyPath:“@distinctUnionOfObjects.self”
  
set bList to bArray as list
  
return bList
end uniquify1DList

★Click Here to Open This Script 

2018/01/11 RGB色をHSBAに変換して色名称を計算

オープンソースの「Colours」(By Ben Gordon)をフレームワーク化したcolorsKitを呼び出して、指定色が「だいたいどんな色」かを計算するAppleScriptの別バージョンです。

hsba_color_picker.png

自分のやり方(色差ΔEで代表的な色との距離を計算する)のほかにもないものかと調べてみたら、RGB色をHSBA色に変換して色判定を行うものを見つけました。ロジックがとても簡潔で技術的な難易度もおっそろしく低かったので、Objective-Cで書かれたプログラムをAppleScriptに置き換えて検証してみました。

RGB色をHSBAに変換して、Hue(色相)の角度を細分化して「この数値からこの数値まではこの色」といったセグメント分けを行なっています。

実行には、colorsKit.frameworkのインストールを必要とします。アーカイブをダウンロードして展開し、~/Library/Frameworksフォルダに入れてください(あくまで自己責任で)。

–> Download Framework Binary

とてもよくわかるのが、やっぱりこのプログラムでもWhite〜Gray〜Blackの判定を最初に行なっていること。また、LightとDarkの判定も行なっています。

色のセグメント分けの箇所が自分のプログラムとは違いはするものの、とてもよく分かる内容でした。ちなみに、さらに細分化した色名称を取得するものがgithub上で公開されています

AppleScript名:RGB色をHSBAに変換して色名称を計算
– Created 2018-01-08 by Takaaki Naganoya
– 2018 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”
use framework “colorsKit” –https://github.com/bennyguitar/Colours
–http://piyocast.com/as/archives/5094

property NSColor : a reference to current application’s NSColor

set {rCol, gCol, bCol} to choose color
set cName to retColorName(rCol, gCol, bCol) of me

on retColorName(rCol as integer, gCol as integer, bCol as integer)
  set aRes to makeNSColorFromRGBAval(rCol, gCol, bCol, 65535, 65535) of me
  
set aDic to aRes’s hsbaDictionary()
  
  
set hueVal to (|HSBA-h| of aDic) as real
  
set satVal to (|HSBA-s| of aDic) as real
  
set brightVal to (|HSBA-b| of aDic) as real
  
set alphaVal to (|HSBA-a| of aDic) as real
  
  
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”
      
      
if (brightVal < 0.33) then
        set colorName to “dark “ & colorName
      else if (brightVal > 0.66) then
        set colorName to “light “ & colorName
      end if
      
    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 “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 “magenta”
    end if
    
    
if (brightVal < 0.5) then
      set colName to “dark “ & colName
    else if (brightVal > 0.0) then
      set colName to “light “ & colName
    end if
  end if
  
  
return colName
end retColorName

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 aCocoaList to {aRedCocoa, aGreenCocoa, aBlueCocoa, aAlphaCocoa}
  
set aColor to NSColor’s colorFromRGBAArray:aCocoaList
  
return aColor
end makeNSColorFromRGBAval

★Click Here to Open This Script 

2018/01/08 Coloursで指定色がだいたいどんな色なのか推測 v3(白、黒、グレーの判別機能つき)

オープンソースの「Colours」(By Ben Gordon)をフレームワーク化したcolorsKitを呼び出して、指定色が「だいたいどんな色」かを計算するAppleScriptの改良版です。

特徴的な色(Red, Yellow, Pink, Blue, Green, Black, White, Gray, Purple, Light Blue)からの色差を計算して、最も色の距離が近いものをその特徴的な色(Color Domain)とみなして名前(Color Domain Name)を返します。

実行には、colorsKit.frameworkのインストールを必要とします。アーカイブをダウンロードして展開し、~/Library/Frameworksフォルダに入れてください(あくまで自己責任で)。

–> Download Framework Binary

col31.png

col32.png

改良点1:Black〜Gray〜Whiteの判定を最初に行うようにした。これらはRGBの各色の値がほぼイコールで構成されているので除外しやすい。また、濃い色で「Black」ではないものを「Black」と判定されないように、Blackの判定範囲をきわめて狭くした

改良点2:NSColorを作成するのに、Colourの機能を用いて行うようにした。これによって、色差の計算を行なったときにおかしな値が出ることがなくなった

改良点3:Black、Whiteに近い値でこれらではない色をBlack、Whiteにしないように、後処理を行なってみた

col30.png

これらの改良の結果、クレヨンカラーピッカーのうち、首をひねりたくなるようなおかしな判定は1色ぐらいに抑えられるようになりました(本当)。緑色に見える「ティール」{0, 32896, 32896}が「Light Blue」に判定される以外はきわめていい感じです。

AppleScript名:Coloursで指定色がだいたいどんな色なのか推測 v3(白、黒、グレーの判別機能つき)
– Created 2018-01-05 by Takaaki Naganoya
– 2018 Piyomaru Software
use AppleScript version “2.5″
use scripting additions
use framework “Foundation”
use framework “AppKit”
use framework “colorsKit” –https://github.com/bennyguitar/Colours
(*
http://piyocast.com/as/archives/5088

*)

property NSColor : a reference to current application’s NSColor
property NSArray : a reference to current application’s NSArray
property NSSortDescriptor : a reference to current application’s NSSortDescriptor

set aColList to choose color
set aRes to retColorDomainName(aColList, 65535) of me

on retColorDomainName(aColList, aColMax)
  set aCocoaList to retCocoaColorList(aColList, aColMax) of me
  
set aCol to NSColor’s colorFromRGBAArray:aCocoaList
  
  
copy aColList to {rVal, gVal, bVal}
  
  
set colorDomainList to {{colName:“Red”, colValue:{64615, 0, 1767}}, {colName:“Yellow”, colValue:{65530, 65344, 2686}}, {colName:“Pink”, colValue:{64551, 0, 57891}}, {colName:“Blue”, colValue:{1636, 0, 65417}}, {colName:“Green”, colValue:{8866, 65535, 2217}}, {colName:“Black”, colValue:{0, 0, 0}}, {colName:“White”, colValue:{65535, 65535, 65535}}, {colName:“Purple”, colValue:{27963, 0, 65430}}, {colName:“Light Blue”, colValue:{7557, 59465, 65463}}}
  
  
–White, Black, Grayの判定。32で整数序算しているのはゆらぎの吸収のため
  
set aDivNum to (aColMax div 32)
  
set rTmp to rVal div aDivNum
  
set gTmp to gVal div aDivNum
  
set bTmp to bVal div aDivNum
  
if {rTmp = gTmp, gTmp = bTmp, bTmp = rTmp} = {true, true, true} then
    if rVal > (aColMax * 0.9) then
      return “White”
    else if rVal < (aColMax * 0.1) then
      return “Black”
    else
      return “Gray”
    end if
  end if
  
  
–各Color Domainからの距離を計算する
  
set nsColList to {}
  
repeat with i in colorDomainList
    set aColV to colValue of i
    
set aColN to colName of i
    
set bCocoaList to retCocoaColorList(aColV, aColMax) of me
    
set bCol to (NSColor’s colorFromRGBAArray:bCocoaList)
    
set aDist to (aCol’s distanceFromColor:bCol type:1) –ColorDistanceCIE94
    
if (aDist as string) = “NaN” then set aDist to 65535
    
set the end of nsColList to {colObj:bCol, colDist:aDist, colName:aColN}
  end repeat
  
  
–距離でソート
  
set aRes to sort1DRecList(nsColList, “colDist”, true) of me
  
set aFirst to first item of aRes
  
set aName to colName of aFirst
  
  
  
–White & Blackを除外(濃い紫などが「Black」と判定される場合への対応)
  
set hitF to false
  
if aName is in {“Black”, “White”} then
    repeat with i in aRes
      set aColD to colDist of i
      
set aColN to colName of i
      
if aColN is not in {“Black”, “White”} and aColD is not equal to 65535 then
        set aName to aColN
        
set hitF to true
        
exit repeat
      end if
    end repeat
    
    
–Black & Whiteの除外処理を行なっても適当なものがない場合の処理
    
if hitF = false then
      set aFirst to first item of aRes
      
set aName to colName of aFirst
    end if
  end if
  
  
return aName
end retColorDomainName

on retCocoaColorList(aColorList, aMax)
  set cocoaColorList to {}
  
repeat with i in aColorList
    set the end of cocoaColorList to i / aMax
  end repeat
  
set the end of cocoaColorList to 1.0
  
return cocoaColorList
end retCocoaColorList

–数値の1D List with Recordをソート
on sort1DRecList(aList as list, aKey as string, ascendingF as boolean)
  set aArray to NSArray’s arrayWithArray:aList
  
set desc1 to NSSortDescriptor’s sortDescriptorWithKey:aKey ascending:ascendingF selector:“compare:”
  
set bList to (aArray’s sortedArrayUsingDescriptors:{desc1}) as list
  
return bList
end sort1DRecList

★Click Here to Open This Script 

2018/01/05 Coloursで指定色の輝度をMAX状態に変更した色を取得する

オープンソースの「Colours」(By Ben Gordon)をフレームワーク化したcolorsKitを呼び出して、指定色の輝度を最大状態に変更した色のデータを計算するAppleScriptです。

4471e317-f308-4575-81fa-9cfe7b3bca1a.png

実行には、colorsKit.frameworkのインストールを必要とします。アーカイブをダウンロードして展開し、~/Library/Frameworksフォルダに入れてください(あくまで自己責任で)。

もともと、Coloursにlighten: というメソッドがあり、「指定色の現在の輝度よりも相対的にどの程度明るくするか」という動作内容だったので、最大値(1.0)を指定すると、現在の値+1.0という結果になってしまい、最大値(1.0)を超える数値が返ってきていました。そのため、AppleScript側でNSColorから値を取得する際に最大値チェックを行なっています。

–> Download Framework Binary

AppleScript名:Coloursで指定色の輝度をMAX状態に変更した色を取得する
– Created 2018-01-03 by Takaaki Naganoya
– 2018 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”
use framework “colorsKit” –https://github.com/bennyguitar/Colours
–http://piyocast.com/as/archives/5085

property NSColor : a reference to current application’s NSColor

set {rVal, gVal, bVal} to choose color
set aCol to makeNSColorFromRGBAval(rVal, gVal, bVal, 65535, 65535) of me

set bCol to aCol’s lighten:1.0

set cList to retColListFromNSColor(bCol, 65535)
choose color default color cList

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

–NSColorからRGBの値を取り出す
on retColListFromNSColor(aCol, aMAX as integer)
  set aRed to round ((aCol’s redComponent()) * aMAX) rounding as taught in school
  
set aGreen to round ((aCol’s greenComponent()) * aMAX) rounding as taught in school
  
set aBlue to round ((aCol’s blueComponent()) * aMAX) rounding as taught in school
  
  
if aRed > aMAX then set aRed to aMAX
  
if aGreen > aMAX then set aGreen to aMAX
  
if aBlue > aMAX then set aBlue to aMAX
  
  
return {aRed, aGreen, aBlue}
end retColListFromNSColor

★Click Here to Open This Script 

2018/01/04 Coloursで色バリエーション展開を計算して表示 v2

オープンソースの「Colours」(By Ben Gordon)をフレームワーク化したcolorsKitを呼び出して、指定色のカラーバリエーションを4通り計算して表示するAppleScriptです。

実行には、colorsKit.frameworkのインストールを必要とします。アーカイブをダウンロードして展開し、~/Library/Frameworksフォルダに入れてください(あくまで自己責任で)。スクリプトエディタ上でControl-Command-Rにより実行します。

–> Download Framework Binary

col2.png

col1_resized.png

Coloursが用意しているColorSchemeAnalagous, ColorSchemeMonochromatic, ColorSchemeTriad, ColorSchemeComplementaryの4通りのカラーバリエーション計算を行なってNSColorWellで表示します。

AppleScript名:Colorsで色バリエーション展開を計算して表示 v2
– Created 2017-12-20 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.5″
use scripting additions
use framework “Foundation”
use framework “AppKit”
use framework “colorsKit” –https://github.com/bennyguitar/Colours
–http://piyocast.com/as/archives/5081

property NSView : a reference to current application’s NSView
property NSColor : a reference to current application’s NSColor
property NSString : a reference to current application’s NSString
property NSScreen : a reference to current application’s NSScreen
property NSButton : a reference to current application’s NSButton
property NSWindow : a reference to current application’s NSWindow
property NSColorWell : a reference to current application’s NSColorWell
property NSWindowController : a reference to current application’s NSWindowController

property windisp : false

set aWidth to 500
set aHeight to 250

set {rVal, gVal, bVal} to choose color
set aNSCol to makeNSColorFromRGBAval(rVal, gVal, bVal, 65536, 65536) of me

dispCustomView(aWidth, aHeight, “Color Variation Result”, “OK”, 180, aNSCol) of me

on dispCustomView(aWidth as integer, aHeight as integer, aTitle as text, aButtonMSG as text, timeOutSecs as number, aNSCol)
  
  
–Check If this script runs in foreground
  
if not (current application’s NSThread’s isMainThread()) as boolean then
    error “This script must be run from the main thread (Command-Control-R in Script Editor).”
  end if
  
  
set (my windisp) to true
  
  
  
–Buttonをつくる
  
set bButton to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(aWidth / 4, 0, aWidth / 2, 40)))
  
bButton’s setTitle:aButtonMSG
  
bButton’s setButtonType:(current application’s NSMomentaryLightButton)
  
bButton’s setBezelStyle:(current application’s NSRoundedBezelStyle)
  
bButton’s setKeyEquivalent:(return)
  
bButton’s setTarget:me
  
bButton’s setAction:(“clicked:”)
  
  
–NSViewをつくる
  
set aNSV to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aHeight, aWidth))
  
aNSV’s addSubview:bButton
  
aNSV’s setNeedsDisplay:true
  
  
–NSColorWellをつくる
  
repeat with ii from 0 to 3 by 1
    set colorArray1 to (aNSCol’s colorSchemeOfType:ii) as list
    
set the beginning of colorArray1 to aNSCol
    
set tmpLen to (length of colorArray1)
    
set aStep to 0
    
repeat with i in colorArray1
      set aColorWell to (NSColorWell’s alloc()’s initWithFrame:(current application’s NSMakeRect((100 * aStep), (((3 - ii) * 50) + 40), 100, 40)))
      (
aColorWell’s setColor:i)
      (
aColorWell’s setBordered:true)
      (
aNSV’s addSubview:aColorWell)
      
set aStep to aStep + 1
    end repeat
  end repeat
  
  
set aWin to makeWinWithView(aNSV, aWidth, aHeight, aTitle, 1.0)
  
  
set wController to NSWindowController’s alloc()
  
wController’s initWithWindow:aWin
  
wController’s showWindow:me
  
  
aWin’s makeKeyAndOrderFront:me
  
  
set aCount to timeOutSecs * 10 –timeout seconds * 10
  
repeat aCount times
    if (my windisp) = false then
      exit repeat
    end if
    
delay 0.1
  end repeat
  
  
my closeWin:aWin
  
end dispCustomView

–Button Clicked Event Handler
on clicked:aSender
  set (my windisp) to false
end clicked:

–make Window for Input
on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV)
  set aScreen to NSScreen’s mainScreen()
  
set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
  
set aBacking to current application’s NSTitledWindowMask
  
set aDefer to current application’s NSBackingStoreBuffered
  
  
– Window
  
set aWin to NSWindow’s alloc()
  (
aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen)
  
  
aWin’s setTitle:aTitle
  
aWin’s setDelegate:me
  
aWin’s setDisplaysWhenScreenProfileChanges:true
  
aWin’s setHasShadow:true
  
aWin’s setIgnoresMouseEvents:false
  
aWin’s setLevel:(current application’s NSNormalWindowLevel)
  
aWin’s setOpaque:false
  
aWin’s setAlphaValue:alphaV –append
  
aWin’s setReleasedWhenClosed:true
  
aWin’s |center|()
  
aWin’s makeKeyAndOrderFront:(me)
  
  
– Set Custom View
  
aWin’s setContentView:aView
  
  
return aWin
end makeWinWithView

–close win
on closeWin:aWindow
  repeat with n from 10 to 1 by -1
    (aWindow’s setAlphaValue:n / 10)
    
delay 0.02
  end repeat
  
aWindow’s |close|()
end closeWin:

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

★Click Here to Open This Script 

2017/12/29 任意の2色の色差ΔEを求める

オープンソースの「Colours」(By Ben Gordon)をフレームワーク化したcolorsKitを呼び出して、指定の2色の色差(ΔE)を計算するAppleScriptです。

色差を求めるためには、RGB→XYZ→L*a*b*と変換する必要があるわけですが、Colours内で計算してくれます。本ScriptではAppleScriptのchoose colorコマンドの実行結果をCocoaの色データに変換してColours内のメソッドを呼び出しています。

実行には、colorsKit.frameworkのインストールを必要とします。アーカイブをダウンロードして、~/Library/Frameworksフォルダに入れてください(あくまで自己責任で)。

–> Download Framework Binary

AppleScript名:任意の2色の色差ΔEを求める
– Created 2017-12-29 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “colorsKit” –https://github.com/bennyguitar/Colours
use framework “AppKit”
–http://piyocast.com/as/archives/5075

property NSColor : a reference to current application’s NSColor

set aASCol to choose color
set bASCol to choose color

set aCocoaList to retCocoaColorList(aASCol, 65535) of me
set bCocoaList to retCocoaColorList(bASCol, 65535) of me

set aCol to NSColor’s colorFromRGBAArray:aCocoaList
set bCol to NSColor’s colorFromRGBAArray:bCocoaList

set aDist to aCol’s distanceFromColor:bCol type:2 –ColorDistanceCIE2000

return aDist

–Convert “choose color” RGB list (0-65535) to Cocoa color RGBA Array (0.0-1.0)
on retCocoaColorList(aColorList, aMax)
  set cocoaColorList to {}
  
repeat with i in aColorList
    set the end of cocoaColorList to i / aMax
  end repeat
  
set the end of cocoaColorList to 1.0
  
return cocoaColorList
end retCocoaColorList

★Click Here to Open This Script