diff options
author | Simen Heggestøyl <simenheg@gmail.com> | 2018-01-28 15:35:46 +0100 |
---|---|---|
committer | Simen Heggestøyl <simenheg@gmail.com> | 2018-01-28 15:38:27 +0100 |
commit | 69a30e8b87fac5888daa26c63663351570e3d533 (patch) | |
tree | f7c4b0a665556c86dcaa4e744f1693bda393375c /lisp/textmodes | |
parent | 97defdfc36d9a83a3081c5f76e249f908645f7ec (diff) | |
download | emacs-69a30e8b87fac5888daa26c63663351570e3d533.tar.gz |
Shorten CSS hex colors when possible
* lisp/textmodes/css-mode.el (css--format-hex): New function for
shortening CSS hex colors when possible.
(css--named-color-to-hex, css--rgb-to-named-color-or-hex): Use it.
* test/lisp/textmodes/css-mode-tests.el (css-test-format-hex): New
tests for 'css--format-hex'.
(css-test-named-color-to-hex, css-test-cycle-color-format): Adjust for
the changes to 'css--named-color-to-hex' and
'css--rgb-to-named-color-or-hex'.
Diffstat (limited to 'lisp/textmodes')
-rw-r--r-- | lisp/textmodes/css-mode.el | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/lisp/textmodes/css-mode.el b/lisp/textmodes/css-mode.el index 135c0d5f928..55c21f8acb0 100644 --- a/lisp/textmodes/css-mode.el +++ b/lisp/textmodes/css-mode.el @@ -1413,6 +1413,15 @@ should not be mixed with those in color.el." (apply-partially #'make-list (if six-digits 2 4)) (seq-partition (seq-drop hex 1) (if six-digits 2 1))))))) +(defun css--format-hex (hex) + "Format a CSS hex color by shortening it if possible." + (let ((parts (seq-partition (seq-drop hex 1) 2))) + (if (and (>= (length hex) 6) + (seq-every-p (lambda (p) (eq (elt p 0) (elt p 1))) parts)) + (apply #'string + (cons ?# (mapcar (lambda (p) (elt p 0)) parts))) + hex))) + (defun css--named-color-to-hex () "Convert named CSS color at point to hex format. Return non-nil if a conversion was made. @@ -1426,7 +1435,7 @@ should not be mixed with those in color.el." (when (member (word-at-point) (mapcar #'car css--color-map)) (looking-at css--colors-regexp) (let ((color (css--compute-color (point) (match-string 0)))) - (replace-match color)) + (replace-match (css--format-hex color))) t))) (defun css--format-rgba-alpha (alpha) @@ -1490,7 +1499,9 @@ should not be mixed with those in color.el." (kill-sexp) (let ((named-color (seq-find (lambda (x) (equal (cdr x) color)) css--color-map))) - (insert (if named-color (car named-color) color))) + (insert (if named-color + (car named-color) + (css--format-hex color)))) t))))) (defun css-cycle-color-format () |