ChromoSelector - jQuery Color Picker plugin - Documentation / Demos / Custom Color Formats

Home

Custom Color Formats

If you would like to use the HEX format with a leading hash, you don't need to do anything as it's the default (example 1).

If you would like to use a format that is known to chromoselector, you just need to implement the "color2str" function (examples 2-5). For a full list of supported format, please see the color manipulation page

If you would like to use a custom format, you will need to implement both the "color2str" and "str2color" functions (examples 6-8).

1: HEX (default)

Code

$(document).ready(function () {
    $('#color0').chromoselector();
});
jsFiddle

2: Comma separated RGB

Code

$(document).ready(function () {
    $('#color1').chromoselector({
        color2str: function (color) {
            return color.getRgbString();
        }
     });
});
jsFiddle

3: Comma separated RGBA

Code

$(document).ready(function () {
    $('#color2').chromoselector({
        panelAlpha: true,
        color2str: function (color) {
            return color.getRgbaString();
        }
     });
});
jsFiddle

4: Comma separated HSL

Code

$(document).ready(function () {
    $('#color3').chromoselector({
        color2str: function (color) {
            return color.getHslString();
        }
    });
});
jsFiddle

5: Comma separated HSLA

Code

$(document).ready(function () {
    $('#color4').chromoselector({
        panelAlpha: true,
        color2str: function (color) {
            return color.getHslaString();
        }
    });
});
jsFiddle

6: Hex RGB without "#"

Code

$(document).ready(function () {
    $('#color5').chromoselector({
        str2color: function (str) {
            return '#' + str;
        },
        color2str: function (color) {
            return color.getHexString().substring(1);
        }
    });
});
jsFiddle

7: JSON encoded CMYK

Code

$(document).ready(function () {
    $('#color6').chromoselector({
        str2color: function (str) {
            try {
                return $.parseJSON(str);
            } catch (e) {}
        },
        color2str: function (color) {
            var cmyk = color.getCmyk();
            for (var i in cmyk) {
                cmyk[i] = Math.round(cmyk[i] * 100) / 100;
            }
            return JSON.stringify(cmyk);
        }
    });
});
jsFiddle

8: RGB as an integer

Code

$(document).ready(function () {
    $('#color7').chromoselector({
        str2color: function (str) {
            var num = parseInt(str, 10) || 0;
            return {
                r: ((num & (255*255*255)) >> 16) / 255,
                g: ((num & (255*255)) >> 8) / 255,
                b: ((num & (255))) / 255
            };
        },
        color2str: function (color) {
            var rgb = color.getRgb();
            var num = (Math.round(rgb.r * 255) << 16)
                + (Math.round(rgb.g * 255) << 8)
                + Math.round(rgb.b * 255)
            return num;
        }
    });
});
jsFiddle