<input type='color' value='#f594d0' />

Why A Colorpicker?

I wasn't satisfied with the solutions available for colorpicking. Many of them included a ton of images, were hard to skin or customize, or were very large plugins. Here are the goals I had when making a new one:

Small Footprint

see a working jsFiddle example

Just include the needed CSS and JavaScript files, and you are ready to go!

<script src='spectrum.js'></script>
<link rel='stylesheet' href='spectrum.css' />

We don't need no stinkin' images!

Nobody wants to add a bunch of code into their project. Spectrum is contained in two files, and both are careful not to mess with your existing code. The JavaScript is well less than 10K gzipped, see the current size in the README.

Polyfill

I wanted an option for the most basic use case, a polyfill for the input[type=color] HTML5 control. This mode needs to work without JavaScript enabled - and fallback to an input[type=text] like other HTML5 inputs.

Customizable

Just because you don't have to change anything to get it to work, doesn't mean you can't! It is easy to skin and customize the plugin with CSS, and there are a wide range of modes and options to explore.

Modes

input[type=color]

If you just want to provide a polyfill for the native color input, the easiest way is to create an input with the type of color. Once a user's browser supports a native color control, it will opt to use their native control instead.

Unlike the other modes, your value must be a 6 character hex value starting with a '#'. Why? Because the spec says so, that's why.

<input type='color' name='color' />
<input type='color' name='color2' value='#3355cc' />

That's it! The field will degrade to a text input if the user does not have JavaScript enabled, so that they will still be able to manually enter a color. You don't need to add a single line of code.

Custom

If you want to get more into the functionality, just create a normal input and initialize it as a normal jQuery plugin. You can set a lot of options when initializing the colorpicker. See the 'Options' section below.

<input type='text' id="custom" />
<script>
$("#custom").spectrum({
    color: "#f00"
});
</script>

Flat

Flat This means that it will always show up at full size, and be positioned as an inline-block element. Look to the left for a full sized flat picker.

<input type='text' id="flat" />
$("#flat").spectrum({
    flat: true
});

Options

$("#picker").spectrum({
    color: tinycolor,
    flat: bool,
    showInput: bool,
    showInitial: bool,
    localStorageKey: string,
    showPalette: bool,
    showPaletteOnly: bool,
    showSelectionPalette: bool,
    cancelText: string,
    chooseText: string,
    className: string,
    preferredFormat: string,
    maxSelectionSize: int,
    palette: [[string]],
    selectionPalette: [string]
});

Color

The initial color will be set with the color option. If you don't pass in a color, Spectrum will use the value attribute on the input.

The color parsing is based on the TinyColor plugin. This should parse any color string you throw at it.

<input type='text' class='basic' value='red' />
<input type='text' class='basic' value='#0f0' />
<input type='text' class='basic' value='blue' />
<br />
<input type='text' class='override' />
            
<script>
$(".basic").spectrum();
$(".override").spectrum({
    color: "yellow"
});
</script>
            

Show Input

You can add an input to allow free form typing. The color parsing is very permissive in the allowed strings. See TinyColor for more details.

$("#showInput").spectrum({
    showInput: true
});
            

Show Palette

Spectrum can show a palette below the colorpicker to make it convenient for users to choose from frequently or recently used colors. When the colorpicker is closed, the current color will be added to the palette if it isn't there already. Check it out here:

$("#showPalette").spectrum({
    showPalette: true,
    palette: [
        ['black', 'white', 'blanchedalmond'], 
        ['rgb(255, 128, 0);', 'hsv 100 70 50', 'lightyellow']
    ]
});
            

Show Palette Only

see a working jsFiddle example

If you'd like, spectrum can show the palettes you specify, and nothing else.

$("#showPaletteOnly").spectrum({
    showPaletteOnly: true,
    color: 'blanchedalmond',
    palette: [
        ['black', 'white', 'blanchedalmond', 
        'rgb(255, 128, 0);', 'hsv 100 70 50'],
        ['red', 'yellow', 'green', 'blue', 'violet']
    ]
});
            
Result

Show Selection Palette

Spectrum can keep track of what has been selected by the user with the showSelectionPalette option.

If the localStorageKey option is defined, the selection will be saved in the browser's localStorage object

$("#showSelectionPalette").spectrum({
    showPalette: true,
    showSelectionPalette: true, // true by default
    palette: [ ]
});
$("#showSelectionPaletteStorage").spectrum({
    showPalette: true,
    showSelectionPalette: true,
    palette: [ ],
    localStorageKey: "spectrum.homepage", // Any Spectrum with the same string will share selection
});
            
