2014-02-09 2 views
0

129a8f과 같은 CSS 16 진수 색상을 choose color default color xxx (이 경우 {4626, 39578, 36751})에 적합한 형식으로 변환하려고합니다. 트릭을 수행하는 루비 코드가 있습니다.CSS 색상을 16 비트 rgb로 변환

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로도 동일한 코드가 필요합니다. 어떤 포인터?

+0

너 한테은 간단하다 - 2 각 쌍에 16 진수 문자열을 깰 -'129a8f'을 나타내는'12 9A 8f'을 제공합니다 R, G 및 B 각각. 이 16 진수 각각을 등가 정수로 변환하면 필요한 결과를 얻을 수 있습니다. 이제 원하는 언어로 구현할 수 있습니다. –

+0

@AshisKumarSahoo : 감사합니다.하지만 제 질문은 AppleScript에 관한 것입니다. – georg

답변

2

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 

-

또는 루비없이 AppleScript 스크립트는

property hexChrs : "abcdef" 

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 
+0

완벽한, 고마워. – georg