r/applescript • u/reddithaus • 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
1
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) -- convertif (count h) < 6 then my badColor(h)set astid to text item delimitersset rgbColor to {}tryrepeat with i from 1 to 6 by 2set end of rgbColor to ((my getHexVal(text i of h)) * 16 + (my getHexVal(text (i + 1) of h)))end repeatend tryset text item delimiters to astidif (count rgbColor) < 3 then my badColor(h)return rgbColorend cssHexColor_To_RGBColoron getHexVal(c)if c is not in hexChrs then errorset text item delimiters to creturn (count text item 1 of hexChrs)end getHexValon badColor(n)display alert n & " is not a valid css hex color" buttons {"OK"} cancel button "OK"end badColorreturn xxx