Function: css--rgb-color
css--rgb-color is a byte-compiled function defined in css-mode.el.gz.
Signature
(css--rgb-color &optional INCLUDE-ALPHA)
Documentation
Parse a CSS rgb() or rgba() color.
Point should be just after the open paren. Returns a hex RGB color, or nil if the color could not be recognized. This recognizes CSS-color-4 extensions. When INCLUDE-ALPHA is non-nil, the alpha component is included in the returned hex string.
Source Code
;; Defined in /usr/src/emacs/lisp/textmodes/css-mode.el.gz
(cl-defun css--rgb-color (&optional include-alpha)
"Parse a CSS rgb() or rgba() color.
Point should be just after the open paren.
Returns a hex RGB color, or nil if the color could not be recognized.
This recognizes CSS-color-4 extensions.
When INCLUDE-ALPHA is non-nil, the alpha component is included in
the returned hex string."
(let ((result '())
(iter 0))
(while (< iter 4)
(css--color-skip-blanks)
(unless (looking-at css--number-or-percent-regexp)
(cl-return-from css--rgb-color nil))
(let* ((is-percent (match-beginning 1))
(str (match-string (if is-percent 1 2)))
(number (string-to-number str)))
(if is-percent
(setq number (* 255 (/ number 100.0)))
(when (and include-alpha (= iter 3))
(setq number (* number 255))))
(push (min (max 0 (round number)) 255) result)
(goto-char (match-end 0))
(css--color-skip-blanks)
(cl-incf iter)
;; Accept a superset of the CSS syntax since I'm feeling lazy.
(when (and (= (skip-chars-forward ",/") 0)
(= iter 3))
;; The alpha is optional.
(cl-incf iter))
(css--color-skip-blanks)))
(when (looking-at ")")
(forward-char)
(apply #'format
(if (and include-alpha (= (length result) 4))
"#%02x%02x%02x%02x"
"#%02x%02x%02x")
(nreverse result)))))