This colorpicker will store what you pick:



Try reloading your page, it will still be here on this one:

Show Initial

Spectrum can show the color that was initially set when opening. This provides an easy way to click back to what was set when opened.

$("#showInitial").spectrum({
    showInitial: true
});
            

Show Input and Initial

Spectrum can show the color that was initially set when opening. This provides an easy way to click back to what was set when opened.

$("#showInputAndInitial").spectrum({
    showInitial: true,
    showInput: true
});
            

Button Text

You can set the button's text using cancelText and chooseText properties.

$("#buttonText").spectrum({
    chooseText: "Alright",
    cancelText: "No way"
});
            

Show Buttons

You can show or hide the buttons using the showButtons property. If there are no buttons, the behavior will be to fire the `change` event (and update the original input) when the picker is closed.

$("#hideButtons").spectrum({
    showButtons: false
});
            

Preferred Format

You can set the format that is displayed

$("#preferredHex").spectrum({
    preferredFormat: "hex",
    showInput: true
});
$("#preferredHsl").spectrum({
    preferredFormat: "hsl",
    showInput: true
});
$("#preferredRgb").spectrum({
    preferredFormat: "rgb",
    showInput: true
});
$("#preferredName").spectrum({
    preferredFormat: "name",
    showInput: true
});
$("#preferredNone").spectrum({
    showInput: true
});
            
Hex
Hsl
Rgb
Name (Falls back to hex)
None (Depends on input - try changing formats with the text box)

Events

$("#picker").spectrum({
    move: function(tinycolor) { },
    show: function(tinycolor) { },
    hide: function(tinycolor) { },
    beforeShow: function(tinycolor) { },
});

change

Called as the original input changes. Only happens when the input is closed or the 'Choose' button is clicked.

change: function(color) {               
    color.toHexString(); // #ff0000
}
            

move

Called as the user moves around within the colorpicker

move: function(color) {             
    color.toHexString(); // #ff0000
}
            

hide

Called after the colorpicker is hidden. This happens when clicking outside of the picker while it is open. Note, when any colorpicker on the page is shown it will hide any that are already open. This event is ignored on a flat colorpicker.

            hide: function(color) {             
                color.toHexString(); // #ff0000
            }
            

show

Called after the colorpicker is opened. This is ignored on a flat colorpicker. Note, when any colorpicker on the page is shown it will hide any that are already open.

            show: function(color) {             
                color.toHexString(); // #ff0000
            }
            

beforeShow

You can prevent the colorpicker from showing up if you return false in the beforeShow event. This event is ignored on a flat colorpicker.

beforeShow: function(color) {
    return false; // Will never show up
}
            

Methods

$("#picker").spectrum("show");
$("#picker").spectrum("hide");
$("#picker").spectrum("get");
$("#picker").spectrum("set", colorString);
$("#picker").spectrum("container");

Skinning

Since it is all built with HTML/CSS, you can skin it easily. There are two parts to the spectrum.css file, the core rules (at the top of the file), and the themable rules (at the bottom). Feel free to tweak these rules to make it look how you want.

Non-input elements

You can use any element you would like to trigger the colorpicker: Click me to open a colorpicker, though it is strongly recommended to stick with <input> tags.

Nitty Gritty

Browser Support

I wanted this to work in the latest and greatest browsers, but also target backwords compatibility and mobile support. Here are the currently supported browers:

IE Implementation

IE Support is provided using proprietary filters. Other browsers use CSS gradients.

Accepted Color Inputs

Spectrum will use the color passed in to initialize. If there is no color passed in, it will try to parse a color based on the value of the input. The color parsing is based on the TinyColor plugin, and accepts many forms of input:

red
#fff
fff
#ffffff
ffffff
rgb(255, 0, 0)
rgb 255 0 0
hsl(0, 100, 50)
hsl(0, 100%, 50%)
hsl 0 100 50
hsl 0 100% 50%
hsv(0, 100%, 100%)
hsv(0, 100, 100)
hsv 0 100% 100%
hsv 0 100 100

It also provides the following forms of output:

var t = $("#element").spectrum("get");
t.toHex()       // "ff0000"
t.toHexString() // "#ff0000"
t.toRgb()       // {"r":255,"g":0,"b":0}
t.toRgbString() // "rgb(255, 0, 0)"
t.toHsv()       // {"h":0,"s":1,"v":1}
t.toHsvString() // "hsv(0, 100%, 100%)"
t.toHsl()       // {"h":0,"s":1,"l":0.5}
t.toHslString() // "hsl(0, 100%, 50%)"
t.toName()      // "red"