Skip to content
gruppler edited this page Jan 24, 2012 · 22 revisions

Legal stuff

CSS3 ColorPicker (https://github.com/gruppler/CSS3-Colorpicker)
Copyright © 2011 Craig Laparo (https://plus.google.com/114746898337682206892)
Based on "PhotoShop-like JavaScript Color Picker"
Copyright © 2007 John Dyer (http://johndyer.name)
MIT style license

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Include these in your header:

<link rel="stylesheet" type="text/css" href="colorpicker.css"/>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="colorpicker.js"></script>

Apply to your selection:

$('.color').colorpicker();
$('#color1').colorpicker({
    realtime: false,
    invertControls: false,
    controlStyle: 'raised'
});

Options:

showAnim:       true        // [true|false] to enable/disable animations
duration:       200         // Fade duration
color:          'FFF'       // Default color
allowNull:      false       // [true|false] to allow an empty color value;
                            //   otherwise default to the default color
realtime:       true        // [true|false] to enable/disable instant updates
invertControls: true        // [true|false] to enable/disable inverting color
                            //   of mouse controls based on luminance
controlStyle:   'simple'    // Mouse control theme [simple|raised|inset];
                            //   separate multiple themes with a space
swatches:       true        // [true|false] to enable/disable,
                            //   or an array of hex codes to pre-fill
alpha:          false       // [true|false] to enable/disable alpha
alphaHex:       false       // [true|false] to enable/disable 4-byte hex
                            //   in the format '#AARRGGBB'

Options can be retrieved or changed at any time:

// Get option's current value
$(".selector").colorpicker("option", "color");

// Set option
$(".selector").colorpicker("option", "color", "fff");

Events:

beforeShow(input, inst)       // Fired before the color picker is shown
onClose(color, inst)          // Fired when the color picker is hidden
onSelect(color, inst)         // Fired when the color is set
onAddSwatch(color, swatches)  // Fired when a new color swatch is added

Event handlers can be set just like options:

// At initialization
$(".selector").colorpicker({
    onSelect: function(color, inst){...}
});

// After initialization
$(".selector").colorpicker('option', 'onSelect', function(color, inst){...});

Public methods:

setDefaults(settings)  // Set the defaults for all color pickers
addSwatch(color, newOnly) // Add a color or array of colors to the swatches;
                       //   preserve sorting if 'newOnly' is true
clearSwatches()        // Remove all swatches
refresh()              // Update the color picker
color(args)            // A useful color class
hexToRgb(hex, alphaOn) // Returns {r:[0-255], g:[0-255], b:[0-255], a:[0-100]}
validateHex(hex, norm, alphaOn) // Returns a valid hex code,
                       //   optionally normalized
rgbToHex(rgb, alphaOn) // rgb = {r:[0-255], g:[0-255], b:[0-255], [a:[0-100]]}
intToHex(dec)          // Base conversion from 10 to 16
hextToInt(hex)         // Base conversion from 16 to 10
rgbToLum(rgb)          // rgb = {r:[0-255], g:[0-255], b:[0-255]}
                       //   Returns integer [0-100]
rgbToHsv(rgb)          // rgb = {r:[0-255], g:[0-255], b:[0-255]}
                       //   Returns {h:[0-359], s:[0-100], v:[0-100]}
hsvToRgb(rgb)          // hsv = {h:[0-359], s:[0-100], v:[0-100]}
                       //   Returns {r:[0-255], g:[0-255], b:[0-255]}

To use a public method, call it like this:

// Validate 'hex'
$.colorpicker.validateHex('hex');

// Create a color object
var myColor = new $.colorpicker.color({hex: 'ccc'});

Clone this wiki locally