Function: css--hsl-color

css--hsl-color is a byte-compiled function defined in css-mode.el.gz.

Signature

(css--hsl-color)

Documentation

Parse a CSS hsl() or hsla() 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.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/css-mode.el.gz
(cl-defun css--hsl-color ()
  "Parse a CSS hsl() or hsla() 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."
  (let ((result '()))
    ;; First parse the hue.
    (css--color-skip-blanks)
    (unless (looking-at css--angle-regexp)
      (cl-return-from css--hsl-color nil))
    (let ((hue (string-to-number (match-string 1)))
	  (unit (match-string 2)))
      (goto-char (match-end 0))
      ;; Note that here "turn" is just passed through.
      (cond
       ((or (not unit) (equal unit "deg"))
	;; Degrees.
	(setq hue (/ hue 360.0)))
       ((equal unit "grad")
	(setq hue (/ hue 400.0)))
       ((equal unit "rad")
	(setq hue (/ hue (* 2 float-pi)))))
      (push (mod hue 1.0) result))
    (dotimes (_ 2)
      (skip-chars-forward ",")
      (css--color-skip-blanks)
      (unless (looking-at css--percent-regexp)
        (cl-return-from css--hsl-color nil))
      (let ((number (string-to-number (match-string 1))))
        (setq number (/ number 100.0))
        (push (min (max number 0.0) 1.0) result)
        (goto-char (match-end 0))
        (css--color-skip-blanks)))
    (css--color-skip-blanks)
    ;; Accept a superset of the CSS syntax since I'm feeling lazy.
    (when (> (skip-chars-forward ",/") 0)
      (css--color-skip-blanks)
      (unless (looking-at css--number-or-percent-regexp)
        (cl-return-from css--hsl-color nil))
      (goto-char (match-end 0))
      (css--color-skip-blanks))
    (when (looking-at ")")
      (forward-char)
      (apply #'color-rgb-to-hex
	     (nconc (apply #'color-hsl-to-rgb (nreverse result)) '(2))))))