diff options
| author | Simen Heggestøyl | 2018-01-28 15:35:46 +0100 |
|---|---|---|
| committer | Simen Heggestøyl | 2018-01-28 15:38:27 +0100 |
| commit | 69a30e8b87fac5888daa26c63663351570e3d533 (patch) | |
| tree | f7c4b0a665556c86dcaa4e744f1693bda393375c /lisp/textmodes | |
| parent | 97defdfc36d9a83a3081c5f76e249f908645f7ec (diff) | |
| download | emacs-69a30e8b87fac5888daa26c63663351570e3d533.tar.gz emacs-69a30e8b87fac5888daa26c63663351570e3d533.zip | |
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." | |||
| 1413 | (apply-partially #'make-list (if six-digits 2 4)) | 1413 | (apply-partially #'make-list (if six-digits 2 4)) |
| 1414 | (seq-partition (seq-drop hex 1) (if six-digits 2 1))))))) | 1414 | (seq-partition (seq-drop hex 1) (if six-digits 2 1))))))) |
| 1415 | 1415 | ||
| 1416 | (defun css--format-hex (hex) | ||
| 1417 | "Format a CSS hex color by shortening it if possible." | ||
| 1418 | (let ((parts (seq-partition (seq-drop hex 1) 2))) | ||
| 1419 | (if (and (>= (length hex) 6) | ||
| 1420 | (seq-every-p (lambda (p) (eq (elt p 0) (elt p 1))) parts)) | ||
| 1421 | (apply #'string | ||
| 1422 | (cons ?# (mapcar (lambda (p) (elt p 0)) parts))) | ||
| 1423 | hex))) | ||
| 1424 | |||
| 1416 | (defun css--named-color-to-hex () | 1425 | (defun css--named-color-to-hex () |
| 1417 | "Convert named CSS color at point to hex format. | 1426 | "Convert named CSS color at point to hex format. |
| 1418 | Return non-nil if a conversion was made. | 1427 | Return non-nil if a conversion was made. |
| @@ -1426,7 +1435,7 @@ should not be mixed with those in color.el." | |||
| 1426 | (when (member (word-at-point) (mapcar #'car css--color-map)) | 1435 | (when (member (word-at-point) (mapcar #'car css--color-map)) |
| 1427 | (looking-at css--colors-regexp) | 1436 | (looking-at css--colors-regexp) |
| 1428 | (let ((color (css--compute-color (point) (match-string 0)))) | 1437 | (let ((color (css--compute-color (point) (match-string 0)))) |
| 1429 | (replace-match color)) | 1438 | (replace-match (css--format-hex color))) |
| 1430 | t))) | 1439 | t))) |
| 1431 | 1440 | ||
| 1432 | (defun css--format-rgba-alpha (alpha) | 1441 | (defun css--format-rgba-alpha (alpha) |
| @@ -1490,7 +1499,9 @@ should not be mixed with those in color.el." | |||
| 1490 | (kill-sexp) | 1499 | (kill-sexp) |
| 1491 | (let ((named-color (seq-find (lambda (x) (equal (cdr x) color)) | 1500 | (let ((named-color (seq-find (lambda (x) (equal (cdr x) color)) |
| 1492 | css--color-map))) | 1501 | css--color-map))) |
| 1493 | (insert (if named-color (car named-color) color))) | 1502 | (insert (if named-color |
| 1503 | (car named-color) | ||
| 1504 | (css--format-hex color)))) | ||
| 1494 | t))))) | 1505 | t))))) |
| 1495 | 1506 | ||
| 1496 | (defun css-cycle-color-format () | 1507 | (defun css-cycle-color-format () |