Flat, Simple, Hackable Color-Picker.
- Simple usage
- Zero dependencies
- Multiple color representations
- Color comparison
- Opacity control
- Detail adjustments via. mouse-wheel
- Responsive and auto-positioning
- Supports touch devices
- Swatches for quick-selection
- Fully accessible and i18n
- Shadow-dom support
Status of this project
This project might continue to get important security- and bug-related updates but its feature set is frozen, and it's highly unlikely that it'll get new features or enhancements.
The reason behind this decision is the way this tool has been build (monolithic, the core is one single file, everything is in plain JS etc.) which makes it incredible hard to maintain, tests become impossible at this stage without a complete rewrite, and the fun is gone at such a level of cramped complexity.
Personally I recommend building these UI-Related "widgets" directly into the app with the framework you're using which takes more time but in return gives you full power of how it should work and look like. Frameworks such as (p)react, vue and svelte will make it a breeze to develop such things within a day.
Nano uses css-grid thus it won't work in older browsers.
Note: The readme is always up-to-date with the latest commit. See Releases for installation instructions regarding to the latest version.
Install via npm:
$ npm install @simonwep/pickr
Install via yarn:
$ yarn add @simonwep/pickr
Include code and style:
// One of the following themes; // 'classic' theme; // 'monolith' theme; // 'nano' theme// Modern or es5 bundle (pay attention to the note below!);;
Attention: The es5-bundle (e.g. legacy version) is quite big (around a triple of the modern bundle). Please take into consideration to use the modern version and add polyfills later to your final bundle! (Or better: give a hint to users that they should use the latest browsers). Browsers such as IE are not supported (at least not officially).
<!-- One of the following themes --><!-- 'classic' theme --><!-- 'monolith' theme --><!-- 'nano' theme --><!-- Modern or es5 bundle -->
Be sure to load the
pickr.min.js (or the es5 version) after
pickr.min.css. Moreover the
script tag doesn't work with the
// Simple example, see optional options for more configuration.const pickr = Pickr;
You can find more examples here.
0.4.x Pickr is event-driven. Use the
on(event, cb) and
off(event, cb) functions to bind / unbind eventlistener.
||Initialization done - pickr can be used||
||Pickr got closed||
||Pickr got opened||
||User clicked the save / clear button. Also fired on clear with
||User cleared the color.||
||Color has changed (but not saved). Also fired on
||User stopped to change the color||
||User clicked the cancel button (return to previous color).||
||User clicked one of the swatches||
const pickr =// Selector or element which will be replaced with the actual color-picker.// Can be a HTMLElement.el: '.color-picker'// Where the pickr-app should be added as child.container: 'body'// Which theme you want to use. Can be 'classic', 'monolith' or 'nano'theme: 'classic'// Nested scrolling is currently not supported and as this would be really sophisticated to add this// it's easier to set this to true which will hide pickr if the user scrolls the area behind it.closeOnScroll: false// Custom class which gets added to the pcr-app. Can be used to apply custom styles.appClass: 'custom-class'// Don't replace 'el' Element with the pickr-button, instead use 'el' as a button.// If true, appendToBody will also be automatically true.useAsButton: false// Size of gap between pickr (widget) and the corresponding reference (button) in pxpadding: 8// If true pickr won't be floating, and instead will append after the in el resolved element.// It's possible to hide it via .hide() anyway.inline: false// If true, pickr will be repositioned automatically on page scroll or window resize.// Can be set to false to make custom positioning easier.autoReposition: true// Defines the direction in which the knobs of hue and opacity can be moved.// 'v' => opacity- and hue-slider can both only moved vertically.// 'hv' => opacity-slider can be moved horizontally and hue-slider vertically.// Can be used to apply custom layoutssliders: 'v'// Start state. If true 'disabled' will be added to the button's classlist.disabled: false// If true, the user won't be able to adjust any opacity.// Opacity will be locked at 1 and the opacity slider will be removed.// The HSVaColor object also doesn't contain an alpha, so the toString() methods just// print HSV, HSL, RGB, HEX, etc.lockOpacity: false// Precision of output string (only effective if components.interaction.input is true)outputPrecision: 0// Defines change/save behavior:// - to keep current color in place until Save is pressed, set to `true`,// - to apply color to button and preview (save) in sync with each change// (from picker or palette), set to `false`.comparison: true// Default color. If you're using a named color such as red, white ... set// a value for defaultRepresentation too as there is no button for named-colors.default: '#42445a'// Optional color swatches. When null, swatches are disabled.// Types are all those which can be produced by pickr e.g. hex(a), hsv(a), hsl(a), rgb(a), cmyk, and also CSS color names like 'magenta'.// Example: swatches: ['#F44336', '#E91E63', '#9C27B0', '#673AB7'],swatches: null// Default color representation of the input/output textbox.// Valid options are `HEX`, `RGBA`, `HSVA`, `HSLA` and `CMYK`.defaultRepresentation: 'HEX'// Option to keep the color picker always visible.// You can still hide / show it via 'pickr.hide()' and 'pickr.show()'.// The save button keeps its functionality, so still fires the onSave event when clicked.showAlways: false// Close pickr with a keypress.// Default is 'Escape'. Can be the event key or code.// (see: )closeWithKey: 'Escape'// Defines the position of the color-picker.// Any combinations of top, left, bottom or right with one of these optional modifiers: start, middle, end// Examples: top-start / right-end// If clipping occurs, the color picker will automatically choose its position.// Pickr uses as positioning-engine.position: 'bottom-middle'// Enables the ability to change numbers in an input field with the scroll-wheel.// To use it set the cursor on a position where a number is and scroll, use ctrl to make steps of fiveadjustableNumbers: true// Show or hide specific components.// By default only the palette (and the save button) is visible.components:// Defines if the palette itself should be visible.// Will be overwritten with true if preview, opacity or hue are truepalette: truepreview: true // Display comparison between previous state and new coloropacity: true // Display opacity sliderhue: true // Display hue slider// show or hide components on the bottom interaction bar.interaction:// Buttons, if you disable one but use the format in default: or setColor() - set the representation-type too!hex: false // Display 'input/output format as hex' button (hexadecimal representation of the rgba value)rgba: false // Display 'input/output format as rgba' button (red green blue and alpha)hsla: false // Display 'input/output format as hsla' button (hue saturation lightness and alpha)hsva: false // Display 'input/output format as hsva' button (hue saturation value and alpha)cmyk: false // Display 'input/output format as cmyk' button (cyan mangenta yellow key )input: false // Display input/output textbox which shows the selected color value.// the format of the input is determined by defaultRepresentation,// and can be changed by the user with the buttons set by hex, rgba, hsla, etc (above).cancel: false // Display Cancel Button, resets the color to the previous stateclear: false // Display Clear Button; same as cancel, but keeps the window opensave: false // Display Save Button,// Translations, these are the default values.i18n:// Strings visible in the UI'ui:dialog': 'color picker dialog''btn:toggle': 'toggle color picker dialog''btn:swatch': 'color swatch''btn:last-color': 'use previous color''btn:save': 'Save''btn:cancel': 'Cancel''btn:clear': 'Clear'// Strings used for aria-labels'aria:btn:save': 'save and close''aria:btn:cancel': 'cancel and close''aria:btn:clear': 'clear and close''aria:input': 'color input field''aria:palette': 'color selection area''aria:hue': 'hue selection slider''aria:opacity': 'selection slider';
Selection through a Shadow-DOM
To select the
.pickr element you can use the custom
>> shadow-dom-selector in
el: '.entry >> .innr .another >> .pickr'
ShadowRoot of the query-result behind a
>> gets used in the next query selection.
An alternative would be to provide the target-element itself as
The HSVaColor object
As default color representation is hsva (
alpha) used, but you can also convert it to other formats as listed below.
- hsva.toHSVA() - Converts the object to a hsva array.
- hsva.toHSLA() - Converts the object to a hsla array.
- hsva.toRGBA() - Converts the object to a rgba array.
- hsva.toHEXA() - Converts the object to a hexa-decimal array.
- hsva.toCMYK() - Converts the object to a cmyk array.
- hsva.clone() - Clones the color object.
toString() is overridden so you can get a color representation string.
hsva; // Returns [r, g, b, a]hsva; // Returns rgba(r, g, b, a) with highest precisionhsva; // Returns rgba(r, g, b, a), rounded to the third decimal
:Boolean) - Set an color, returns true if the color has been accepted.
:String | null, silent
:Boolean- Parses a string which represents a color (e.g.
rgb(10, 156, 23)) or name e.g. 'magenta', returns true if the color has been accepted.
nullwill clear the color.
silent is true (Default is false), the button won't change the current color.
:Pickr- Appends an event listener to the given corresponding event-name (see section Events).
:Pickr- Removes an event listener from the given corresponding event-name (see section Events).
:Pickr- Shows the color-picker.
:Pickr- Hides the color-picker.
:Pickr- Disables pickr and adds the
disabledclass to the button.
:Pickr- Enables pickr and removes the
disabledclass from the button.
:Pickr- Returns true if the color picker is currently open.
:Object- Returns the dom-tree of pickr as tree-structure.
:HSVaColor- Returns the current HSVaColor object.
:HSVaColor- Returns the currently applied color.
- pickr.destroy() - Destroys all functionality.
- pickr.destroyAndRemove() - Destroys all functionality and removes the pickr element including the button.
:Boolean- Change the current color-representation. Valid options are
CMYK, returns false if type was invalid.
:String- Returns the currently used color-representation (eg.
:Pickr- Same as pressing the save button. If silent is true the
onSaveevent won't be called.
:Boolean- Adds a color to the swatch palette. Returns
trueif the color has been successful added to the palette.
:Boolean- Removes a color from the swatch palette by its index, returns true if successful.
:Pickr- Creates a new instance.
:Object]) - Attach an event handle which will be fired only once
:Object]) - Attach an event handler function.
:Object]) - Remove an event handler.
:HTMLElement- Creates an new HTML Element out of this string.
:[HTMLElement]- A polyfill for the event-path event propery.
:String) - See inline doumentation.
:String|HTMLElement) - Resolves a
>>>as shadow dom selector.
:Function) - Creates the possibility to change the numbers in an inputfield via mouse scrolling. The mapper function takes three arguments: the matched number, an multiplier and the index of the match.
Use this utils carefully, it's not for sure that they will stay forever!
- version - The current version.
- I18N_DEFAULTS - i18n default values.
- DEFAULT_OPTIONS - Default options (Do not override this property itself, only change properties of it!).
How do I initialize multiple pickr's? Can I access the instance via
No, you can't. You need to keep track of your instance variables - pickr is (not yet) a web-component.
The best option would be to create new elements via
document.createElement and directly pass it as
I want to use pickr in a form, how can I do that?
You can use
useAsButton: true and pass a reference (or selector) of your input-element as
el. Then you can update the input-element whenever a change was made. example.
I want to update options after mounting pickr, is that possible?
Unfortunately not. The core-code of this project is rather old (over 2 years), and I made it in my early js-days - the widget is not able to dynamically re-render itself in that way. You have to destroy and re-initialize it.
If you want to open a issue, create a Pull Request or simply want to know how you can run it on your local machine, please read the Contributing guide.