Applescript 将css颜色转换为16位rgb

Applescript 将css颜色转换为16位rgb,applescript,hex,Applescript,Hex,我正在尝试将css十六进制颜色(如129a8f转换为适合选择默认颜色xxx(在本例中,{46263957836751})的格式。我有一个ruby代码来完成这个任务 if m = input.match('([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})') then clr = "{#{m[1].hex*257}, #{m[2].hex*257}, #{m[3].hex*257}}" 现在我需要在AppleScript中使用相同的代码(我不知

我正在尝试将css十六进制颜色(如
129a8f
转换为适合
选择默认颜色xxx
(在本例中,
{46263957836751}
)的格式。我有一个ruby代码来完成这个任务

if m = input.match('([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})') then
  clr = "{#{m[1].hex*257}, #{m[2].hex*257}, #{m[3].hex*257}}"

现在我需要在AppleScript中使用相同的代码(我不知道;)。有任何指针吗?

您可以在AppleScript脚本中使用
ruby

set x to text returned of (display dialog "Type a css hex color." default answer "129a8f")
set xx to do shell script "/usr/bin/env ruby -e  'x=\"" & x & "\"; if m= x.match(\"([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})\") then puts \"{#{m[1].hex*257}, #{m[2].hex*257}, #{m[3].hex*257}}\"; end' "
if xx is "" then
    display alert x & " is not a valid css hex color" buttons {"OK"} cancel button "OK"
else
    set xxx to run script xx -- convert ruby string to AppleScript list
    set newColor to choose color default color xxx
end if
--

或者没有ruby的AppleScript脚本

property hexChrs : "0123456789abcdef"

set x to text returned of (display dialog "Type a css hex color." default answer "129a8f")
set xxx to my cssHexColor_To_RGBColor(x)
set newColor to choose color default color xxx

on cssHexColor_To_RGBColor(h) -- convert 
    if (count h) < 6 then my badColor(h)
    set astid to text item delimiters
    set rgbColor to {}
    try
        repeat with i from 1 to 6 by 2
            set end of rgbColor to ((my getHexVal(text i of h)) * 16 + (my getHexVal(text (i + 1) of h))) * 257
        end repeat
    end try
    set text item delimiters to astid
    if (count rgbColor) < 3 then my badColor(h)
    return rgbColor
end cssHexColor_To_RGBColor

on getHexVal(c)
    if c is not in hexChrs then error
    set text item delimiters to c
    return (count text item 1 of hexChrs)
end getHexVal

on badColor(n)
    display alert n & " is not a valid css hex color" buttons {"OK"} cancel button "OK"
end badColor
属性hexChrs:“0123456789abcdef”
将x设置为返回的文本(显示对话框“键入css十六进制颜色”。默认答案为“129a8f”)
将xxx设置为我的cssHexColor\U设置为\U RGB颜色(x)
设置newColor以选择颜色默认颜色xxx
关于cssHexColor_到RGBColor(h)——转换
如果(计数h)<6,则我的坏颜色(h)
将astid设置为文本项分隔符
将rgbColor设置为{}
尝试
重复从1到6乘2的i
将rgbColor的结尾设置为((my getHexVal(h的文本i))*16+(my getHexVal(h的文本i+1))*257
结束重复
结束尝试
将文本项分隔符设置为astid
如果(计数rgbColor)<3,则我的坏颜色(h)
返回RGB颜色
结束cssHexColor\u至\u RGB颜色
关于getHexVal(c)
如果c不在hexChrs中,则出现错误
将文本项分隔符设置为c
返回(计算hexChrs的文本项1)
结束getHexVal
关于badColor(n)
显示警报n&“不是有效的css十六进制颜色”按钮{“确定”}取消按钮“确定”
结束坏颜色

算法很简单-将十六进制字符串分成两对,每组2对-
129a8f
给出分别代表R、G和B的
12 9a 8f
。将这些十六进制转换为等效整数将得到所需的结果。现在你可以用你喜欢的任何语言实现了。@AshisKumarSahoo:谢谢,但我的问题是关于Applescript的。