RGB Farbcode zu HTML und umgekehrt

Wenn man in AppleScript eine Farbe angibt (z.B. für einen Text) so geschieht das über eine RGB-Farbwert-Angabe wie {255,5645,4565}. Das Besonder daran, es können eben 16,7 Millionen Farbwerte sein (256*256*256) + jeweils 256 mögliche Werte für den Grad der Transparenz (so glaube ich). Ich fand auch schnell eine Routine zum umwandeln solcher RGB-Werte in HTML-Werte, doch für die andere Richtung schien es nichts zu geben… hier also die Orig-Routine von macosxautomation und meine Routine, die das exakte Gegenteil bewirkt.

set html to RBG_to_HTML({0, 22359, 30583})

return HTML_to_RGB(characters 2 through -1 of html as text)

on RBG_to_HTML(RGB_values)

— NOTE: this sub-routine expects the RBG values to be from 0 to 65535

set the hex_list to {“0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “A”, “B”, “C”, “D”, “E”, “F”}

set the the hex_value to “”

repeat with i from 1 to the count of the RGB_values

set this_value to (item i of the RGB_values) div 256

if this_value is 256 then set this_value to 255

set x to item ((this_value div 16) + 1) of the hex_list

set y to item (((this_value / 16 mod 1) * 16) + 1) of the hex_list

set the hex_value to (the hex_value & x & y) as string

end repeat

return (“#” & the hex_value) as string

end RBG_to_HTML

on HTML_to_RGB(HTML_value)

— NOTE: this sub-routine expects the HTML values to have 6 characters

set the hex_list to {“0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “A”, “B”, “C”, “D”, “E”, “F”}

set the the RGB_value to {}

set HTML_values to {characters 1 through 2 of HTML_value as text, characters 3 through 4 of HTML_value as text, characters 5 through 6 of HTML_value as text}

repeat with HTML_value in HTML_values

repeat with i from 1 to count of hex_list

if character 1 of HTML_value = item i of hex_list then

set firstvalue to (i – 1) * 16

end if

end repeat

repeat with i from 1 to count of hex_list

if character 2 of HTML_value = item i of hex_list then

set secondvalue to (i – 1) * 1

end if

end repeat

set RGB_value to RGB_value & (firstvalue + secondvalue) * 257

end repeat

return RGB_value

end HTML_to_RGB

Leave a Reply