For emacs lisp coders, there's a newish color package 〔color.el〕, bundled with emacs 24.3 at least.
the package lets you convert colors from various models and formats. ⁖ RGB, HSL, HSV, named color values, and things like finding the color complement.
color-rgb-to-hex and you'll have a link to it.
it's written by Julien Danjou, Drew Adams. Superb. Thanks guys.
For more info and sample code, see: http://ergoemacs.org/emacs/elisp_convert_rgb_hsl_color.html