Convert colors

Hello to all generous helpers,

Is there an easy way to convert a hex color value to it’s RGB 16 bit value equivalent ?

ex: “#0000DD” to {0, 0, 56797}

thank you

Hi Jeremy,

welcome :slight_smile:

quick&dirty


property HEXvalue : "123456789ABCDEF"

set webColor to "#0000DD"
set RGB to {}
if webColor starts with "#" then
	repeat with i from 2 to 6 by 2
		set end of RGB to convertHEX_DEZ(text i thru (i + 1) of webColor)
	end repeat
end if
RGB --> {0, 0, 56576}

on convertHEX_DEZ(v)
	return ((offset of (character 1 of v) in HEXvalue) * 16 + (offset of (character 2 of v) in HEXvalue)) * 256
end convertHEX_DEZ

Stefan, thank you very much for your answer

…but I encounter a small problem with your script. When I enter “#FFFFFF”, the choose color picker shows the wrong color :frowning:


property HEXvalue : "123456789ABCDEF"
set webColor to "#FFFFFF" -- white

on convertHEX_DEZ(v)
   return ((offset of (character 1 of v) in HEXvalue) * 16 + (offset of (character 2 of v) in HEXvalue)) * 256
end convertHEX_DEZ


set RGB to {}
if webColor starts with "#" then
   repeat with i from 2 to 6 by 2
       set end of RGB to convertHEX_DEZ(text i thru (i + 1) of webColor)
   end repeat
end if

choose color default color RGB -- > {65280, 65280, 65280} = opens with "~white~" colr selected 

Being completely illiterate into hexadecimal, I don’t know where to make the suitable modifications…

Best regards

does this work?


.
on convertHEX_DEZ(v)
	return (((offset of (character 1 of v) in HEXvalue) * 16 + (offset of (character 2 of v) in HEXvalue)) + 1) * 255
end convertHEX_DEZ
.

Thank you Stefan, but I don’t either get the expected results with your last modification.

I finally ended to this script, but I don’t really like it I because of the use of two hex property lists.

property hexList : "123456789ABCDEF"
property hexList2 : "0123456789ABCDEF"
--
to RBG16toHex8(AppleRGB16)
	set hexString to ""
	repeat with primary in AppleRGB16
		set hexString to hexString & character (primary div 4096 + 1) of hexList2 & character (primary div 256 mod 16 + 1) of hexList2
	end repeat
	return hexString
end RBG16toHex8
--
to Hex8toRGB16(htmColor)
	set c to count hexList
	set RGB16 to {}
	set HEXtriplets to {{characters 1 thru 2 of htmColor}, {characters 3 thru 4 of htmColor}, {characters 5 thru 6 of htmColor}}
	repeat with thisTriplet in HEXtriplets
		set thisTriplet to thisTriplet as string
		set n to (((offset of thisTriplet's character 1 in hexList) * 16) + ((offset of thisTriplet's character 2 in hexList) * 1))
		set n to n * 257
		set RGB16 to RGB16 & n
	end repeat
	return RGB16
end Hex8toRGB16
-----------------------------------------------------------------

set defaultHex to "#000000"

if defaultHex starts with "#" then
	set defaultHex to defaultHex's text 2 thru -1
	set AppleScript's text item delimiters to ""
	try
		set chosenColor to choose color default color Hex8toRGB16(defaultHex)
	on error
		return
	end try
	set hexValue to RBG16toHex8(chosenColor)
end if

great, here the “merge” of the two scripts


property hexList : "0123456789ABCDEF"
--
to RBG16toHex8(AppleRGB16)
	set hexString to ""
	repeat with primary in AppleRGB16
		set hexString to hexString & character (primary div 4096 + 1) of hexList & character (primary div 256 mod 16 + 1) of hexList
	end repeat
	return hexString
end RBG16toHex8
--
to Hex8toRGB16(htmColor) -- format #xxxxxx
	set RGB16 to {}
	repeat with i from 2 to 6 by 2
		set end of RGB16 to ((((offset of htmColor's character i in hexList) - 1) * 16) + (offset of htmColor's character (i + 1) in hexList) - 1) * 257
	end repeat
	return RGB16
end Hex8toRGB16
-----------------------------------------------------------------

set defaultHex to "#FFFFFF"

if defaultHex starts with "#" then
	try
		set chosenColor to choose color default color Hex8toRGB16(defaultHex)
	on error
		return
	end try
	set hexValue to RBG16toHex8(chosenColor)
end if