JSColorPicker
JSFontPicker
Star

Javascript Color PickerJavascript Color Picker

An open source, free (as in beer), versatile, flexible and lightweight Javascript Color Picker Component supporting light and dark skins, swatches, multiple color formats, CSS color parsing, instant and confirm modes and much more.


Tap to pick color

Contents

JS Color Picker Demo

Theme:
Button element
Edit on
Input element
Edit on
Anchored to element (headless)
#069eff
Edit on
Positioned dialog (headless)
Edit on
Swatches only
Edit on

JS Color Picker Features

Download JS Color Picker

Download
Please visit our GitHub repository to download JSColorPicker.

Getting Started with JS Color Picker

JSColorPicker requires a tiny stylesheet. Please include it like this:

<link rel="stylesheet" href="colorpicker.min.css" />

Now, depending on your environment, choose one of the following:

IIFE Bundle

Import the IIFE script using a script tag in your HTML:

<script src="colorpicker.iife.min.js"></script>

This exposes the ColorPicker class (on the global window object).

ESM Bundle

Import the ESM bundle using the import directive in your script:

import ColorPicker from 'colorpicker.min.js'

This allows you to use ColorPicker directly.

<!doctype html>
<html>
<head>
  <link rel="stylesheet" href="/css/colorpicker.min.css">
</head>
<body>
  <button id="demoButton"></button>

  <script type="module">
    import ColorPicker from '/js/colorpicker.min.js'
    new ColorPicker('#demoButton')
  </script>
</body>
</html>

JS Color Picker Options

Configure the ColorPicker by passing the element to bind it to and a configuration object. The element to bind to (first argument to ColorPicker constructor) can either be:

The ColorPicker can behave in three ways:

Configuration

const picker = new ColorPicker(element, {
  /**
   * How to render the ColorPicker: as an input element or a button?
   * Default: 'button'
   */
  toggleStyle: 'button' | 'input',

  /**
   * When enabled, run the picker in headless mode:
   * - leaves the target element untouched, and does not render a toggle
   * - requires manually calling the prompt() method to show the dialog
   * - still positions the dialog relative to the target element
   * Default: false
   */
  headless: false,

  /**
   * The HTML element the picker dialog will be appended to. When you experience
   * issues focusing the input element in the ColorPicker, changing this will help.
   * Required when using the ColorPicker inside a Bootstrap OffCanvas or Modal.
   * By default, this is the body.
   */
  container: HTMLElement | string | null,

  /**
   * The initial color.
   * When toggleStyle == 'input', the default color will be taken from the input's
   * value (set color to null).
   * You can also set a data-color attribute on the element the ColorPicker is
   * attached to (set color to null).
   * Default: null
   */
  color: string | null,

  /**
   * A list of predefined color swatches available for selection.
   * Pass null, false or an empty array to disable them altogether.
   * Default: null
   */
  swatches: string[] | null | false,

  /**
   * Hide hsv, hue and alpha sliders as well as format selector and input field.
   * Keep swatches only. Picking a color will close the dialog.
   * You must provide an array of swatches as well (duh!)
   * Default: false
   */
  swatchesOnly: boolean,

  /**
   * Whether to enable the alpha (transparency) slider.
   * Default: true
   */
  enableAlpha: boolean,

  /**
   * Whether to enable the built-in eyedropper tool for selecting colors from the screen.
   * As of January 2025, this is only supported on Chromium based browsers: https://caniuse.com/mdn-api_eyedropper
   * Default: true
   */
  enableEyedropper: boolean,

  /**
   * The set of color formats the user can choose from.
   * Pass null or false to disable format selection.
   * Default: ['hex', 'rgb', 'hsv', 'hsl']
   */
  formats: ColorFormat[] | null | false,

  /**
   * The default color format to select when multiple formats are enabled.
	* If there are no formats to choose from (formats: null|false), this set
	s the color format of the color picker's input field.
   * Default: 'hex'
   */
  defaultFormat: 'hex' | 'rgb' | 'hsv' | 'hsl',

  /**
   * Determines how the chosen color is applied:
   * - 'instant': applies immediately as the user picks a color
   * - 'confirm': requires user confirmation (via a submit button)
   * Default: 'confirm'
   */
  submitMode: 'instant' | 'confirm',

  /**
   * Whether to show the clear button for resetting the color.
   * Default: false
   */
  showClearButton: boolean

  /**
   * Whether the color picker should close when clicking outside of it.
   * Default: true
   */
  dismissOnOutsideClick: boolean

  /**
   * Whether the color picker should close when escape is pressed.
   * Default: true
   */
  dismissOnEscape: boolean

  /**
   * How to place the dialog relative to the toggle. This is where the color dialog
   * will be shown, if there is room. If not, the dialog will be shown there where
   * there is room.
   * Default: 'top'
   */
  dialogPlacement: 'top' | 'bottom' | 'left' | 'right'

  /**
   * How big the gap between the toggle and dialog should be, in pixels.
   * Default: 8
   */
  dialogOffset: number

  /**
   * How to place the dialog when no anchor element is defined.
   * Default: 'top'
   */
  staticPlacement: 'top left' | 'top center' | 'top right' |
                   'center left' | 'center center' | 'center right' |
                   'bottom left' | 'bottom center' | 'bottom right'

  /**
   * How big the gap between the dialog and the edge of the page should be, in pixels.
   * Default: 8
   */
  staticOffset: number
})

