HTMLカラー値からNSColorを作成し、Gray Scaleに変換したのちに明度情報を取得するAppleScriptです。
RGB値をHSV(HSB)に変換して明度情報を取得するバージョンも試してみたのですが、個人的にはこちらの処理の方がしっくりくる感じでした。
# コメント部分のWhiteとBlackの記述が逆だったので修正しておきました
AppleScript名:HTMLカラー値から明度を取得 v1.1.scptd |
— – Created by: Takaaki Naganoya – Created on: 2020/12/07 — – Copyright © 2020 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use scripting additions 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 set aCol to "#412a23" set wCol to calcBrightnessFromHTMLColorCodeStr(aCol) of me –> 0.245871186256 set aCol to "#000000" –Black set wCol to calcBrightnessFromHTMLColorCodeStr(aCol) of me –> 0.0 set aCol to "#FFFFFF" –White set wCol to calcBrightnessFromHTMLColorCodeStr(aCol) of me –> 1.0 set aCol to "#EEA096" set wCol to calcBrightnessFromHTMLColorCodeStr(aCol) of me –> 0.759391903877 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 |
More from my site
(Visited 60 times, 1 visits today)