Function: ansi-color--code-as-hex

ansi-color--code-as-hex is a byte-compiled function defined in ansi-color.el.gz.

Signature

(ansi-color--code-as-hex COLOR)

Documentation

Convert COLOR to hexadecimal string representation.

COLOR is an ANSI color code. If it is between 16 and 255 inclusive, it corresponds to a color from an 8-bit color cube. If it is greater or equal than 256, it is subtracted by 256 to directly specify a 24-bit color.

Return a hexadecimal string, specifying the color, or nil, if COLOR is less than 16.

Source Code

;; Defined in /usr/src/emacs/lisp/ansi-color.el.gz
(defun ansi-color--code-as-hex (color)
  "Convert COLOR to hexadecimal string representation.
COLOR is an ANSI color code.  If it is between 16 and 255
inclusive, it corresponds to a color from an 8-bit color cube.
If it is greater or equal than 256, it is subtracted by 256 to
directly specify a 24-bit color.

Return a hexadecimal string, specifying the color, or nil, if
COLOR is less than 16."
  (cond
   ((< color 16) nil)
   ((>= color 256) (format "#%06X" (- color 256)))
   ((>= color 232) ;; Grayscale
    (format "#%06X" (* #x010101 (+ 8 (* 10 (- color 232))))))
   (t ;; 6x6x6 color cube
    (setq color (- color 16))
    (let ((res 0)
          (frac (* 6 6)))
      (while (<= 1 frac)                ; Repeat 3 times
        (setq res (* res #x000100))
        (let ((color-num (mod (/ color frac) 6)))
          (unless (zerop color-num)
            (setq res (+ res #x37 (* #x28 color-num)))))
        (setq frac (/ frac 6)))
      (format "#%06X" res)))))