A simple color picker application written in pure JavaScript, for modern browsers.

Overview

Color Picker

A simple color picker application written in pure JavaScript, for modern browsers.

Has support for touch events. Touchy… touchy…

Color Picker

Demo and Documentation

Contribute

  • Please do not make pull requests by editing the files that are in the root of the project. They are generated automatically by the build tool.
  • Install Git and Node.js
  • Run git clone https://github.com/taufik-nurrohman/color-picker.git
  • Run cd color-picker && npm install --save-dev
  • Edit the files in the .github/source folder.
  • Run npm run pack to generate the production ready files.

Contributors

Code Contributors

This project exists thanks to all the people who contribute.

Contributors

Financial Contributors

Become a financial contributor and help us sustain our community.

Individuals

Contribute

Organizations

Support this project with your organization. Your logo will show up here with a link to your website.


Release Notes

2.3.2

  • Added keyboard controls example #71.
  • Removed picker.value property. Initial color data can be stored manually before initialization.
  • Removed state.parent option. Use picker.enter(parent) to append the color picker panel to parent.

2.3.0

Starting from this version, you need to have a form element to store the color data. We no longer support storing color data via data-color attribute and element content.

  • Added picker._set() method.
  • Added picker.value property to store the initial color data.
  • Removed picker.value() method. Use the picker.set() method instead.

2.2.6

  • Maintenance.
  • Prioritized maintainability over file size. Say hello to Node.js and ES6! 👋
  • Removed custom element example.
  • Restructured the test files.

2.1.6

  • Make color picker tweaks reusable by wrapping them in a function.
  • Modernized syntax.

2.1.5

  • Fixed minified code does not yet updated after release.

2.1.4

  • Removed CP._ method.
  • Small bug fixes.

