Copy RGB or HEX color values from the Color Picker

Mar 06, '08 07:30:04AM

Contributed by: b.leopard

As one might know, Apple has included a system-wide application called Color Picker with OS X. You can call it in every application that supports it, like TextEdit or Mail.app. Color Picker allows you to modify colors, choosing colors from a color wheel, HSB sliders, etc. (More about the Color Picker.)

Problem: The Color Picker is a fantastic tool to select a color, but you can't copy the result for use in other programs. You can extend Color Picker with the free Hex Color Picker, but not everybody likes to receive a HEX code. What about copying the RGB values as text to insert them wherever you want?

Here's my solution: A little AppleScript calls Color Picker and parses the result. A dialog appears, asking you about the desired format for the copied values:

Here's the code:
set the RGB16bit_list to (choose color default color {65535, 50584, 0})
display dialog "Which notation should be copied to the clipboard?" buttons {"8-bit RGB", "16-bit RGB", "HEX"} default button 3
if the button returned of the result is "HEX" then
	-- convert choosen color to HEX with leading #
	set the formatedColor to my RBG_to_HEX(RGB16bit_list)
else if the button returned of the result is "8-bit RGB" then
	-- convert choosen color to (class:RGB color, red:8bitvalue, green:8bitvalue, blue:8bitvalue)
	set the formatedColor to my RBG_to_RGB8bit(RGB16bit_list)
else
	-- convert choosen color to (class:RGB color, red:16bitvalue, green:16bitvalue, blue:16bitvalue)
	set the formatedColor to my RBG_to_RGB16bit(RGB16bit_list)
end if
set the clipboard to formatedColor

on RBG_to_HEX(RGB_values)
	-- this subroutine was taken from "http://www.apple.com/applescript/sbrt/sbrt-04.html"
	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)
end RBG_to_HEX

on RBG_to_RGB8bit({r, g, b})
	set r to (r ^ 0.5) div 1
	set g to (g ^ 0.5) div 1
	set b to (b ^ 0.5) div 1
	return "{class:8-bit RGB color, red:" & r & ", green:" & g & ", blue:" & b & "}" as string
end RBG_to_RGB8bit

on RBG_to_RGB16bit({r, g, b})
	return "{class:16-bit RGB color, red:" & r & ", green:" & g & ", blue:" & b & "}" as string
end RBG_to_RGB16bit
Installation and usage: Copy the code above to an empty Script Editor document, save it to your users script folder (~/Library » Scripts). Run the program from the Script menu, choose the color in the Color Picker, press OK at the bottom of the window, and select the preferred notation. Then paste it to wherever you want.

ATTENTION: If you are copying from the "color sliders" tab (second from left), than the selcted ColorSync profile will dramatically influence the values. I found no way to get the uncorrected color, nor did I find a solution to receive the name of the choosen colorsync profile.

[robg adds: This works as described.]

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080229093118547