Created
July 16, 2016 05:54
-
-
Save leshy/45210f3466cb71c9b18d500e036765f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(load-file "~/emacs/el/lesh/colorpicker/colorpicker.el") | |
(defun xah-syntax-color-hex () | |
"Syntax color hex color spec such as 「 #FF1100 」 in current buffer." | |
(interactive) | |
(font-lock-add-keywords | |
nil | |
'(("#[abcdefABCDEF[:digit:]]\\{6\\}" | |
(0 (put-text-property | |
(+ (match-beginning 0) 1) | |
(match-end 0) | |
'face (list :background (match-string-no-properties 0))))))) | |
(font-lock-fontify-buffer) | |
) | |
(defun xah-syntax-color-hsl () | |
"Syntax color CSS's HSL color spec ➢ for example: 「hsl(0,90%,41%)」 in current buffer. | |
URL `http://ergoemacs.org/emacs/emacs_CSS_colors.html' | |
Version 2015-06-11" | |
(interactive) | |
(font-lock-add-keywords | |
nil | |
'(("hsl( *\\([0-9]\\{1,3\\}\\) *, *\\([0-9]\\{1,3\\}\\)% *, *\\([0-9]\\{1,3\\}\\)% *)" | |
(0 (put-text-property | |
(+ (match-beginning 0) 3) | |
(match-end 0) | |
'face | |
(list | |
:background | |
(concat | |
"#" | |
(mapconcat | |
'identity | |
(mapcar | |
(lambda (x) (format "%02x" (round (* x 255)))) | |
(color-hsl-to-rgb | |
(/ (string-to-number (match-string-no-properties 1)) 360.0) | |
(/ (string-to-number (match-string-no-properties 2)) 100.0) | |
(/ (string-to-number (match-string-no-properties 3)) 100.0))) | |
"" )) ; "#00aa00" | |
)))))) | |
(font-lock-fontify-buffer)) | |
(defun xah-syntax-color-rgba () | |
"Syntax color CSS's RGB color spec ➢ for example: 「rgb(0,90,41)」 in current buffer. | |
URL `http://ergoemacs.org/emacs/emacs_CSS_colors.html' | |
Version 2015-06-11" | |
(interactive) | |
(font-lock-add-keywords | |
nil | |
'(("rgba( *\\([0-9]\\{1,3\\}\\) *, *\\([0-9]\\{1,3\\}\\) *, *\\([0-9]\\{1,3\\}\\),.*\)" | |
(0 (put-text-property | |
(+ (match-beginning 0) 5) | |
(match-end 3) | |
'face | |
(rgb-to-face | |
(list | |
(string-to-number (match-string-no-properties 1)) | |
(string-to-number (match-string-no-properties 2)) | |
(string-to-number (match-string-no-properties 3)) | |
)) | |
))))) | |
(font-lock-fontify-buffer)) | |
(defun xah-syntax-color-rgb () | |
"Syntax color CSS's RGB color spec ➢ for example: 「rgb(0,90,41)」 in current buffer. | |
URL `http://ergoemacs.org/emacs/emacs_CSS_colors.html' | |
Version 2015-06-11" | |
(interactive) | |
(font-lock-add-keywords | |
nil | |
'(("rgb( *\\([0-9]\\{1,3\\}\\) *, *\\([0-9]\\{1,3\\}\\) *, *\\([0-9]\\{1,3\\}\\) *)" | |
(0 (put-text-property | |
(+ (match-beginning 0) 4) | |
(match-end 3) | |
'face | |
(rgb-to-face | |
(list | |
(string-to-number (match-string-no-properties 1)) | |
(string-to-number (match-string-no-properties 2)) | |
(string-to-number (match-string-no-properties 3)) | |
)) | |
))))) | |
(font-lock-fontify-buffer)) | |
(defun rgb-to-face (rgb) | |
(interactive) | |
(list | |
:background | |
(concat "#" (mapconcat 'identity (mapcar (lambda (x) (format "%02x" x)) rgb) "" )) | |
:box | |
"#3AACFF" | |
; :foreground | |
; (message (concat "#" (mapconcat 'identity (mapcar (lambda (x) (format "%02x" (- 255 x))) rgb) "" ))) | |
)) | |
(defun color-pick-replace (startpoint endpoint color) | |
(let ((newcolor (color-pick color))) | |
(if (> (length newcolor) 0) | |
(progn | |
(delete-region startpoint endpoint) | |
(goto-char startpoint) | |
(insert newcolor)))) | |
) | |
(defun color-pick (color) | |
(with-temp-buffer | |
(progn | |
(if (stringp color) (setq color (list color))) | |
(apply `process-file colorpicker--script (append (list nil '(t nil) nil) color)) | |
(goto-char (point-min)) | |
(buffer-substring-no-properties (point) (line-end-position))))) | |
(defun picky () | |
(interactive) | |
(let (startpoint) | |
(save-excursion | |
(skip-chars-backward "-a-z0-9,(#") | |
(setq startpoint (point)) | |
(if | |
(re-search-forward | |
"rgb( *\\([0-9]\\{1,3\\}\\) *, *\\([0-9]\\{1,3\\}\\) *, *\\([0-9]\\{1,3\\}\\) *)" | |
(+ (point) 20) t) | |
(progn | |
(color-pick-replace (+ startpoint 4) (- (point) 1) (list | |
(match-string-no-properties 1) | |
(match-string-no-properties 2) | |
(match-string-no-properties 3))))) | |
(if | |
(re-search-forward | |
"rgba( *\\([0-9]\\{1,3\\}\\) *, *\\([0-9]\\{1,3\\}\\) *, *\\([0-9]\\{1,3\\}\\),.*\)" | |
(+ (point) 20) t) | |
(progn | |
(color-pick-replace (+ startpoint 5) (match-end 3) (list | |
(match-string-no-properties 1) | |
(match-string-no-properties 2) | |
(match-string-no-properties 3))))) | |
(if | |
(re-search-forward | |
"#[abcdef[:digit:]]\\{6\\}" | |
(+ (point) 8) t) | |
(progn | |
(color-pick-replace startpoint (point) (match-string-no-properties 0)))) | |
))) | |
(define-minor-mode colorPick-mode | |
"Syntax color css colors and provide a color picker." | |
nil " colorPick" nil | |
(xah-syntax-color-rgba) | |
(xah-syntax-color-rgb) | |
(xah-syntax-color-hex) | |
(xah-syntax-color-hsl) | |
(local-set-key (kbd "C-p") 'picky) | |
) | |
(add-hook 'css-mode-hook 'colorPick-mode) | |
(add-hook 'less-mode-hook 'colorPick-mode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment