branch: externals/colorful-mode
commit 4351a8728eb03ec8342378dd406088f58a7b72af
Author: Elias G. B. Perez <[email protected]>
Commit: Elias G. B. Perez <[email protected]>
Add missing support for CSS rgba
Emacs doesn't support alpha colors, so the only
way is only preview them as solid colors.
Maybe alpha color can be implemented in a hacky
way based on window background color and using
color.el library, but i don't know how this can be done.
If anyone has any idea in how this can be implemented
please open a issue.
* colorful-mode.el (colorful-rgb-font-lock-keywords):
Add regexp for rgba.
* README.org: Delete an unnecessary and confusing
comparation in colorful and rainbow-mode table.
---
README.org | 30 +++++++++++++-----------------
colorful-mode.el | 29 +++++++++++++++++++++--------
2 files changed, 34 insertions(+), 25 deletions(-)
diff --git a/README.org b/README.org
index ad93da48c4..cedc2fc02d 100644
--- a/README.org
+++ b/README.org
@@ -135,25 +135,21 @@ For emacs 30 users you can use =use-package= macro with
=:vc= keyword:
=colorful-mode= improves =rainbow-mode= in adding more features
and fixing some /(and old)/ bugs:
-| Comparation | colorful-mode.el |
rainbow-mode.el |
-|---------------------------------------------------------+------------------+-----------------|
-| Compatible with hl-line and other overlays? | ✓ |
❌ |
-| Convert color to other formats? | ✓ |
❌ |
-| Insert open color hex | Work in progress |
❌ |
-| Opcionally use string preffix/suffix instead highlight | ✓ |
❌ |
-| Use emacs built-in functions instead define custom ones | ✓^{1} |
✓^{2} |
-| Exclude keywords/colors to highlight | ✓ |
❌^{3} |
-| Highlight only in strings and docstrings | ✓ |
❌ |
-| Allow highlight some colors only in specific modes | ✓ |
❌ |
-| No performance issues?^{4} | ❌ |
✓ |
-
-1. However colorful defines some helper funtions for some cases and
- avoiding extra computation.
-2. Only for some cases.
-3. rainbow-mode (like colorful) uses regex for highlight some
+| Comparation | colorful-mode.el |
rainbow-mode.el |
+|--------------------------------------------------------+------------------+-----------------|
+| Compatible with hl-line and other overlays? | ✓ |
❌ |
+| Convert color to other formats? | ✓ |
❌ |
+| Insert open color hex | Work in progress |
❌ |
+| Opcionally use string preffix/suffix instead highlight | ✓ |
❌ |
+| Exclude keywords/colors to highlight | ✓ |
❌^{1} |
+| Highlight only in strings and docstrings | ✓ |
❌ |
+| Allow highlight some colors only in specific modes | ✓ |
❌ |
+| No performance issues?^{2} | ❌ | ✓
|
+
+1. rainbow-mode (like colorful) uses regex for highlight some
keywords, however it cannot exclude specifics colors keywords
(such as "#def" that overrides C "#define" keyword).
-4. I didn't a benchmark however due colorful-mode uses overlays
+2. I didn't a benchmark however due colorful-mode uses overlays
instead text properties (like rainbow-mode) it can be a bit slow.
The intention is to provide a featured alternative to
diff --git a/colorful-mode.el b/colorful-mode.el
index fd6d5a0c95..725afae3b3 100644
--- a/colorful-mode.el
+++ b/colorful-mode.el
@@ -236,8 +236,12 @@ If the percentage value is above 100, it's converted to
100."
(defun colorful--rgb-to-hex (rgb)
"Return CSS RGB as hexadecimal format.
RGB must be a string."
- (let* ((rgb (string-split (string-remove-prefix "rgb(" rgb) ","))
- (r (colorful--percentage-to-absolute (nth 0 rgb)))
+ (let* ((rgb (string-split
+ (if (string-prefix-p "rgba(" rgb)
+ (string-remove-prefix "rgba(" rgb)
+ (string-remove-prefix "rgb(" rgb))
+ ","))
+ (r (colorful--percentage-to-absolute (nth 0 rgb)))
(g (colorful--percentage-to-absolute (nth 1 rgb)))
(b (colorful--percentage-to-absolute (nth 2 rgb))))
(format "#%02X%02X%02X" r g b)))
@@ -339,7 +343,8 @@ PROMPT must be a string with 1 format control (generally a
string argument)."
((member color (defined-colors))
(list (colorful--name-to-hex color) beg end))
;; Is a CSS rgb?
- ((string-prefix-p "rgb(" color)
+ ((or (string-prefix-p "rgb(" color)
+ (string-prefix-p "rgba(" color))
(list (colorful--rgb-to-hex color) beg end)))
(colorful--change-color ov "%s is already a Hex color. Try again: "
@@ -432,8 +437,10 @@ it can be white or black."
(beg (match-beginning match))
(end (match-end match)))
(cond
- ((string-prefix-p "rgb(" string)
- (setq string (colorful--rgb-to-hex string))))
+ ((or (string-prefix-p "rgb(" string)
+ (string-prefix-p "rgba(" string))
+ (setq string (colorful--rgb-to-hex string)))
+ )
;; Delete duplicates overlays found
(dolist (ov (overlays-in beg end))
@@ -453,9 +460,9 @@ it can be white or black."
"Font-lock keywords to add for `colorful-color-keywords'.")
(defvar colorful-rgb-font-lock-keywords
- `((,(rx (seq "rgb(" (zero-or-more " ")
+ `((,(rx (seq "rgb" (opt "a") "(" (zero-or-more " ")
(group (repeat 1 3 (any "0-9"))
- (opt nonl (any "0-9"))
+ (opt "." (any "0-9"))
(opt (zero-or-more " ") "%"))
(zero-or-more " ") "," (zero-or-more " ")
(group (repeat 1 3 (any "0-9"))
@@ -465,10 +472,16 @@ it can be white or black."
(group (repeat 1 3 (any "0-9"))
(opt "." (any "0-9"))
(opt (zero-or-more " ") "%"))
+ (opt
+ (zero-or-more " ") "," (zero-or-more " ")
+ (zero-or-more (any "0-9")) (opt nonl)
+ (one-or-more (any "0-9"))
+ (zero-or-more " ")
+ (opt "%"))
(zero-or-more " ") ")"))
(0 (colorful-colorize-itself))))
-
"Font-lock keywords to add for RGB colors.")
+
(defun colorful-add-color-names ()
"Function for add Emacs color names to `colorful-color-keywords'.
This is intended to be used with `colorful-extra-color-keyword-functions'."