JS Color Picker Methods

The following methods can be called on a ColorPicker instance:

/**
 * Get selected color
 * @returns {Color}
 */
const color = picker.color
console.log('Picked color', color ? color.string('rgb' /* hex|rgb|hsl|hsv */) : null)

/**
 * Get swatches
 * @returns string[]
 */
console.log(picker.swatches)

/**
 * Get currently selected (not submitted) color
 * useful when submitMode == 'confirm'
 * Returns {Color}
 */
console.log(picker.selectedColor)

/**
 * Set a color
 * @param {Color} color. Can either be
 * - a hex(a) or rgb(a) string:  '#f0f', '#aabbcc99', 'rgb(255,0,0)', 'rgba(0,128,244,.5)'
 * - a named color: 'red', 'black', 'transparent'
 * - an array of numbers, representing H, S, V, A: [.6, .5, 1, .5]
 * - a Color object (see below)
 * @param {bool} emit Whether or not to emit pick event
 */
picker.setColor(color: Color | number[] | string | null, emit = true)

/**
 * Update swatches
 * @param {array} swatches An array of new colors for swatches
 *                Use null/false for no swatches
 */
picker.setSwatches(swatches: string[] | null | false)

/**
 * Set format
 * @param {string} format Either 'rgb', 'hex', 'hsv' or 'hsl'
 * @param {bool} update Whether or not to update/convert the selected color
 */
picker.setFormat(format: ColorFormat, update = true)

/**
 * Clear the color
 * @param {bool} emit Whether or not to emit pick event
 */
picker.clear(emit = true)

/**
 * Open the ColorPicker.
 * @param {bool} emit Whether or not to emit open/opened events
 */
picker.open(emit = true)

/**
 * Close the ColorPicker.
 * @param {bool} emit Whether or not to emit close/closed events
 */
picker.close(emit = true)

/**
 * Toggle the ColorPicker. Open when closed, close when opened
 * @param {bool} value Whether to close or open the ColorPicker
 * @param {bool} emit Whether or not to emit open/opened and close/closed events
 */
picker.toggle(value = !this._open, emit = true)

/**
 * Destroy the picker, restore DOM to what it was before the ColorPicker was attached
 */
picker.destroy()

Dialog only with prompt

If you want to open a picker dialog without an explicit toggle element, (optionally) destroying it after, you can use the headless option with the prompt() method. prompt() returns a promise once the picker is closed.

