Skip to main content
4 of 4
Minor improvement/future-proofing. This doesn't change the current behaviour at all, so the docstring is unchanged; but if `color-rgb-to-hex` ever handles digits-per-component values besides 2 and 4 then this version can supply them via a numeric prefix arg.
phils
  • 53.1k
  • 3
  • 84
  • 127

How about:

(defun my-insert-color-hex (&optional arg)
  "Select a color and insert its 24-bit hexadecimal RGB format.

With prefix argument \\[universal-argument] insert the 48-bit value."
  (interactive "*P")
  (let ((buf (current-buffer)))
    (list-colors-display
     nil nil `(lambda (name)
                (interactive)
                (quit-window)
                (with-current-buffer ,buf
                  (insert (apply #'color-rgb-to-hex
                                 (nconc (color-name-to-rgb name)
                                        (unless (consp ',arg)
                                          (list (or ,arg 2)))))))))))
phils
  • 53.1k
  • 3
  • 84
  • 127