branch: externals/colorful-mode
commit bcc2ea2b077ff574098b3745e4b3908cf0597a7f
Author: Elías Gabriel Pérez <eg642...@gmail.com>
Commit: Elías Gabriel Pérez <eg642...@gmail.com>

    Fix Oklab and Oklch coloring.
    
    Make Oklab and Oklch support no percentage lightness number.
    
    * README.org (Customizable User options): Adapt warning to Github
    Flavored README.
    * colorful-mode.el (colorful--oklab-to-hex, colorful--oklch-to-hex)
    (colorful--colorize, colorful-oklab-oklch-font-lock-keywords): Rework.
    * test/css.css: Add new tests.
---
 README.org       | 12 ++++++++----
 colorful-mode.el | 47 ++++++++++++++++++++++++++++++-----------------
 test/css.css     |  5 ++++-
 3 files changed, 42 insertions(+), 22 deletions(-)

diff --git a/README.org b/README.org
index c37d6d6b42..ea8b1d687a 100644
--- a/README.org
+++ b/README.org
@@ -66,11 +66,15 @@ Support for color changing at region.
   for change color.
 - =colorful-use-prefix (default: nil)= If non-nil, use prefix for preview color
   instead highlight them.
-#+begin_src C
-⛔ WARNING: CSS-DERIVED MODES COLORIZE RGB AND HEX COLORS OUT THE BOX,
+
+#+begin_quote
+[!WARNING]
+
+CSS-DERIVED MODES COLORIZE RGB AND HEX COLORS OUT THE BOX,
 THIS MAY INTERFERE WITH COLORFUL PREFIX, YOU CAN DISABLE THIS SETTING
-`css-fontify-colors' TO nil
-#+end_src
+'css-fontify-colors' TO nil
+#+end_quote
+
 - =colorful-prefix-string (default: "●")= String to be used in highlights.
   Only relevant if `colorful-use-prefix' is non-nil.
 - =colorful-prefix-alignment (default: 'left)= The position to put prefix 
string.
diff --git a/colorful-mode.el b/colorful-mode.el
index 70f98bdf26..6501090bcd 100644
--- a/colorful-mode.el
+++ b/colorful-mode.el
@@ -371,18 +371,31 @@ The conversion is controlled by 
`colorful-short-hex-conversions'.  If
 
 (defun colorful--oklab-to-hex (l a b)
   "Convert OKLab color (L, A, B) to HEX format.
-L, A and B must be floats divided by 100."
-  (let ((rgb (mapcar #'color-clamp (color-oklab-to-srgb l a b))))
+L A and B must be strings."
+  (let* ((l (if (not (seq-contains-p l ?%))
+                (string-to-number l)
+              (/ (string-to-number l) 100.0)))
+         (a (string-to-number a))
+         (b (string-to-number b))
+         (rgb (mapcar #'color-clamp (color-oklab-to-srgb l a b))))
     (apply #'color-rgb-to-hex rgb)))
 
 (defun colorful--oklch-to-hex (l c h)
   "Convert OKLCH color (L, C, H) to HEX format.
-L, A and must be floats divided by 100.
-H must be a float not divided."
-  (let* ((h-rad (* h (/ float-pi 180.0)))
+L C and H must be strings."
+  (let* ((l (if (not (seq-contains-p l ?%))
+                (string-to-number l)
+              (/ (string-to-number l) 100.0)))
+         (c (string-to-number c))
+         (h (float (string-to-number h)))
+         ;; Convert to LAB
+         (h-rad (* h (/ float-pi 180.0)))
          (a (* c (cos h-rad)))
-         (b (* c (sin h-rad))))
-    (colorful--oklab-to-hex l a b)))
+         (b (* c (sin h-rad)))
+         ;; Convert to RGB
+         (rgb (mapcar #'color-clamp (color-oklab-to-srgb l a b))))
+    ;; Return HEX
+    (apply #'color-rgb-to-hex rgb)))
 
 (defun colorful--hex-to-name (hex)
   "Return HEX as color name."
@@ -675,14 +688,14 @@ BEG and END are color match positions."
          (setq color (colorful--hsl-to-hex match-1 match-2 match-3))) ; h s l
 
         ('css-oklab
-         (setq color (colorful--oklab-to-hex (/ (string-to-number match-1) 
100.0) ; l
-                                             (string-to-number match-2) ; a
-                                             (string-to-number match-3)))) ; b
+         (setq color (colorful--oklab-to-hex match-1 ; l
+                                             match-2 ; a
+                                             match-3))) ; b
 
         ('css-oklch
-         (setq color (colorful--oklch-to-hex (/ (string-to-number match-1) 
100.0) ; l
-                                             (string-to-number match-2) ; c
-                                             (float (string-to-number 
match-3))))) ; h
+         (setq color (colorful--oklch-to-hex match-1 ; l
+                                             match-2 ; c
+                                             match-3))) ; h
 
         ('css-color-variable
          (cond
@@ -845,8 +858,8 @@ This is intended to be used with 
`colorful-extra-color-keyword-functions'."
 (defvar colorful-oklab-oklch-font-lock-keywords
   `((,(rx (seq "oklab(" (zero-or-more " ")
                (group (repeat 1 3 digit)
-                      (opt "." (1+ (any digit)))
-                      "%")
+                      (opt "." (1+ digit))
+                      (opt "%"))
                (zero-or-more " ") (opt "," (zero-or-more " "))
                (group (opt "-")
                       digit
@@ -864,10 +877,10 @@ This is intended to be used with 
`colorful-extra-color-keyword-functions'."
                            (opt (or "%" (zero-or-more " ")))))
                ")"))
      css-oklab)
-    (,(rx (seq "oklch" "(" (zero-or-more " ")
+    (,(rx (seq "oklch(" (zero-or-more " ")
                (group (repeat 1 3 digit)
                       (opt "." (1+ digit))
-                      "%")
+                      (opt "%"))
                (zero-or-more " ") (opt "," (zero-or-more " "))
                (group digit
                       (opt "." (1+ digit)))
diff --git a/test/css.css b/test/css.css
index b371365351..f2c7f67074 100644
--- a/test/css.css
+++ b/test/css.css
@@ -33,12 +33,15 @@ oklab(89.32% -0.04 -0.11); /* #b5e1ff */
 oklch(64.56% 0.2146 0 / 20%); /* #ed4188 */
 oklab(64.56% 0.21 0 / 20%); /* #ed4188 */
 
+oklab(0.50 -0.06 -0.24); /* #0054EB */
+oklch(0.50 0.25 255.2); /* #0054EB */
+
 /*****************************************************************************/
 /*                          rgb and hsl (with alpha)                         */
 /*****************************************************************************/
 
 rgb(245, 224, 220)   hsl(360, 47%, 63%)
-rgb(232,100,0)       hsl(23deg, 55, 67)
+rgb(232,100,0)       hsl(23deg, 55, 67+)
 
 rgb(300,100,0) /* <-- These both shouldn't be highlighted. */
 hsl(361, 47%, 63%) /* <-- */

Reply via email to