Skip to content

Instantly share code, notes, and snippets.

@emaphp
Created February 22, 2020 18:41
Show Gist options
  • Select an option

  • Save emaphp/ad639fd6f38a49cc02c1cfb3abb579cc to your computer and use it in GitHub Desktop.

Select an option

Save emaphp/ad639fd6f38a49cc02c1cfb3abb579cc to your computer and use it in GitHub Desktop.
Hex color to RGBA conversion
(defun dotspacemacs/user-config ()
"Configuration for user code:
This function is called at the very end of Spacemacs startup, after layer
configuration.
Put your configuration code here, except for variables that should be set
before packages are loaded."
(defun hex-to-list (hex)
(mapcar (lambda (z)
(number-to-string (string-to-number z 16)))
(mapcar (lambda (y)
(substring hex (car y) (car (last y))))
(mapcar (lambda (x)
(list (* 2 (1- x)) (+ (* 2 (1- x)) 2))) (number-sequence 1 3)))))
(defun rgba-from-input (color)
(interactive "S")
(message "%s" color)
)
(defun hex-color-to-rgba (start end)
"Converts a color expressed in hexadecimal into its RGBA representation"
(interactive "r")
(when (region-active-p)
(let* ((region (buffer-substring start end))
(color (if (char-equal ?# (string-to-char region)) (substring region 1) region))
(len (length color))
(rgb (hex-to-list (apply 'concat (mapcar (lambda (c) (concat c c)) (split-string color "")))))
(save-excursion
(save-restriction
(narrow-to-region start end)
(delete-region start end)
(insert (format "rgba(%s1)" (apply 'concat (mapcar (lambda (x) (concat x ", ")) rgb))))
))
)
)
))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment