jQuery Color Picker Sliders with human color perception

Github project page Download

There is a bootstrap-optimized version of this plugin: Bootstrap Color Picker Sliders.

A jQuery plugin that overhauls the classic color pickers and provides users with a whole new way of selecting desired color codes. Softpedia.com

Do you know the difference between the usual HSL and the human perceived (CIE Lch) sliders?

See the difference

Use the HSL hue slider, and the human perceived lightness will change a lot between the different hue angles (blue seems to be darker than yellow). Use the CIE hue slider, and the perceived lightness remains much more constant.

Update color from code: hsl(0,100%,50%) rgba(0,255,0,0.5) blue #ffff00 transparent

Color picker in a popup

Hint: you can type in any CSS color (even named ones, like yellow).

Let's see some different color pickers in action

HSL Color Picker

The following demo uses the HSL sliders with preview.

        <span class="hsl-demo"></span>

        $(".hsl-demo").ColorPickerSliders({
            flat: true,
            previewformat: 'hsl',
            order: {
                hsl: 1,
                preview: 2
            }
        });

CIE Lch Color Picker with human perceived lightness

The following demo uses the CIE Lch sliders with human perceived lightness. It uses a different customswatches name and different default swatches, so the swatches are independent from the other color pickers on this page.

        <span class="cielch-demo"></span>

        $(".cielch-demo").ColorPickerSliders({
            flat: true,
            customswatches: "different-swatches-groupname",
            swatches: ["rgb(174, 156, 227)","rgb(29, 179, 229)","rgb(54, 185, 163)","rgb(144, 176, 105)","rgb(224, 148, 110)","rgb(233, 137, 168)"],
            order: {
                cie: 1,
                preview: 2
            }
        });

RGB Color Picker

The following demo uses the RGB and opacity sliders. The swatches are disabled for this color picker.

        <span class="rgb-demo"></span>

        $(".rgb-demo").ColorPickerSliders({
            flat: true,
            swatches: false,
            order: {
                rgb: 1,
                opacity: 2
            },
            labels: {
                rgbred: 'Red',
                rgbgreen: 'Green',
                rgbblue: 'Blue'
            }
        });

Popup on any element

The following example is linked to different elements and rendered in popups. Only the cutom swatches are disabled for this demo, so you can use the predefined swatches.

        <input id="color" type="text" class="demo-popup" value="#56B29A" data-color-format="hex">
        <button type="button" class="demo-popup">button</button>
        <span class="demo-popup">span</span>
        <a class="demo-popup">link</a>

        $('.demo-popup').ColorPickerSliders({
            previewontriggerelement: true,
            flat: false,
            color: '#cf966f',
            customswatches: false,
            swatches: ['red', 'green', 'blue'],
            order: {
                rgb: 1,
                preview: 2
            }
        });
span link

Example for using the onchange() setting

this span is updated on change

        <input type="text" class="onchange-example" data-color-format="hex" value="GreenYellow"> <span class="onchange-example" style="padding:3px;border:1px solid #ccc;">this span is updated on change</span>

        <script>
            $("input.onchange-example").ColorPickerSliders({
                order: {
                    hsl: 1
                },
                onchange: function(container, color) {
                    var span = $("span.onchange-example");

                    span.css("background-color", color.tiny.toRgbString());

                    if (color.cielch.l < 60) {
                        span.css("color", "white");
                    }
                    else {
                        span.css("color", "black");
                    }
                }
            });
        </script>

A full-blown example showing all the sliders in action

The following demo uses all the sliders, and connects to a bunch of inputs to show the actual color values in different formats. The color format can be set on the inputs data-color-format property.

The color can be updated via any of the connected inputs. The plugin uses the excellent Tiny Color plugin, so it handles all valid CSS color formats (CSS color names as well!).

        <input type="text" class="full-demo-input" data-color-format="hsl">
        <input type="text" class="full-demo-input" data-color-format="hex">
        <input type="text" class="full-demo full-demo-input" data-color-format="rgb">

        $(".full-demo").ColorPickerSliders({
            flat: true,
            invalidcolorsopacity: 0,
            connectedinput: '.full-demo-input'
        });

How perceived lightness work?

This color picker can work internally in the CIE Lab color space, represented with the CIE Lch color model. The benefit is the consistent lightness and chroma values between the different colors, so it can represent yellow and blue with the same human perceived lightness. This is not the case with the more known RGB or HSL color models, where yellow is much brighter than blue. Altough, this color picker supports them as well, so you can decide which way to go.

What is the drawback of using CIE Lch?

