HTML and BBCode Color Picker

I often have to choose colors for article text in MacScripter.net, and for forum entries in several places, so I wrote this script to extract 8-bit colors suitable for the web or a forum from Apple’s color picker (which returns 16-bit colors in decimal notation). I prefer to use it by running the script, clicking the magnifier icon, picking a sample of the color I want, closing the color picker, and then choosing the form I want in the dialog box presented. The answer goes into the clipboard ready to paste into your doc.

-- I find this script endlessly useful in preparing both the MacScripter.net articles (HTML), and forum entries (BBCode).
-- set the initial shade in the palette to light grey
set C10 to choose color default color {50000, 50000, 50000}
set Val16 to ""
repeat with C in C10
	if contents of C = 0 then
		set Val16 to Val16 & "00"
	else
		set Val16 to Val16 & |10->16|(C) -- as 8-bit colors
	end if
end repeat

set B to button returned of (display dialog "The color values for the color chosen:" & return & return & Val16 default button 3 buttons {"BB to Clip", "Num to Clip", "HTML to Clip"})
if B = "BB to Clip" then
	set the clipboard to "[color=#" & Val16 & "]"
else if B = "Num to Clip" then
	set the clipboard to Val16
else
	set the clipboard to "<font color=\"#" & Val16 & "\">"
end if

on |10->16|(n10)
	set n10 to n10 div 256 -- to get 8-bit
	if n10 is less than 0 then set n10 to -n10
	set Ch16 to "0123456789ABCDEF"
	set Txt16 to ""
	set N to n10
	set Dgt16 to 1
	repeat
		if N is 0 then exit repeat
		set nx to N div Dgt16
		set mx to nx mod 16
		set Txt16 to (character (mx + 1) of Ch16) & Txt16
		set N to N - (mx * Dgt16)
		set Dgt16 to (Dgt16 * 16)
	end repeat
	if length of Txt16 = 1 then set Txt16 to "0" & Txt16
	return Txt16
end |10->16|

Hi Adam,

thanks, a very useful script.
Here a suggestion with a “specialized” subroutine

property hexList : "0123456789ABCDEF"

set C10 to choose color default color {50000, 50000, 50000}
set Val16 to RBG2HEX(C10)

set B to button returned of (display dialog "The color values for the color chosen:" & return & return & Val16 default button 3 buttons {"BB to Clip", "Num to Clip", "HTML to Clip"})
if B = "BB to Clip" then
	set the clipboard to "[color=#" & Val16 & "]"
else if B = "Num to Clip" then
	set the clipboard to Val16
else
	set the clipboard to "<font color=\"#" & Val16 & "\">"
end if

on RBG2HEX(RGB_List)
	set hexValue to ""
	repeat with i in RGB_List
		if contents of i = 0 then
			set hexValue to hexValue & "00"
		else
			set aValue to i div 256
			set nibH to character ((aValue div 16) + 1) of hexList
			set nibL to character (((aValue / 16 mod 1) * 16) + 1) of hexList
			set hexValue to hexValue & nibH & nibL
		end if
	end repeat
	return hexValue
end RBG2HEX

Certainly “crisper”.

Yes, what a great idea!

Here’s another suggested improvement to the handler:

property hexList : "0123456789ABCDEF"

on RBG16toHex8(rgbValues)
    set hexString to ""
    repeat with comp in rgbValues
        set comp to comp div 256
        set hexString to hexString & character (comp div 16 + 1) of hexList
        set hexString to hexString & character (comp mod 16 + 1) of hexList
    end repeat
    return hexString
end RBG16toHex8

Also, for the HTML option, the preferred way of colorizing text is using a tag’s style attribute, like so:

<span style="color: #7F23C3">Pink text</span>

The attribute’s text is in the CSS (Cascading Style Sheet) language.

So, here’s the script changed to reflect that, with the above handler condensed a little further:

property hexList : "0123456789ABCDEF"

on RBG16toHex8(rgbValues)
    set hexString to ""
    repeat with comp in rgbValues
        set hexString to hexString & character (comp div 4096 + 1) of hexList & character (comp div 256 mod 16 + 1) of hexList
    end repeat
    return hexString
end RBG16toHex8


set chosenColor to choose color default color {50000, 50000, 50000}
set hexValue to RBG16toHex8(chosenColor)

set B to button returned of (display dialog "Copy color '" & hexValue & "' to the clipboard in which form?" default button 3 buttons {"BBCode", "Hex Value", "HTML"})
if B = "BBCode" then
    set the clipboard to "[color=#" & hexValue & "]"
else if B = "HTML" then
    set the clipboard to "<span style=\"color: #" & hexValue & "\">"
else
    set the clipboard to hexValue
end if