Emacs: Convert CSS Color, RGB to HSL 🚀
Command to Convert RGB Color to HSL
Here's a command that convert CSS RGB hexadecimal syntax to HSL.
put this in your Emacs Init File:
(defun xah-css-normalize-number-scale (Val RangeMax) "Scale Val from range [0, RangeMax] to [0, 1] The arguments can be int or float. Return value is float. URL `http://xahlee.info/emacs/emacs/emacs_css_color_rgb_to_hsl.html' Version: 2016-07-19 2024-03-24" (/ (float Val) (float RangeMax))) (defun xah-css-convert-color-hex-to-vec (Rrggbb) "Convert color Rrggbb from 6-digit RGB string to a elisp vector [r g b], where the values are from 0 to 1. e.g. 00ffcc becomes [0.0 1.0 0.8] Note: arguments is a string and must NOT start with #. URL `http://xahlee.info/emacs/emacs/emacs_css_color_rgb_to_hsl.html' Version: 2016-07-19 2023-05-13 2024-03-24" (vector (xah-css-normalize-number-scale (string-to-number (substring Rrggbb 0 2) 16) 255) (xah-css-normalize-number-scale (string-to-number (substring Rrggbb 2 4) 16) 255) (xah-css-normalize-number-scale (string-to-number (substring Rrggbb 4) 16) 255))) (defun xah-css-hex-to-hsl-color (HexStr) "Convert HexStr color to CSS HSL format. Return a string. e.g. ffefd5 becomes hsl(37,100%,91%) Note: The input string must NOT start with #. URL `http://xahlee.info/emacs/emacs/emacs_css_color_rgb_to_hsl.html' Version: 2016-07-19 2023-05-13 2024-03-24" (let* ( (xcolorVec (xah-css-convert-color-hex-to-vec HexStr)) (xR (elt xcolorVec 0)) (xG (elt xcolorVec 1)) (xB (elt xcolorVec 2)) (xhsl (color-rgb-to-hsl xR xG xB)) (xH (elt xhsl 0)) (xS (elt xhsl 1)) (xL (elt xhsl 2))) (format "hsl(%d,%d%%,%d%%)" (* xH 360) (* xS 100) (* xL 100)))) (defun xah-css-hex-color-to-hsl () "Convert color spec under cursor from CSS 6-digit RGB to CSS HSL format. e.g. #ffefd5 becomes hsl(37,100%,91%) URL `http://xahlee.info/emacs/emacs/emacs_css_color_rgb_to_hsl.html' Version: 2016-07-19 2024-03-24" (interactive) (let (xp1 xp2 xcurrentWord) (seq-setq (xp1 xp2) (xah-get-pos-of-glyphs)) (setq xcurrentWord (buffer-substring-no-properties xp1 xp2)) (if (string-match "[a-fA-F0-9]\\{6\\}" xcurrentWord) (progn (delete-region xp1 xp2) (when (eq (char-before) ?#) (delete-char -1)) (insert (xah-css-hex-to-hsl-color xcurrentWord))) (progn (user-error "The current word 「%s」 is not of the form #rrggbb." xcurrentWord)))))
requires package Emacs: xah-get-thing.el