Skip to main content
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.
Source Link
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)
                                        (orunless (consp ',arg)
                                          (list (or ,arg 2)))))))))))

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)
                                        (or ',arg (list 2))))))))))

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)))))))))))
Use 24-bit output by default, rather than 48-bit. As my original answer made no comment on the matter, I suspect I was getting 24-bit values with the original code at the time I wrote it, but nowadays (at least) `color-rgb-to-hex` defaults to 4 hex characters per colour, rather than the typical 2.
Source Link
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#'color-rgb-to-hex
                                 (nconc (color-name-to-rgb name)
                                        (or ',arg (list 2))))))))))

How about:

(defun my-insert-color-hex ()
  "Select a color and insert its hexadecimal format."
  (interactive "*")
  (let ((buf (current-buffer)))
    (list-colors-display
     nil nil `(lambda (name)
                (interactive)
                (quit-window)
                (with-current-buffer ,buf
                  (insert (apply 'color-rgb-to-hex
                                 (color-name-to-rgb name))))))))

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)
                                        (or ',arg (list 2))))))))))
added 107 characters in body
Source Link
phils
  • 53.1k
  • 3
  • 84
  • 127

How about:

(defun my-insert-color-hex ()
  "Select a color and insert its hexadecimal format."
  (interactive "*")
  (let ((buf (current-buffer)))
    (list-colors-display
     nil nil `(lambda (name)
                (interactive)
                (quit-window)
                (with-current-buffer ,buf
                  (insert (apply 'color-rgb-to-hex
                                 (color-name-to-rgb name))))))))

How about:

(defun my-insert-color-hex ()
  "Select a color and insert its hexadecimal format."
  (interactive)
  (list-colors-display
   nil nil (lambda (name)
             (interactive)
             (quit-window)
             (insert (apply 'color-rgb-to-hex
                            (color-name-to-rgb name))))))

How about:

(defun my-insert-color-hex ()
  "Select a color and insert its hexadecimal format."
  (interactive "*")
  (let ((buf (current-buffer)))
    (list-colors-display
     nil nil `(lambda (name)
                (interactive)
                (quit-window)
                (with-current-buffer ,buf
                  (insert (apply 'color-rgb-to-hex
                                 (color-name-to-rgb name))))))))
Source Link
phils
  • 53.1k
  • 3
  • 84
  • 127
Loading