Function: css--hex-to-rgb

css--hex-to-rgb is a byte-compiled function defined in css-mode.el.gz.

Signature

(css--hex-to-rgb)

Documentation

Convert CSS hex color at point to RGB format.

Return non-nil if a conversion was made.

Note that this function handles CSS colors specifically, and should not be mixed with those in color.el.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/css-mode.el.gz
(defun css--hex-to-rgb ()
  "Convert CSS hex color at point to RGB format.
Return non-nil if a conversion was made.

Note that this function handles CSS colors specifically, and
should not be mixed with those in color.el."
  (save-excursion
    (unless (or (eq (char-after) ?#)
                (eq (char-before) ?\())
      (backward-sexp))
    (when-let* ((hex (when (looking-at css--colors-regexp)
                       (and (eq (elt (match-string 0) 0) ?#)
                            (match-string 0))))
                (rgb (css--hex-color hex)))
      (seq-let (r g b)
          (mapcar (lambda (x) (round (* x 255)))
                  (color-name-to-rgb (css--color-to-4-dpc rgb)))
        (replace-match
         (if-let* ((alpha (css--hex-alpha hex))
                   (a (css--format-rgba-alpha
                       (/ (string-to-number alpha 16)
                          (float (- (expt 16 (length alpha)) 1))))))
             (format "rgba(%d, %d, %d, %s)" r g b a)
           (format "rgb(%d, %d, %d)" r g b))
         t))
      t)))