16bitカラー値から明度を取得 v1.1
choose colorコマンドやiWorkアプリケーションが返してくる16ビットのカラー値({R,G,B})から明度情報を取得するAppleScriptです。0から1までの値を返し、0に近づくほど暗く、1に近づくほど明るい色であることを表現しています。
色情報同士を比較して、明るい箇所に指定している色データと暗い箇所に指定している色データを、RGB値から自動認識するような処理に使っています。
また、ファイル名から「章」(Chapter)の番号を検出して、実際のツメ(Index)の選択部分を暗い色で塗り直す処理も行なっています。
Pages書類の上に作成したツメ(辞書などの端に印刷されているAからZのインデックス)に対して、ツメのオブジェクトを(座標や形状から)自動認識したあとに、色の塗り分け情報を本ルーチンを用いて自動認識。濃い色を現在の選択箇所の色、明るい(淡い)色を通常の箇所の色情報として扱い、あらかじめツメに指定していた色情報を用いて実際の章構成を反映させてツメのオブジェクト(表)を再構成する処理に必須のものです。
明度情報の取得には、グレースケールに変換してグレー度(whiteComponent)を明度とみなして計算しています。直感的にこちらのほうが、まっとうな処理(HSB色空間の色に変換してからBrightnessを取得)よりもしっくり来る計算結果だったので採用しています。
おまけで、2つの色情報のうちどちらが暗いか、結果をインデックス値で返す処理ルーチンをつけています。これも、選択色と非選択色の自動識別のために必要なものです。
– Created by: Takaaki Naganoya
– Created on: 2022年01月20日
—
– Copyright © 2022 Piyomaru Software, All Rights Reserved
—
set a1Col to {26148, 65535, 58650}
set a1Col to choose color default color a1Col
set w1Col to calcBrightnessFrom16bitColorList(a1Col) of colorBrightnessKit
–> 0.900838315487
set a2Col to {2966, 23133, 21203}
set a2Col to choose color default color a2Col
set w2Col to calcBrightnessFrom16bitColorList(a2Col) of colorBrightnessKit
–> 0.37399661541–0に近いのでこちらのほうが暗い
set cIndRes to calcColorPairsDarkerCol(a1Col, a2Col) of colorBrightnessKit
–> {2, 1}–暗い順にインデックス値を返す
–色情報から明度を計算するKit。0に近い値が暗い。CMYKやグレースケール値は対象外。RGBのみ
script colorBrightnessKit
use AppleScript
use framework "Foundation"
use framework "AppKit"
use scripting additions
property parent : AppleScript
property NSColor : a reference to current application’s NSColor
property NSString : a reference to current application’s NSString
property NSAttributedString : a reference to current application’s NSAttributedString
property NSUTF16StringEncoding : a reference to current application’s NSUTF16StringEncoding
property NSDeviceWhiteColorSpace : a reference to current application’s NSDeviceWhiteColorSpace
–2つの16ビットカラー値でそれぞれ明度を計算し、暗いものから順にインデックス値を返す
on calcColorPairsDarkerCol(aCol as list, bCol as list)
set b1 to calcBrightnessFrom16bitColorList(aCol) of me
set b2 to calcBrightnessFrom16bitColorList(bCol) of me
if b1 ≥ b2 then
return {2, 1} –bColのほうが暗い
else
return {1, 2} –aColのほうが暗い
end if
end calcColorPairsDarkerCol
–16ビットカラー値から明度を計算
on calcBrightnessFrom16bitColorList(colList as list)
copy colList to {rVal, gVal, bVal}
–NSColorを作成
set aCol to makeNSColorFromRGBAval(rVal, gVal, bVal, 65535, 65535) of me
— グレースケール化
set gCol to aCol’s colorUsingColorSpaceName:(NSDeviceWhiteColorSpace)
set wComp to gCol’s whiteComponent() –whiteComponentを取得することで擬似的に明度情報を取得
return wComp
end calcBrightnessFrom16bitColorList
–HTMLカラー値あから明度を計算
on calcBrightnessFromHTMLColorCodeStr(aStr as string)
set {rVal, gVal, bVal} to rgbHex2nunList(aStr) of me
–NSColorを作成
set aCol to makeNSColorFromRGBAval(rVal, gVal, bVal, 255, 255) of me
— グレースケール化
set gCol to aCol’s colorUsingColorSpaceName:(NSDeviceWhiteColorSpace)
set wComp to gCol’s whiteComponent() –whiteComponentを取得することで擬似的に明度情報を取得
return wComp
end calcBrightnessFromHTMLColorCodeStr
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
on decodeCharacterReference(aStr)
set anNSString to NSString’s stringWithString:aStr
set theData to anNSString’s dataUsingEncoding:(NSUTF16StringEncoding)
set styledString to NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value)
set plainText to (styledString’s |string|()) as string
return plainText
end decodeCharacterReference
–HTMLコードのRGB 16進数コードを数値リストに変換
on rgbHex2nunList(aHexStr)
–エラーチェック
if aHexStr does not start with "#" then return false
if length of aHexStr is not equal to 7 then return false
set bHex to text 2 thru -1 of aHexStr
set {rStr, gStr, bStr} to {text 1 thru 2 of bHex, text 3 thru 4 of bHex, text 5 thru 6 of bHex}
set bList to {}
repeat with i in {rStr, gStr, bStr}
set j to contents of i
set the end of bList to aHexStrToNum(j) of me
end repeat
return bList
end rgbHex2nunList
–16進数文字列を10進数数値に変換する
on aHexStrToNum(hexStr)
set hStr to "0123456789ABCDEF"
set aNum to 0
set aLen to length of hexStr
repeat with i from aLen to 1 by -1
set aCon to contents of character i of hexStr
using terms from scripting additions
set aVal to (offset of aCon in hStr) – 1
end using terms from
set aNum to aNum + aVal * (16 ^ (aLen – i))
end repeat
return aNum as integer
end aHexStrToNum
end script
More from my site
- Intel MacとApple Silicon Macの速度差〜画像処理 Intel MacとApple Silicon Macの速度差〜画像処理
- Excel__Numbersセルアドレスの相互変換 Excel__Numbersセルアドレスの相互変換
- Keynoteで選択中のtext itemの冒頭のフォントを太くする v2 Keynoteで選択中のtext itemの冒頭のフォントを太くする v2
- iCalendarファイルの作成 iCalendarファイルの作成
- 国民の祝日を求める v7 国民の祝日を求める v7
- UI Browserがgithub上でソース公開され、オープンソースに UI Browserがgithub上でソース公開され、オープンソースに