branch: externals/colorful-mode
commit ba161e9e04fd044cc56a530f66de0db843de55b6
Author: Elias G. B. Perez <eg642...@gmail.com>
Commit: Elias G. B. Perez <eg642...@gmail.com>

    Add support for LaTex rgb, html and gray
    
    This only add support for being highlighted,
    and there are no plans for support convertions
    to other formats.
    * colorful-mode.el (colorful-extra-color-keyword-functions):
    Add latex-mode and update docstring.
    (colorful--rgb-to-hex, colorful--hsl-to-hex):
    Performance Improvement.
    (colorful-colorize-itself): Rework.
    (colorful-latex-keywords, colorful-add-latex-colors):
    New variable and function.
---
 README.org       | 35 +++++++++++---------
 colorful-mode.el | 99 +++++++++++++++++++++++++++++++++++++-------------------
 2 files changed, 85 insertions(+), 49 deletions(-)

diff --git a/README.org b/README.org
index 71e1ecdbf7..338f952cc0 100644
--- a/README.org
+++ b/README.org
@@ -19,12 +19,12 @@ in real time and in a user friendly way based on 
🌈[[https://elpa.gnu.org/pack
 *WARNING: THIS PACKAGE STILL IN A EXPERIMENTAL STATE, ABRUPT CHANGES SUCH AS 
DELETE USER FUNCTIONS, OPTIONS AND KEYBINDINGS OR UNEXPECTED BREAKS ARE USUALLY 
COMMON*
 
 * Features ✨
-- Preview emacs colors names, hexadecimal colors and CSS rgb functions
+- Preview colors such as colors names, hexadecimal colors and more
   in your current buffer in real time.
-- Replace or copy to other color formats such as hexadecimal or color names.
+- Replace or copy to other color formats such as hexadecimal or color names 
(only for some colors).
 - Preview using highlight or a prefix/suffix string.
-- Preview only colors in strings.
-- Exclude colors such as hex values and color names.
+- Alow preview colors only in strings.
+- Exclude colors from being highlighted such as hex values and color names.
 
 * Screenshots and animated GIFs 📷
 [[./assets/gif1.gif]]
@@ -57,6 +57,7 @@ in real time and in a user friendly way based on 
🌈[[https://elpa.gnu.org/pack
   '((emacs-lisp-mode . colorful-add-color-names)
     ((mhtml-mode html-ts-mode css-mode css-ts-mode)
     . (colorful-add-rgb-colors colorful-add-hsl-colors 
colorful-add-color-names))
+    (latex-mode . colorful-add-latex-colors)
     colorful-add-hex-colors)
   List of functions to add extra color keywords to colorful-color-keywords.
 
@@ -79,6 +80,7 @@ in real time and in a user friendly way based on 
🌈[[https://elpa.gnu.org/pack
    + colorful-add-color-names.
    + colorful-add-rgb-colors.
    + colorful-add-hsl-colors.
+   + colorful-add-latex-colors
 
 - =colorful-exclude-colors (default: '("#def"))= List of keyword to don't 
highlight.
 - =colorful-short-hex-convertions (default: 2)= If set to 2, hex values 
converted by colorful should be as short as possible.
@@ -115,13 +117,13 @@ colors:
 - =colorful-add-color-names= Add color names.
 - =colorful-add-rgb-colors= Add CSS RGB colors.
 - =colorful-add-hsl-colors= Add CSS HSL colors.
+- =colorful-add-latex-colors= Add LaTex rgb/RGB/HTML/Grey colors.
 
 For use them add it to:
 #+begin_src emacs-lisp
-;; In this example add emacs color names only for yaml-,mode and derived.
+;; In this example add emacs color names only for yaml-mode and derived.
   (add-to-list 'colorful-extra-color-keyword-functions '(yaml-mode . 
colorful-add-color-names))
 #+end_src
-NOTE: colorful-keyword is buffer-local variable.
 
 See: =colorful-extra-color-keyword-functions= for more details.
 
@@ -134,7 +136,7 @@ For install colorful run:
 Or if you prefer using =use-package= macro:
 #+begin_src emacs-lisp
   (use-package colorful-mode
-    :ensure t ; <-- Optional
+    :ensure t ; Optional
     :hook (prog-mode text-mode)
     ...)
 
@@ -178,15 +180,16 @@ expert in elisp)/.
 
 Due I have plans for submit it to *GNU ELPA* keep in mind if you want
 to contribute with a PR *you will need assign copyright to FSF*, this
-for make FSF can still maintaining or save the package if this repo
-gets deleted or something else happen.  If you don't want assign
-copyright please open an issue and send instructions for your
-feature/code, so i can implement it without requiring copyright
-paperwork.  Also if your contribution is less than 15 lines [see:
-[[https://www.gnu.org/software/emacs/manual/html_node/emacs/Copyright-Assignment.html][Copyright
 Assignment]]] you can open a PR and your contribution will be
-merged quickly.  However for people that are willing to doing
-paperwork you must wait until this package is ready for submit to
-ELPA, this is for ensure that you are ready for doing paperwork.
+will ensure FSF can still maintaining or save the package if this repo
+gets deleted or something else happens.  If you don't want assign
+copyright then open an issue and send instructions for your
+feature/code, so i can implement it (not in the same way) without
+requiring copyright paperwork.  Also if your contribution is less than
+15 lines [see: 
[[https://www.gnu.org/software/emacs/manual/html_node/emacs/Copyright-Assignment.html][Copyright
 Assignment]]] you can open a PR and your
+contribution will be reviewed and maybe merged quickly.  However for
+people that are willing to doing paperwork you must wait until this
+package is ready for submit to ELPA, this is for ensure that you are
+ready for doing paperwork.
 
 Also i will try to make this package as stable as possible before
 submit it to ELPA, later that *I won't be able to be the mantainer* of
diff --git a/colorful-mode.el b/colorful-mode.el
index 1487a119a1..474ac1037a 100644
--- a/colorful-mode.el
+++ b/colorful-mode.el
@@ -8,7 +8,7 @@
 ;; Package-Requires: ((emacs "28.1") (compat "29.1.4.5"))
 ;; Homepage: https://github.com/DevelopmentCool2449/colorful-mode
 ;; Keywords: faces, tools, matching
-;; Version: 0.3.0
+;; Version: 0.5.0
 
 ;; This file is not part of GNU Emacs.
 
@@ -40,7 +40,6 @@
 
 (require 'color)
 (eval-when-compile (require 'subr-x))
-;; (require 'rx) ; Not sure if this should be added.
 
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -70,6 +69,7 @@
   '((emacs-lisp-mode . colorful-add-color-names)
     ((mhtml-mode html-ts-mode css-mode css-ts-mode)
      . (colorful-add-rgb-colors colorful-add-hsl-colors 
colorful-add-color-names))
+    (latex-mode . colorful-add-latex-colors)
     colorful-add-hex-colors)
   "List of functions to add extra color keywords to `colorful-color-keywords'.
 It can be a cons cell specifing the mode (or a list of modes)
@@ -88,7 +88,8 @@ Available functions are:
  - `colorful-add-hex-colors'
  - `colorful-add-color-names'.
  - `colorful-add-rgb-colors'.
- - `colorful-add-hsl-colors'"
+ - `colorful-add-hsl-colors'
+ - `colorful-add-latex-colors'"
   :type '(repeat
           (choice
            (cons (choice :tag "Mode(s)"
@@ -99,13 +100,6 @@ Available functions are:
                          function))
            function)))
 
-(defvar colorful-extra-color-keywords-hook nil
-  "Hook used for add extra color keywords to `colorful-color-keywords'.
-Available functions are: `colorful-add-color-names'.")
-
-(make-obsolete-variable 'colorful-extra-color-keywords-hook
-                        'colorful-extra-color-keyword-functions "0.1.3")
-
 (defcustom colorful-allow-mouse-clicks t
   "If non-nil, allow using mouse buttons for change color."
   :type 'boolean)
@@ -162,7 +156,7 @@ mode is derived from `prog-mode'."
 ;; FIXME: THIS MACRO WORKS FINE, HOWEVER IT DOESN'T WORK WITH
 ;;       MOUSE CLICKS, IF ANYONE KNOWs WHY, PLEASE OPEN AN ISSUE.
 ;;       MAYBE THIS CAN BE DELETED.
-;;;; (defmacro colorful--check-ov (varlist &rest then)
+;; (defmacro colorful--check-ov (varlist &rest then)
 ;;   "Check if there is a colorful-ov at current position, execute THEN.
 ;; Otherwise throw a user error message.
 ;; Works as a let* macro using VARLIST for lexical values but only for
@@ -176,22 +170,11 @@ mode is derived from `prog-mode'."
 ;;                               (if (overlay-get ov 'colorful--overlay)
 ;;                                   (throw 'val ov))))))
 ;;           varlist))
-
 ;;        ,(macroexp-progn then)
 ;;      (user-error "No color found")))
 
 ;;;;;;;;;; Base Convertion functions ;;;;;;;;;;
 
-;; TEMP: Since CSS derived modes allow converting hexs to rgb/hsl
-;;       this may be deleted when colorful 1.0.0 is released.
-;; (defun colorful--hex-to-rgb (hex)
-;;   "Return HEX color as CSS rgb format."
-;;   (let* ((rgb (color-name-to-rgb hex))
-;;        (r (* 255 (nth 0 rgb)))
-;;        (g (* 255 (nth 1 rgb)))
-;;        (b (* 255 (nth 2 rgb))))
-;;   (format "rgb(%d, %d, %d)" r g b)))
-
 (defun colorful--percentage-to-absolute (percentage)
   "Convert PERCENTAGE to a absolute number.
 If PERCENTAGE is absolute, return PERCENTAGE.
@@ -202,13 +185,30 @@ If PERCENTAGE is above 100%, it's converted to 100."
         (/ (* (min (string-to-number percentage) 100) 255) 100)
       (string-to-number percentage))))
 
+(defun colorful--latex-rgb-to-hex (rgb)
+  "Return LaTex RGB as hexadecimal format.
+RGB must be a string."
+  (let* ((rgb (string-split (string-remove-prefix "{rgb}{" rgb) ","))
+         (r (string-to-number (nth 0 rgb)))
+         (g (string-to-number (nth 1 rgb)))
+         (b (string-to-number (nth 2 rgb))))
+    (color-rgb-to-hex r g b)))
+
+(defun colorful--latex-gray-to-hex (gray)
+  "Return LaTex GRAY as hexadecimal format.
+GRAY must be a string."
+  (let ((gray (string-to-number (string-remove-prefix "{gray}{" gray))))
+    (apply #'color-rgb-to-hex (color-hsl-to-rgb 0 1 gray))))
+
+
 (defun colorful--rgb-to-hex (rgb &optional digit)
   "Return CSS RGB as hexadecimal format.
 DIGIT specifies which how much digits per component must have return value.
 RGB must be a string."
   (let* ((rgb (string-split
-               (string-remove-prefix "rgba("
-                                     (string-remove-prefix "rgb(" rgb))
+               (if (string-prefix-p "rgb(" rgb)
+                   (string-remove-prefix "rgb(" rgb)
+                 (string-remove-prefix "rgb(" rgb))
                ","))
          (r (/ (colorful--percentage-to-absolute (nth 0 rgb)) 255.0))
          (g (/ (colorful--percentage-to-absolute (nth 1 rgb)) 255.0))
@@ -220,8 +220,9 @@ RGB must be a string."
 DIGIT specifies which how much digits per component must have return value.
 HSL must be a string."
   (let* ((hsl (string-split
-               (string-remove-prefix "hsla("
-                                     (string-remove-prefix "hsl(" hsl))
+               (if (string-prefix-p "hsl(" hsl)
+                   (string-remove-prefix "hsl(" hsl)
+                 (string-remove-prefix "hsla(" hsl))
                ","))
          (h (/ (string-to-number (nth 0 hsl)) 360.0))
          (s (/ (string-to-number (nth 1 hsl)) 100.0))
@@ -416,8 +417,12 @@ it can be white or black."
                      (:background ,color)
                      (:inherit colorful-base)))))))
 
+;; NOTE: I'm not sure if this function must be splitted into multiple
+;;       ones according to their color.
 (defun colorful-colorize-itself (&optional match)
-  "Helper function for Colorize MATCH with itself."
+  "Helper function for Colorize MATCH with itself.
+If MATCH is not any hex color or Emacs color name, it will be
+converted to a Hex color."
   (when-let* ((match (or match 0))
               (string (match-string-no-properties match))
               ((and (not (member string colorful-exclude-colors))
@@ -432,7 +437,17 @@ it can be white or black."
      ((string-match-p (rx (one-or-more "rgb" (opt "a") "(")) string)
       (setq string (colorful--rgb-to-hex string)))
      ((string-match-p (rx (one-or-more "hsl" (opt "a") "(")) string)
-      (setq string (colorful--hsl-to-hex string))))
+      (setq string (colorful--hsl-to-hex string)))
+     ((string-prefix-p "{rgb}{" string)
+      (setq string (colorful--latex-rgb-to-hex string)))
+     ((string-prefix-p "{RGB}{" string)
+      (setq string (colorful--rgb-to-hex
+                    (string-remove-prefix "{RGB}{" string))))
+     ((string-prefix-p "{HTML}{" string)
+      (setq string (concat "#" (string-remove-suffix
+                                "}" (string-remove-prefix "{HTML}{" string)))))
+     ((string-prefix-p "{gray}{" string)
+      (setq string (colorful--latex-gray-to-hex string))))
 
     ;; Delete duplicates overlays found
     (dolist (ov (overlays-in beg end))
@@ -491,7 +506,8 @@ it can be white or black."
                (opt (any "Ee")
                     (opt (any "+-"))
                     (one-or-more (any "0-9")))))
-     (0 (colorful-colorize-itself)))))
+     (0 (colorful-colorize-itself))))
+  "Font-lock keywords to add Hexadecimal color.")
 
 (defun colorful-add-hex-colors ()
   "Function for add hex colors to `colorful-color-keywords'.
@@ -522,7 +538,7 @@ This is intended to be used with 
`colorful-extra-color-keyword-functions'."
                      "White" "Yellow"))
                   'words)
      (0 (colorful-colorize-itself))))
-  "Font-lock keywords to add for `colorful-color-keywords'.")
+  "Font-lock keywords to add color names.")
 
 (defun colorful-add-color-names ()
   "Function for add Color names to `colorful-color-keywords'.
@@ -577,11 +593,29 @@ This is intended to be used with 
`colorful-extra-color-keyword-functions'."
   "Font-lock keywords for add HSL colors.")
 
 (defun colorful-add-hsl-colors ()
-  "Function for add CSS HSL colors to `colorful-color-keywords'.
+  "Function for add CSS HSL colors.
 This is intended to be used with `colorful-extra-color-keyword-functions'."
   (dolist (colors colorful-hsl-font-lock-keywords)
     (add-to-list 'colorful-color-keywords colors t)))
 
+(defvar colorful-latex-keywords
+  `((,(rx (seq "{" (or "rgb" "RGB") "}{"
+               (group (one-or-more (any "0-9" "."))) "," (zero-or-more " ")
+               (group (one-or-more (any "0-9" "."))) "," (zero-or-more " ")
+               (group (one-or-more (any "0-9" "."))) "}"))
+     (0 (colorful-colorize-itself)))
+    (,(rx (seq "{HTML}{" (group (= 6 (any "0-9A-Fa-f"))) "}"))
+     (0 (colorful-colorize-itself)))
+    (,(rx (seq "{gray}{" (group (one-or-more (any "0-9" "."))) "}"))
+     (0 (colorful-colorize-itself))))
+  "Font-lock keywords for add LaTex rgb/RGB/HTML/Grey colors.")
+
+(defun colorful-add-latex-colors ()
+  "Function for add LaTex rgb/RGB/HTML/Grey colors.
+This is intended to be used with `colorful-extra-color-keyword-functions'."
+  (dolist (colors colorful-latex-keywords)
+    (add-to-list 'colorful-color-keywords colors t)))
+
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;                           Minor mode defintinions                          ;
@@ -616,8 +650,7 @@ This is intended to be used with 
`colorful-extra-color-keyword-functions'."
 
 ;;;###autoload
 (define-minor-mode colorful-mode
-  "Preview color hexs in current buffer.
-This will fontify colors strings like \"#aabbcc\" or \"blue\"."
+  "Preview any color in your buffer such as hex, color names, CSS rgb in real 
time."
   :lighter nil :keymap colorful-mode-map
   (if colorful-mode
       (colorful--turn-on)

Reply via email to