Position the dialog relative to a DOM element

The colorpicker dialog will open anchored to a DOM element (first arg to ColorPicker constructor). Use the dialogPlacement config to control where the dialog appears, relative to this DOM element.
CodePen example.

const picker = new ColorPicker(element, {
  headless: true,
  // dialogPlacement: 'top' | 'bottom' | 'left' | 'right'
})

const color = await picker.prompt(true /* destroy instance after color is picked */)
// color now contains a Color object, see below

Position the dialog on a fixed position

The colorpicker dialog will open on a fixed position. Use null as the first argument to the ColorPicker constructor. Use the staticPlacement config option to determine the dialog's position on screen. Options are: top left, top center, top right, center left, center center, center right, bottom left, bottom center, bottom right. Optionally use the staticOffset config option to alter the dialog's offset.
CodePen example.

const picker = new ColorPicker(null, {
  headless: true,
  staticPlacement: 'center center',
  staticOffset: 10 // in pixels
})
.on('pick', color => {
  console.log('You picked', color.string('hex'))
})
.prompt()

Retrieving selected color

You can retrieve the color the user selected in several ways.

JS Color Picker Events

If you want to listen to certain events, like opening or picking, use the .on() method:

/**
 * The pick event is fired when the user selects a color.
 * When submitMode == 'instant', this event is fired as soon as a color is picked
 * When submitMode == 'confirm', this event is fired when the user clicks the Submit button
 * The color argument is either null (when the color is cleared, showClearButton == true) or a Color object, see below
 */
picker.on('pick', (color) => console.log('pick', color))

/**
 * The following events are fired when the picker is opening/closing and opened/closed (transition is complete)
 */
picker.on('open', () => console.log('open'))
picker.on('opened', () => console.log('opened'))
picker.on('close', () => console.log('close'))
picker.on('closed', () => console.log('closed'))

JS Color Picker Color object

For maximum flexibility, the pick event receives a Color object. You can query this object to retrieve the color the user selected in any format you like: RGB(A), HEX(A), HSV or HSL.
Use the string function on the Color object for this.

picker.on('pick', (color) => {
  if (!color) {
    return console.log('Color cleared')
  }
  console.log(
    'Color picked',
    // When a Color object is converted to a string, you'll get HEX(A) notation
    color.toString(),
    color.string('hex'),
    color.string('rgb'), // returns `rgb(r,g,b)`, or `rgba(r,g,b,a)` when opaque
    color.string('rgba'), // always returns `rgba(r,g,b,a)`
    color.string('hsv'),
    color.string('hsl')
  )
 })

When a rgb color is requested (color.string('rgb')), and the color has opacity applied (alpha < 1), then you will receive an rgba color value.

JS Color Picker Customization

Color scheme

You can change the color scheme (light & dark) by using one of the following dataset properties:

<html data-cp-theme="dark"></html>
<html data-bs-theme="dark"></html>
<html data-cp-theme="light"></html>
<html data-bs-theme="light"></html>

CSS variables

You can alter a variety of styles using their respective CSS variables:

/* Metrics */
--cp-size: 2.375rem; /* Size of color picker thumb */
--cp-border-radius-sm: 0.25rem;
--cp-border-radius-lg: 0.5rem;
--cp-swatch-width: 2rem;

/* Colors */
--cp-body-bg: #fff;
--cp-body-color: #212529;
--cp-body-color-rgb: 33, 37, 41;
--cp-border-color: #ccc;
--cp-button-color: #ccc;
--cp-border-color-translucent: rgba(0, 0, 0, 0.175);
--cp-tertiary-color: rgba(33, 37, 41, 0.5);

/* Shadows */
--cp-box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
--cp-box-shadow-sm: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);

/* Animations (set to 0s to disable) */
--cp-delay: 150ms;

JS Color Picker Tips

License

JSColorPicker was created by Zygomatic, is open source and licensed under the MIT license.

cards
Powered by paypal