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:
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.
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.
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.
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.
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 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
});
$("#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]
});
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>
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
});
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']
]
});
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']
]
});
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
});
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
});
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
});
You can set the button's text using cancelText and chooseText properties.
$("#buttonText").spectrum({
chooseText: "Alright",
cancelText: "No way"
});
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
});
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
});
$("#picker").spectrum({
move: function(tinycolor) { },
show: function(tinycolor) { },
hide: function(tinycolor) { },
beforeShow: function(tinycolor) { },
});
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
}
Called as the user moves around within the colorpicker
move: function(color) {
color.toHexString(); // #ff0000
}
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
}
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
}
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
}
$("#picker").spectrum("show");
$("#picker").spectrum("hide");
$("#picker").spectrum("get");
$("#picker").spectrum("set", colorString);
$("#picker").spectrum("container");
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.
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.
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 Support is provided using proprietary filters. Other browsers use CSS gradients.
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"