• Stars
    star
    1,265
  • Rank 36,218 (Top 0.8 %)
  • Language
    JavaScript
  • License
    Mozilla Public Li...
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐ŸŽจ Modular color picker widget for JavaScript, with support for a bunch of color formats


iro.js

Modular, design-conscious color picker widget for JavaScript - with support for a bunch of color formats | iro.js.org

license version downloads minzip size no jQuery

Features | Demo | Installation | Usage | Documentation | Special Thanks | Forum



NOTE: this project is currently on hiatus until further notice, as working on open source projects is becoming unsustainable for the maintainer.


Features

  • Work with colors in hex, RGB, HSV and HSL formats (plus kelvin temperatures!) in one simple, frictionless API
  • Add multiple colors to the same color picker for selecting color harmonies and themes
  • Create the perfect color picker from a selection of pre-built UI components
  • All of iro.js can run from a single script - no extra CSS, images, or third-party libraries required!
  • ~9kb minified and gzipped
  • Licenced under MPL 2.0 - 100% free for personal and commercial use

Codepen Demos

Installation

Install with NPM

npm install @jaames/iro --save

If you are using a module bundler like Webpack or Rollup, import iro.js into your project:

// Using ES6 module syntax
import iro from '@jaames/iro';

// Using CommonJS modules
const iro = require('@jaames/iro');

Using the jsDelivr CDN

<script src="https://cdn.jsdelivr.net/npm/@jaames/iro@5"></script>

When you manually include the library like this, iro.js will be made globally available on window.iro.

Download and host yourself

Development version
Uncompressed, with source comments included. Intended for debugging.

Production version
Minified and optimized version.

Then add it to the <head> of your page with a <script> tag:

<html>
  <head>
    <!-- ... -->
    <script src="./path/to/iro.min.js"></script>
  </head>
  <!-- ... -->
</html>

Usage

Getting Started

First, we need a HTML element with a unique identifier (like an id attribute) to act as a container for the color picker:

<div id="picker"></div>

Then use JavaScript to create a new iro.ColorPicker with a CSS selector that matches your container element:

var colorPicker = new iro.ColorPicker('#picker');

You can also use a DOM object instead of a CSS selector here -- this might be more suitable if you're integrating iro.js into an application built with a framework such as Vue, React, etc.

Color Picker Options

The color picker can be customized by passing a set of options to the second iro.ColorPicker parameter:

var colorPicker = new iro.ColorPicker("#picker", {
    // Set the size of the color picker
    width: 320,
    // Set the initial color to pure red
    color: "#f00"
});

Available Options

