ELisp: Convert Color Formats: RGB HSL HSV

By Xah Lee. Date: . Last updated: .

Package color.el

The color package color.el, in Emacs 24 (Released 2012-06) lets you convert colors from various models and formats. For example: RGB, HSL, HSV, named color values, and things like finding the color complement.

This page shows you:

Convert RGB to HSL

;; convert RGB color to HSL.
;; all input and output are in the range {0, 1}

(require 'color )

(color-rgb-to-hsl 1 0 0) ; ⇒  (0.0 1 0.5) red

(color-rgb-to-hsl 0 1 0) ; ⇒ (0.3333333333333333 1 0.5) green

(color-rgb-to-hsl 0 0 1) ; ⇒ (0.6666666666666666 1 0.5) blue

(color-rgb-to-hsl 1 1 1) ; ⇒ (0.0 0.0 1.0) white

(color-rgb-to-hsl 0 0 0) ; ⇒ (0.0 0.0 0.0) black

Note that if you are converting from hexadecimal valued RGB format such as #aabbcc, you must first convert the hexadecimal to integer, then re-scale from {0 to 255} based range to {0 to 1} based range, then feed that to color-rgb-to-hsl.

For explanation of the HSL color model, see: CSS: HSL Color

Convert HSL to RGB

;; convert HSL color to RGB.
;; all input and output are in the range {0 to 1}

(require 'color )

(color-hsl-to-rgb 0 1 .5) ; (1.0 0.0 0.0) red

(color-hsl-to-rgb .3 1 .5) ; ⇒ (0.19999999999999996 1.0 0.0) greenish

(color-hsl-to-rgb .6 1 .5) ; ⇒ (0.0 0.3999999999999999 1.0) blueish

(color-hsl-to-rgb 0 0 1) ; ⇒ (1 1 1) white

(color-hsl-to-rgb 0 0 0) ; ⇒ (0 0 0) black

Thanks

color.el is written by Julien Danjou [https://julien.danjou.info/ ] , Drew Adams [http://www.emacswiki.org/emacs/DrewAdams ] Thanks guys.

Emacs, CSS Color Topics