2.1.3

  • Added transition effect example.
  • Removed opacity value rounding internally.
  • Updated to pass the Firefox extension validator (#57)

2.1.2

  • Updated the donation target.

2.1.1

  • Fixed common issue with ES6 module which does not reference the this scope to window object by default.

2.1.0

  • Added blur and focus hook that removed state.e option.
  • Added ability to clear the hook storage object if it’s empty.

2.0.3

  • Added CP.state property to set initial state globally.

2.0.2

  • Renamed state.events to state.e.

2.0.1

  • Removed CP.each() method.
  • Renamed CP.__instance__ to CP.instances.

2.0.0

  • Hooks function arguments is now contains red, green, blue and alpha color value instead of static hex color string value.
  • Reduced file size by removing all color supports other than hex.
  • Removed change:sv, change:h, start:sv, start:h, drag:sv, drag:h, stop:sv, stop:h hooks.

1.4.2

  • Changed to CSS flexbox for layout.
  • Fixed #48

1.4.1

  • Removed the instance parameter in the first function argument of the hook and move the this scope in the function body to the current color picker instance.
  • Trigger enter and exit hooks on enter and exit method call.

1.4.0

  • Renamed target property to source and picker property to self.

1.3.10

  • Automatic color picker size based on container’s font size.

1.3.9

  • Renamed trigger method to fire.

1.3.8

  • Fixed color picker panel position using HTMLElement.prototype.getBoundingClientRect() by @flamestream and @alex3683 #29

1.3.5

  • Allow users to change the plugin name from CP to another.

1.3.2

  • Allow users to override the fit method.

1.3.0

  • Converted all color converters into static function.

1.2.0

  • Added static __instance__ property to collect all of the color picker instance.

1.1.0

  • Fixed Chrome bug with desktop events #9

1.0.3

  • Fixed event.stopPropagation() issue #8

1.0.2

  • Added change hook.

1.0.0

  • Initial release.
Comments
  • Text input inside picker combined with multiple instances

    Text input inside picker combined with multiple instances

    Do you have a demo of the text input inside the color picker combined with multiple instances?

    https://taufik-nurrohman.js.org/color-picker/color-picker.picker.html https://taufik-nurrohman.js.org/color-picker/color-picker.picker-view-code.html

    I've tried to get it working but changing the value in the text input doesn't get the correct instance of the picker.

    I created a demo here but you might need to run it in debug mode to get it to work.

    https://codepen.io/asa-carter/pen/YzWLZOb

    opened by asacarter 9
  • Color picker doesn't use correct cursor position when placed inside popover

    Color picker doesn't use correct cursor position when placed inside popover

    I'm trying to use the color picker inside a Bootstrap popover, but when I do this, the color picker doesn't use the correct cursor position when selecting a color. It behaves as if the color picker and the cursor were in the upper left of the screen. You can see this behavior by clicking on any of the color swatches on this page. Is there any way I can get the color picker to use the correct cursor position?

    bug question 
    opened by DePasqualeOrg 9
  • Initialize instance with predefined color

    Initialize instance with predefined color

    Hi

    How can I initialize the color picker instance with the one set in target element https://jsfiddle.net/Greggg/916wwx1h/

    Any help would be appreciated

    help wanted 
    opened by gregBerthelot 8
  • Can we use

    Can we use "hs + b" controls rather than "sb + h"?

    Thanks for a great tool! It's the very best approach - to keep it simple to use and to extend.

    My only inconvenience is that it uses rectangle of saturation * brightness, plus a slider of hue.

    That approach is good to fine-tune colors of a given hue, but it's not the best way to quickly select different colors.

    Maybe that's OK for heavy duty things like Photoshop, but for simple web applications it seems to me it would be more convenient to have a large rectangle of hue * saturation, and a stripe slider of brightness.

    Then it's like choosing a color on a large palette, and then maybe adjusting brightness.

    I tried both approaches in different color pickers and I'm pretty sure.

    My JS skills are rudimentary, so I'd like to ask you, respected professionals: is it possible to make a control "hue on X, saturation on Y" + brightness?

    Thank you again ;).

    PS. I often want to choose several colors of the same brightness, rather than of the same hue; so fro me it is brightness that should go into a separate slider.

    question 
    opened by chang-zhao 8
  • Version 1.4.1 broke update callback

    Version 1.4.1 broke update callback

    Hello,

    I tried updating your library from 1.4.0 to 1.4.1 and noticed the picker is no longer passed into events which causes issues if multiple pickers are used in the same page. We use it like this (sorry for CoffeeScript):

    class Abc
      constructor: ->
        for input in @inputs
          $input = $(input)
          picker = new CP(input)
          $input.data('picker', picker)
          picker.on('change', this._handlePickerChange)
    
      _handlePickerChange: (color, picker) => # <-- picker is undefined in 1.4.1
        color  = "##{color}"
        $input = $(picker.source)
        # ... do other stuff
    

    Is this fixable or should we do things differently?

    Thank you, Ollie

    opened by ollie 7
  • Why does picker.on(

    Why does picker.on("change", ...) fire on page load?

    Anyone know why picker.on("change", function(color) {...}); fires on page load?

    Ideally I would like to only have it fire if the color picker is interacted with but for some reason it fires even if I never use the color picker.

    Basic example:

    <script>
    
        var source = document.querySelector('input'),
            picker = new CP(source);
    
        // prevent showing native color picker panel
        source.onclick = function(e) {
            e.preventDefault();
        };
    
        picker.on("change", function(color) {
            alert('fire');
            this.source.value = '#' + color;
        });
    
    </script>
    
    opened by jjrabbit 6
  • Calling enter() doesn't fire enter hook.

    Calling enter() doesn't fire enter hook.

    Because of my issues with visibility not updating, I'm having to manually manage my own visibility flag using the enter and exit hooks. This works pretty good except if I open the color picker manually with another button using:

    CP.__instance__.someColorPicker.enter()
    

    The enter hook doesn't get fired. This would be expected behavior no? As then I could put all my flag management code in one place - the enter and exit hooks versus having to add code wherever I call enter().

    Strangely, calling the above with exit() does indeed fire the exit hook.

    This can be reproduced easily by putting a console.log in the picker.on('enter') {}, create a new color picker instance, then in Chrome's console call CP.__instance__.someColorPicker.enter(). It won't fire the enter hook. However, it will fire if you manually click the color picker to enter it.

    bug 
    opened by evolross 6
  • Disabling Auto-Fit?

    Disabling Auto-Fit?

    Is there any way to disable the way the color-picker "auto fits" to the visible screen area?

    This is happening in my app and the color picker gets moved to an area that covers up my UI and leaves the user unable to click the necessary buttons to choose a color or close the color picker.

    Could I add a PR to pass this as a setting parameter when initiating? Or would this be something easy you could do?

    EDIT: I see there's a fit hook that gets called when the color picker gets auto fitted, but there doesn't seem to be any easy way to "undo" or cancel the auto fit using that hook.

    opened by evolross 6
  • Issue with Emitter

    Issue with Emitter

    When I install color-picker with npm, the following error occurs when I try to import the package:

    Module build failed: Error: ENOENT: no such file or directory, open '.../node_modules/emitter/dist/emit
    

    When I install the package with npm install --save emitter, the error seems to disappear in the terminal but a new one occurs in Chrome:

    Uncaught (in promise) TypeError: Emitter is not a function
    

    If you inspect the file index.js in node_modules, this seems to come from:

    /**
     * Mixin Emitter.
     */
    
    Emitter(ColorPicker.prototype);
    

    For the first issue, I suggest you add this package as a module dependency in package.json. For the second one, I don't have any answer.

    opened by LeCoupa 6
  • Added package.json and UMD loader wrapper

    Added package.json and UMD loader wrapper

    Hi Taufik, I found your color-picker widget a few weeks ago and decided to use it for a few projects that I have. I neede da basic color picker and love that yours (in comparison ot many others outthere) is light in size, dependency free and not bloated with too many extra feature. What I found to be missing (for me :) ) is a package.json, which allows folks to use package managers (ex. npm) and (most important), that it provides support for use with module patterns, so that the glboal window space is not automtically poluted with the CP namespace...

    With this contribution, I am providing:

    • A package.json file
    • Changes to color-picker.js to wrap the library in an UMD declaration.

    This will enable your library to be used with packge manager, as well as modern module loaders (ex. ES2015 import or AMD or CommonJS.

    Thanks for your work on this and for sharing it on GitHub.

    /Paul

    opened by purtuga 6
  • Change Hook Running On Init

    Change Hook Running On Init

    Hi guys, I have a weird issue the change hook is running on plugin initializing, here is the test i did:

    // Initialization
    let picker = new CP(element);
    
    // change hook listener
    picker.on('change', function(r, g, b, a) {
        // firing immediately 
        console.log(1);
    });
    
    opened by mgabi4488 5
  • React 17 Cannot read properties of null

    React 17 Cannot read properties of null

    In react error click <input name="thename" type="text" value="#0f0" onclick={(new CP(document.querySelector(input[name="thename"]))) error Cannot read properties of null (reading 'append')

    opened by immakdas 1
  • How dynamically trigger move of selected point in color-picker:sv element - n points up, left, down or right?

    How dynamically trigger move of selected point in color-picker:sv element - n points up, left, down or right?

    I would like to make it possible, in custom javascript, to move the selected round point in color-picker:sv element - using e.g. the up, down, left and right arrows (and/or using the I, M, J and L keys) on the keyboard. And I would like to make it so the selected point moves n points in a given direction on each press of the arrow - where the value of n is in relation to how many points there are in total horisontally (and vertically).

    How do i get number of points in the color-picker:sv element - horisontally and vertically? How do I move the selected point n points up, right, left or down - and trigger the same events that are triggered when I left click to select a color?

    enhancement 
    opened by bongobongo 36
  • Picker not working with numeric only hex values?

    Picker not working with numeric only hex values?

    Hi, it seems that if I have a data-color="#276895" , numeric only hex number, it does not work, and returns black as fallback. Also, is it ok for the "change" event to fire after init? Thanks.


    Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

    opened by tehmaestro 1
  • Fix picker position/cursor when page has non-fixed position.

    Fix picker position/cursor when page has non-fixed position.

    This is a solution for #29. The easiest way was to force the placement of the picker to be placed at root child, and adjust origins from there, losing the custom container/bucket feature.

    opened by flamestream 2
  • Set the crosshair cursor for the hue slider

    Set the crosshair cursor for the hue slider

    Cursor: crosshair allows precise selecting of the color more easily than "ns-resize". Semantically it is also correct: rather than "regulating hue" we usually try to select the point of the necessary color.

    help wanted 
    opened by chang-zhao 2
Releases(v2.4.4)
Owner
Taufik Nurrohman
I have a dream, but it won’t make me rich.
Taufik Nurrohman
Colormath.js - a color conversion and color manipulation library written in typescript for Node.js, Deno and Browser

Colormath.js - a color conversion and color manipulation library written in typescript for Node.js, Deno and Browser

TheSudarsanDev 8 Feb 8, 2022
A simple Discord bot that will listen for HEX, RGB(a), and HSL(a) colors in a message, and provide a small image of that color.

Pigment For the teams of designers and developers out there - Pigment will listen for messages containing a HEX, RGB(a), or HSL(a) color, and provide

Conrad Crawford 17 Dec 8, 2022
JavaScript library for all kinds of color manipulations

Chroma.js Chroma.js is a tiny small-ish zero-dependency JavaScript library (13.5kB) for all kinds of color conversions and color scales. Usage Initiat

Gregor Aisch 9.2k Jan 4, 2023
:rainbow: Javascript color conversion and manipulation library

color JavaScript library for immutable color conversion and manipulation with support for CSS color strings. var color = Color('#7743CE').alpha(0.5).l

Qix 4.5k Jan 3, 2023
JavaScript Library for creating random pleasing colors and color schemes

#PleaseJS www.checkman.io/please Please.js is a polite companion that wants to help you make your projects beautiful. It uses HSV color space to creat

Jordan Checkman 2.3k Dec 22, 2022
Fast, small color manipulation and conversion for JavaScript

TinyColor JavaScript color tooling TinyColor is a small, fast library for color manipulation and conversion in JavaScript. It allows many forms of inp

Brian Grinstead 4.5k Jan 1, 2023
Color2k - a color parsing and manipulation lib served in roughly 2kB

color2k a color parsing and manipulation lib served in roughly 2kB or less (2.8kB to be more precise) color2k is a color parsing and manipulation libr

Rico Kahler 506 Dec 31, 2022
A CLI utility to calculate/verify accessible magic numbers for a color palette.

A11y Contrast A CLI utility to calculate/verify accessible magic numbers for a color palette. Read my blog post for some more information. Installatio

Darek Kay 23 Nov 13, 2022
A dependency-free image color extraction library

Color mage A dependency-free image color extraction library. The extraction consists of using K-Means algorithm. It has a few utility functions as wel

Gilmar Quinelato 4 Mar 11, 2022
Create your own complete Web color system fast and easy!

Simpler Color Create your own complete Web color system fast and easy, from as little as one base color! Color is at the heart of every UI design syst

Arnel Enero 278 Dec 20, 2022
A tool for creating and maintaining cohesive, consistent, and accessible color palettes

Primer Prism Primer Prism is a tool for creating and maintaining cohesive, consistent, and accessible color palettes. ?? primer.style/prism ?? Read th

Primer 517 Jan 3, 2023
A array with color name -> decimal rgbs.

colors-named-decimal A array with color name -> decimal rgbs. Based on https://www.w3.org/TR/css-color-4/#colors-named Installation This package is ES

小弟调调™ 4 Jul 20, 2022
A TypeScript package to calculate WCAG 2.0/3.0 and APCA color contrasts

a11y-color-contrast A simple utility package for working with WCAG 2.2/3.0 color contrasts a11y: Built for checking how readable colors are together S

Sondre Aasemoen 10 Oct 10, 2022
A simple color picker application written in pure JavaScript, for modern browsers.

Color Picker A simple color picker application written in pure JavaScript, for modern browsers. Has support for touch events. Touchy… touchy… Demo and

Taufik Nurrohman 207 Dec 14, 2022
Tool Cool Color Picker is a color picker library written in typescript and using web component technologies.

Tool Cool Color Picker Tool Cool Color Picker is a color picker library written in typescript and using web component technologies. Check out the demo

Tool Cool 13 Oct 23, 2022
Colr Pickr, a vanilla JavaScript color picker component built with SVGs, with features like saving colors. Similar design to the chrome-dev-tools color picker.

Colr Pickr Colr Pickr, a vanilla JavaScript color picking component built with SVGs, with features like saving colors. Similar design to the chrome-de

TEK 27 Jun 27, 2022
Duet Date Picker is an open source version of Duet Design System’s accessible date picker. Try live example at https://duetds.github.io/date-picker/

Duet Date Picker Duet Date Picker is an open source version of Duet Design System’s accessible date picker. Duet Date Picker can be implemented and us

Duet Design System 1.6k Jan 6, 2023
Coloris - A lightweight and elegant JavaScript color picker. Written in vanilla ES6, no dependencies. Accessible.

Coloris A lightweight and elegant JavaScript color picker written in vanilla ES6. Convert any text input field into a color field. View demo Features

Momo Bassit 126 Dec 27, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Colormath.js - a color conversion and color manipulation library written in typescript for Node.js, Deno and Browser

Colormath.js - a color conversion and color manipulation library written in typescript for Node.js, Deno and Browser

TheSudarsanDev 8 Feb 8, 2022