OptionPurposeDefault Value
widthTotal width of the control UI.300
colorThe initial color value. This can be any [supported color format](https://iro.js.org/color_api.html#supported-color-formats).#ffffff
colorsInitial color values used for [multi-color selections](https://iro.js.org/advanced.html#multi-color-selections).null
displayCSS display value for the color picker root element.block
idHTML ID for the color picker root element.null
layoutUsed for customising the [UI component layout](https://iro.js.org/advanced.html#custom-ui-layouts).null
layoutDirectionUI component stacking direction; either vertical or horizontal.vertical
paddingPadding around the control handles.6
marginGap between individual components.12
borderWidthWidth of the border around the controls. Set to 0 for no border.0
borderColorColor of the border. Any valid CSS color is supported.#ffffff
handleRadiusRadius of the control handles.8
handleSvgCustom handle SVG, used for [custom handles](https://iro.js.org/advanced.html#custom-handles).null
handlePropsCustom handle properties, used for [custom handles](https://iro.js.org/advanced.html#custom-handles).{x:0, y:0}
wheelLightnessIf set to false, the color wheel will not fade to black when the lightness decreases.true
wheelAngleStarting angle of the color wheel's hue gradient, measured in degrees.0
wheelDirectionDirection of the color wheel's hue gradient; either clockwise or anticlockwise.anticlockwise
sliderSizeSlider control size. By default this will be calculated automatically.undefined
boxHeightBox control height. By default this will be the same as the width.undefined

More details about color picker options, properties, and methods can be found on the colorPicker API documentation.

Working with Colors

Each color picker has a color object which stores the currently selected color. This color object is tied to the color picker, so any changes to its values will be reflected by the picker, and vice versa.

Color Properties

The color object has some "magic" properties which can be used to both get and set the selected color in different formats. Whenever one of these properties is set, the color picker controls will update and the color:change event will fire.

For example, to get the current color as a hex string:

var hex = colorPicker.color.hexString;
console.log(hex); // hex = "#ff0000"

Or to set the selected color from a hsl object:

colorPicker.color.hsl = { h: 180, s: 100, l: 50 };
// Color picker updates to match hsl(180, 100, 50)

The color object has properties which cover all of the most common web color formats (HEX, RGB, HSL and HSV), as well as some extras:

PropertyExample Format
hexString#ff0000
hex8String#ff0000ff
rgb{ r: 255, g: 0, b: 0 }
rgba{ r: 255, g: 0, b: 0, a: 1 }
rgbStringrgb(255, 0, 0)
rgbaStringrgb(255, 0, 0, 1)
hsl{ h: 360, s: 100, l: 50 }
hsla{ h: 360, s: 100, l: 50, a: 1 }
hslStringhsl(360, 100%, 50%)
hslaStringhsla(360, 100%, 50%, 1)
hsv{ h: 360, s: 100, v: 100 }
hsva{ h: 360, s: 100, v: 100, a: 1 }
red0 to 255
green0 to 255
blue0 to 255
alpha0 to 1
hue0 to 360
saturation0 to 100
value0 to 100
kelvin1000 to 40000

For more details about color objects, check out the Color API documentation.

Color Picker Events

Events let you to run your own code after certain things have happened, like when the selected color has changed or when the user has interacted with the color picker.

The color picker's on method can be used to attach functions that will be called whenever a particular event is fired. In this example, we add a listener for the color:change event:

// listen to a color picker's color:change event
// color:change callbacks receive the current color
colorPicker.on('color:change', function(color) {
    // log the current color as a HEX string
    console.log(color.hexString);
});

The on method can also take an array of event names, in case you want to listen to multiple events with one function:

// listen to a color picker's color:init and color:change events
colorPicker.on(['color:init', 'color:change'], function(color) {
    // log the current color as a HEX string
    console.log(color.hexString);
});

Event listeners can also be removed at any time by passing the same function to the color picker's off method:

// create a callback function
function onColorChange(color) {
    console.log(color.hexString);
}
  
// add color:change listener
colorPicker.on('color:change', onColorChange);
  
// later, if we want to stop listening to color:change...
colorPicker.off('color:change', onColorChange);

Available Events

color:change

Fired whenever the selected color changes -- either when the user interacts with the color picker, or when the color is updated by your own code. This event's callback functions will recieve two values:

  • color: the currently selected color
  • changes: an object showing which HSV channels have changed since the last time the event was fired

It is safe to modify the color object within callbacks for this event. This can be helpful if you want to limit the range or a certain color channel, for example:

colorPicker.on('color:change', function(color) {
    // don't let the color saturation fall below 50!
    if (color.saturation < 50) {
      color.saturation = 50;
    }
});
input:change

Similar to color:change, except this event is only fired when the color is changed with the user's mouse or touch input.

Callbacks for this event recieve the same values as color:change, and it is also safe to modify the color object within callbacks for this event.

input:start

Fired whenever the users starts interacting with the color picker controls. The currently selected color is passed to this event's callback function.

input:move

Fired when the user moves their pointer/mouse after beginning interaction. The currently selected color is passed to this event's callback function.

input:end

Fired whenever the user stops interacting with the color picker controls. The currently selected color is passed to this event's callback function.

color:init

Fired whenever a color is added. This event's callbacks will recieve the newly added color object.

color:remove

Fired when a color is removed from the color picker. This event's callbacks will receive the removed color object.

color:setActive

Fired whenever the 'active' color is switched. This event's callbacks will receive the active color object.

mount

Fired when the colorPicker's UI has been mounted to the DOM and is ready for user interaction. The colorPicker object is passed to this event's callback function.

Special Thanks

Thank you to the following people for their support and contributions!

  • @KaanMol and @mksglu for starting the Typescript port
  • @asonix for vertical slider implementation

Website | Forum | Codepen Demo | Contribution Guide | Donate | Changelog | License

ยฉ James Daniel

More Repositories

1

zfont

๐Ÿ’ฌ Text plugin for Zdog - works with any .ttf font!
JavaScript
178
star
2

playdate-reverse-engineering

Panic Playdate reverse-engineering notes/tools - covers file formats, server API and USB commands
Python
148
star
3

flipnote-player

๐ŸŽฌ Web player and video converter for animations made with Flipnote Studio; an animation app for the Nintendo DSi and 3DS
JavaScript
102
star
4

playnote-studio

An unofficial Flipnote Studio animation player for the Playdate console!
Lua
81
star
5

flipnote.js

JS library for decoding, converting, and in-browser playback of animations created with Nintendo's Flipnote Studio and Flipnote Studio 3D apps
TypeScript
63
star
6

dreams-api

API proxy & network reverse-engineering notes for Media Molecule's "Dreams" game
TypeScript
29
star
7

fakenote-studio

Design files, assets and notes from the Flipnote Switch mockup that was part of our community's 2020 April Fools prank.
24
star
8

game-builder-garage-editor

(WIP) Experimental save editor for Nintendo's Game Builder Garage
TypeScript
22
star
9

pd-usb

JavaScript library for interacting with a Panic Playdate console over USB, wherever WebSerial is supported.
TypeScript
22
star
10

mii-assets

Extract textures and meshes from Nintendo's Mii rendering library
Python
17
star
11

nx-hbmenu-mockup

design mockups and assets (including icons, color swatches, etc) for the nintendo switch homebrew menu
16
star
12

flipnote-video

Command-line tool and Node.js library for converting Flipnote Studio and Flipnote Studio 3D animations to video
JavaScript
10
star
13

flipclip

Web tool for converting Flipnote animations to the Clipnote file format
JavaScript
10
star
14

gb-operator-reverse-engineering

Reverse engineering notes for Epilogue's GB Operator
9
star
15

colors-drw-parser

Parser/renderer for the .drw painting format used by the Colors! art application for 3DS, iOS, Android and PS Vita
TypeScript
9
star
16

pdbug

Debug overlay utility for Lua-based Playdate games
Lua
9
star
17

flipnote-audio-tool

Tool for debugging audio from Flipnote Studio 3D's DSi Library notes
TypeScript
7
star
18

bitmap.scss

.bmp image encoder and a 2D raster drawing API implemented entirely in SCSS/SASS
CSS
6
star
19

kakimasu

Web-based reference app for people learning how to write Japanese
Vue
6
star
20

ugoFormats

JavaScript API for parsing and rendering the proprietary image and menu formats used by Flipnote Hatena
JavaScript
5
star
21

spriteFX

super fast image effects for javascript
JavaScript
5
star
22

labs

Setups and notes from the various animation experiments (and maybe other things) that I make for fun
5
star
23

playupdate

A bot that periodically checks for Playdate firmware updates and notifies a Discord channel when a new one is detected
TypeScript
3
star
24

pvr-model-extractor

Python decompiler for the .pod model format used in some of Nintendo's mobile games
Python
2
star
25

baba-levels

Browse user-created levels from the new Baba Is You level editor update, on the web!
Svelte
2
star
26

framer-easing-gradients

Enhance your Framer X gradients with non-linear color mixing and custom color spaces
TypeScript
1
star
27

jpeg-dot-biz

i realised i own the domain jpeg.biz so i am putting it to good use
HTML
1
star