将CSS颜色转换为16位RGB

乔治

我正在尝试将css十六进制颜色129a8f转换为适合的格式choose color default color xxx(在这种情况下为{4626, 39578, 36751})。我有红宝石代码可以解决问题

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中使用它(我不知道;)。有指针吗?

杰克300

您可以ruby在AppleScript脚本中使用

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在C ++中将16位十六进制颜色转换为RGB888值

来自分类Dev

C#将4位颜色(= ConsoleColor)转换为RGB并返回

来自分类Dev

将Android颜色转换为HEX或RGB

来自分类Dev

将RGB颜色转换为XY

来自分类Dev

将0.75 RGB的RGB颜色值转换为RGBA

来自分类Dev

php函数将RYB颜色转换为RGB颜色

来自分类Dev

PHP将24位颜色转换为4位

来自分类Dev

PHP将24位颜色转换为4位

来自分类Dev

将32位数字转换为16位或更少

来自分类Dev

无法将青色的整数颜色转换为RGB

来自分类Dev

将RGB代码列表转换为Matplotlib颜色图

来自分类Dev

将RGB字符串转换为颜色类型

来自分类Dev

将RGBW颜色转换为标准RGB / HSB表示

来自分类Dev

Chrome DevTools将所有HEX颜色转换为RGB

来自分类Dev

使用OpenCV将YUV颜色空间转换为RGB

来自分类Dev

将RGBW颜色转换为标准RGB / HSB表示

来自分类Dev

将RGB颜色转换为x,y方向并返回

来自分类Dev

将int转换为16位无符号short

来自分类Dev

将图像转换为16位彩色图像

来自分类Dev

Android将音频转换为16位PCM

来自分类Dev

将int转换为16位无符号短整数

来自分类Dev

Android将音频转换为16位PCM

来自分类Dev

将图像16位灰度转换为位图16位灰度(构造函数错误)

来自分类Dev

位操作;将16位值转换为16个布尔值的数组?C语言

来自分类Dev

将16位立体声转换为16位单声道声音

来自分类Dev

将两种16位RGB颜色与alpha混合相结合

来自分类Dev

将两种16位RGB颜色与alpha混合相结合

来自分类Dev

Java-将十进制颜色转换为RGB颜色

来自分类Dev

PHP-如何将RGB颜色转换为CIE 1931颜色规格