AppleScript:选择颜色,返回颜色名称

问题描述 投票:0回答:1

我想做的是:

set retry to true
repeat while retry is true
    display dialog "In the next window you'll have to choose a color!" buttons {"Cancel", "COLORR!"} cancel button 1 default button 2
    
    set Chosencolor1 to (choose color)
    
    
    display dialog "The chosen color is: " & Chosencolor1 & " Is this right?" buttons {"NO", "YES"}
    if the button returned of the result is "no" then
        set retry to true
        display dialog "Retry is now: " & retry
    else
        set retry to false
        display dialog "Retry is now: " & retry
    end if
end repeat
end

但是当我运行这段代码时它会返回一个颜色代码作为数字。但我想要的是它会返回一个颜色名称,如:淡蓝色、蓝色、绿色

我知道的唯一方法是做类似的事情:

if the chosencolor1 is "000" then
set the chosencolor1 to "Black"
end if
end

还有其他更简单的方法吗?或者这是不可能的?

macos colors applescript osx-yosemite color-codes
1个回答
0
投票

来自 AppleScript 语言指南选择颜色

Result 所选颜色,表示为三个整数的列表 从 0 到 65535 分别对应红色、绿色和蓝色分量 一种颜色;例如,{0, 65535, 0} 代表绿色。

正如您所猜测的那样,获得文本响应的唯一方法是使用用户选择的列表的值自行对其进行编程。

这里是一个非常简单的例子,将所选 RGB 整数的所有值相加,并确定它是否更接近黑色或白色:

set myColor to choose color
display dialog ("You have chosen: " & my fetchColorName(myColor))

to fetchColorName(clst)
    set totalColorValue to ((item 1 of clst) + (item 2 of clst) + (item 3 of clst))
    set blackWhiteMidpoint to (196605 / 2)
    if totalColorValue < blackWhiteMidpoint then
        set colorName to "A color closer to black than white."
    else
        set colorName to "A color closer to white than black."
    end if
    return colorName
end fetchColorName
© www.soinside.com 2019 - 2024. All rights reserved.