W3C home > Mailing lists > Public > www-style@w3.org > September 2010

Re: Propsal: Mechanism for converting rgb, hex, and named color strings between one another

From: Tab Atkins Jr. <jackalmage@gmail.com>
Date: Sat, 25 Sep 2010 11:43:01 -0700
Message-ID: <AANLkTimn1u0LnQYT2BC8bGBeOUAhkckfTAj+0bGwZDdo@mail.gmail.com>
To: Daniel Buchner <daniel@mozilla.com>
Cc: www-style@w3.org
On Fri, Sep 24, 2010 at 4:22 PM, Daniel Buchner <daniel@mozilla.com> wrote:
> Many developers, js libraries, and frameworks interact with colors in a
> variety ways in their pages and apps. One common action they perform in many
> of these interactions is to convert colors between RGB and HEX. A fair
> amount of js is needed to do this type of thing, see the following examples:
> converting hex to rgb in js
>
> I propose that we provide a way for developers to get all color equivalents
> of a valid color string - red, #ff0, rgb(255, 0, 0).
>
> Currently to derive RGB or HEX values from a named css color there is even
> more craziness required. You must create a dummy element, inject it into the
> DOM, set styles, get the computed styles, and finally perform various
> validity checks to ensure the correct output: Here is an implementation I
> put together to convert named colors to RGB or HEX: named colors to hex and
> rgb - try typing in 'red', 'magenta', etc.
>
> The very fact that the colors can be converted via the implementation I use,
> indicates the look-up is already present in browsers, there is just nothing
> exposed to developers to take advantage of it.

As Anne said in the original message that was sent to WHATWG, this
should be a part of the CSSOM Values API that is  being discussed now.
 You'll be able to do something like "el.style.values.color.red" or
"el.style.values.color.hue" to get the components of the color.

Going from red/green/blue values (either in 0-255 or 0-1 range) to
showing it as an rgb() or #rgb string is obviously trivial.  Same with
hue/saturation/lightness to hsl().

~TJ
Received on Saturday, 25 September 2010 19:13:19 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 17:20:31 GMT