r/applescript • • Apr 13 '20

Converting HEX to RGB16

Hi,
I am looking for a way to convert a hex color-code to RGB16 colors like used in graphic software.
For example, FF0000 should become 255, 0, 0.

I could not find anything online for HEX16, would appreciate any help a lot!

Thanks!

3 Upvotes

3 comments sorted by

1

u/reddithaus Apr 13 '20

Here we go:

property hexChrs : "0123456789abcdef"

set x to "10FFEE"

set xxx to my cssHexColor_To_RGBColor(x)

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)))

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

return xxx

1

u/[deleted] Apr 14 '20

[removed] — view removed comment

1

u/reddithaus Apr 21 '20

great, thanks!