JavaScript Color Parsing – TinyColor

I threw together a color parsing library to help make my JavaScript colorpicker as small and accurate as possible. It is called TinyColor. One of the main goals is a small footprint – it clocks in at 8.98KB (3.41KB gzipped).

I have written in more detail about this on my TinyColor page. In it, I talk about the Color conversion, input recognition, color manipulation, scheme (combination) generation, and more.

2 Responses to “JavaScript Color Parsing – TinyColor”

  1. 0i0 (lior hakim) Says:

    very nice!

    check oute this patteren to scrap a few more bytes for places like line 185
    from

    switch(max) {
                case r: h = (g - b) / d + (g < b ? 6 : 0); break;
                case g: h = (b - r) / d + 2; break;
                case b: h = (r - g) / d + 4; break;
            }

    to

    h = max == r ? (g - b) / d + (g < b ? 6 : 0):
        max == g ? (b - r) / d + 2:
        max == b ? (r - g) / d + 4:
        h;

    better looking and less code

  2. Brian Says:

    Yes, many of the conversion functions were modified from other sources, so I didn’t make many changes besides changing the bounding of the parameters (normalizing the input to a 0-1 scale).

    I usually avoid nested ternary expressions because it is hard to visually parse them out (at least when reading the code months later) so I end up explicitly parenthesizing the nesting, which makes it a little more verbose.

Leave a Reply

Posting Code: Use html such as <pre lang='javascript'></pre>. See all supported languages.

*