In CIE Lch model, there are colors that can not be converted to RGB values (for example there is no dark yellow color with high chroma value). When there is an Lch variation that can not be represented in the RGB color space, then the color picker searches for the highest possible chroma value for that particular color (for example the only valid chroma value for black or white colors is 0), and uses the lowered chroma value for conversion. This way the color's hue and the lightness will not distorted.

CIE Lch color picker with hidden invalid color ranges

The position markers on the sliders are always shows an exclamation mark when the actual Lch setting can not be converted to an RGB value, but there is more to it! You can make the invalid colors invisible or semi-transparent using the invalidcolorsopacity setting. This way it is easier to find different hues or lightness with the same chroma.
You can notice that if the chroma is set to too high, then the preview shows the actual color with the highest possible chroma value. See it in action:

        <span class="cielch-demo2"></span>

        $(".cielch-demo2").ColorPickerSliders({
            flat: true,
            invalidcolorsopacity: 0.2,
            order: {
                cie: 1,
                preview: 2
            }
        });

Settings

Option Description
color This is the initial color if there is no valid (any CSS formatted) color value on the connected input element. Can be in any format that Tiny Color supports.
previewformat This is the color format on the preview slider (if visible). Can be rgb, hsl or hex. Note that hex format can not represent the opacity value.
preventtouchkeyboardonshow true: Makes the input readonly to prevent touch keyboard to show up on focus, needs a second click to become editable.
swatches array: Array of CSS colors (can be CSS color names as well).
false: The swatches are disabled.
customswatches string: Name of the custom swatches group. Color pickers with the same customswatches name share the same swatch colors, so if you add a new color to one of the color pickers, then all the color pickers swatches will be updated on the page which has the same customswatches name.
false: The custom swatches are disabled, so only the predefined swatches can be used.
connectedinput selector or jQuery object: The connected input elements value will be automatically updated in sync with the color picker. The color format which the color is represented i the input can be set via the data-color-format property. It can be any of hex, hsl or rgb. If it is hex, then the opacity will be discarded.
flat false: The color picker is rendered in a popup, which is shown on focus of the connected element.
true: The color picker is rendered right after the connected element, and is always visible.
disableautopopup false: The popup will be visible on blu or on click.
true: No popup shown on blur or on click. Must trigger colorpickersliders.showPopup and colorpickersliders.hidePopup events to show/hide the popup.
updateinterval Update interval of the sliders while dragging (ms). The default is 30.
previewontriggerelement true: Preview of the color on the connected element's background.
false: No preview on the connected element.
previewcontrasttreshold 30: If previewontriggerelement is enabled, then if the lightness is below of this value, the element's text color will be white. Otherwise the text is black.
erroneousciecolormarkers true: Unconvertable CIE colors are marked with an exclamation mark on the CIE sliders level marker.
false: No exclamation mark on the markers.
invalidcolorsopacity 1: Opacity of the invalid (unconvertable) area of the CIE sliders.
finercierangeedges true: Sharper, more accurate edges of the valid CIE ranges on the sliders.
false: Smoother edges of the valid CIE ranges on the sliders.
order An object which defines which sliders appear and in which order.
Possible properties: opacity, hsl, rgb, cie, preview.
labels An object which defines the labels of the sliders.
Possible properties: hslhue, hslsaturation, hsllightness, rgbred, rgbgreen, rgbblue, cielightness, ciechroma, ciehue, opacity, preview.
onchange function(container, color): This function is triggered when the color is changed with the sliders or via a connected input. The color parameter holds the actual color object in the following formats: rgb object, hsl object, cie lch object, Tiny Color object.
titleswatchesadd string: Title of the add color to swatches button.
titleswatchesreset string: Title of the reset swatches button.

Events

Example usage: $("#colorpicker").trigger("colorpickersliders.updateColor", "red"));

The following events can be triggered on the element which the color picker is initialized on.

Event Description
colorpickersliders.destroy function(): Destroys the color picker.
colorpickersliders.reset function(): Reinitializes the color picker.
colorpickersliders.updateColor function(newcolor): Sets a new color for the color picker.
colorpickersliders.showPopup function(): Shows the popup if the color picker uses it (it's not flat).
colorpickersliders.hidePopup function(): Hides the popup if the color picker uses it (it's not flat).

Download

Download from github. Please report issues and suggestions to github's issue tracker or contact me on g+ or twitter!

Follow us to get notified about the upcoming tools!

Follow us on LinkedIn to get notified about the upcoming tools, and feel free to share your thoughts with us! We welcome any jQuery/Bootstrap plugin suggestions which can help you and the others.

Comments

Like this plugin, have a question or just want to say hello? Feel free to leave a comment below.