]> git.eshelyaron.com Git - emacs.git/commitdiff
Parse percent values in CSS alpha components
authorSimen Heggestøyl <simenheg@gmail.com>
Sat, 13 Jan 2018 08:41:47 +0000 (09:41 +0100)
committerSimen Heggestøyl <simenheg@gmail.com>
Sun, 21 Jan 2018 13:20:56 +0000 (14:20 +0100)
* lisp/textmodes/css-mode.el (css--rgb-color): Support parsing percent
values in the alpha component.

* test/lisp/textmodes/css-mode-tests.el
(css-test-rgb-to-named-color-or-hex, css-test-rgb-parser): Update for
the above changes.

lisp/textmodes/css-mode.el
test/lisp/textmodes/css-mode-tests.el

index 1d13070f120381e772f04eb62913582665422cd5..7e997ac2c0f662652edff0e42e3b7133f63b190a 100644 (file)
@@ -954,11 +954,11 @@ the returned hex string."
       (let* ((is-percent (match-beginning 1))
             (str (match-string (if is-percent 1 2)))
             (number (string-to-number str)))
-       (when is-percent
-         (setq number (* 255 (/ number 100.0))))
-        (if (and include-alpha (= iter 3))
-            (push (round (* number 255)) result)
-          (push (min (max 0 (truncate number)) 255) result))
+       (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)
index 537a88ec52bd754db5acdb8e8b57ec5d4612fa7b..272d281217ef4b2bb7bd29cd1bd72c3a1da350cf 100644 (file)
                   ("rgb(255, 255, 255)" "white")
                   ("rgb(255, 255, 240)" "ivory")
                   ("rgb(18, 52, 86)" "#123456")
-                  ("rgba(18, 52, 86, 0.5)" "#12345680")))
+                  ("rgba(18, 52, 86, 0.5)" "#12345680")
+                  ("rgba(18, 52, 86, 50%)" "#12345680")
+                  ("rgba(50%, 50%, 50%, 50%)" "#80808080")))
     (with-temp-buffer
       (css-mode)
       (insert (nth 0 item))
 (ert-deftest css-test-rgb-parser ()
   (with-temp-buffer
     (css-mode)
-    (dolist (input '("255, 0, 127"
-                     "255, /* comment */ 0, 127"
-                     "255 0 127"
-                     "255, 0, 127, 0.75"
-                     "255 0 127 / 0.75"
+    (dolist (input '("255, 0, 128"
+                     "255, /* comment */ 0, 128"
+                     "255 0 128"
+                     "255, 0, 128, 0.75"
+                     "255 0 128 / 0.75"
                      "100%, 0%, 50%"
                      "100%, 0%, 50%, 0.115"
                      "100% 0% 50%"
       (erase-buffer)
       (save-excursion
         (insert input ")"))
-      (should (equal (css--rgb-color) "#ff007f")))))
+      (should (equal (css--rgb-color) "#ff0080")))))
 
 (ert-deftest css-test-hsl-parser ()
   (with-temp-buffer