sc-picker v1.0.3
sc-picker
A simple, lightweight color picker.
features
- Small file size.
- Touch friendly.
- Support dark theme.
- Alpha channel (opacity).
- Support 3 color formats hsl, rgb and hex.
- Keyboard accessible.
- Simple interface (inspired by google chrome's color picker).
- No dependencies.
Demo
Getting started
Install using package manager:
npm install sc-picker
or
yarn add sc-picker
import files
// Import javascript.
import picker from 'sc-picker';
// Import css.
import 'sc-picker/dist/css/picker.min.css';
CDN
Add it to you page.
- Jsdelivr CDN
<!-- Style -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sc-picker/dist/css/picker.min.css">
<!-- Script -->
<script src="https://cdn.jsdelivr.net/npm/sc-picker/dist/js/picker.min.js"></script>
- Unpkg CDN
<!-- Style -->
<link rel="stylesheet" href="https://unpkg.com/sc-picker/dist/css/picker.min.css">
<!-- Script -->
<script src="https://unpkg.com/sc-picker/dist/js/picker.min.js"></script>
Usage
createPicker can take two arguments, a reference element either a selector or an HTML element (required), and options object (optional).
const picker = createPicker('#reference', {
// Options...
});
Options
You can try these options in the demo, play around with it :)
Option | Type | Default | Description |
---|---|---|---|
id | string | '' | Set the container's (widget) id. |
classname | string | '' | One or many classes (separated by a white space) to add it to the preset button. |
theme | string | light | Choose a theme, 'dark' or 'light'. |
toggle | boolean | true | Toggle picker's visibility (Show/Hide), Setting this to false keeps the picker visible. |
popover | boolean | true | Set the position of the popper (if popover is set to true) relative to the reference element, if it's false, picker container will be displayed as a block (embeded in the page's content). |
position | string | bottom-start | Set the position of the popper (if popover is set to true) relative to the reference element.The position has two values seperated by a dash (-)the first value is the direction (top, bottom, right, left) and the second value is the alignment (start, center, end), omitting this value will default to center.E.g. 'bottom-start': 'bottom' places the picker below the reference element, and 'start' aligns the left side of the container with the left side of the reference element.Note: If the picker container has no space to be placed, it will auto-position itself based on the available space. |
margin | number | 8 | Set the gap (in pixels) between the picker container and the reference element. |
preset | boolean | true | Replace the reference element with a pre-styled button. |
color | string | #000 | Initial color. |
default | string | #000 | Default color. |
target | string or HTMLElement | '' | Target can be a selector or an HTML element, If the option popover is set to true, the picker container will be positionned retalive to this element,else if popover option is set to false, the picker container will be appended as a child into this element. |
disabled | boolean | false | Disable the picker, users won't be able to pick colors. |
format | string | rgb | Initial color format. |
singleInput | boolean | false | For the formats 'hsl' and 'rgb', choose a single input to display the color string,if false, display an input for each color channel. |
inputs | array<string> | ['hsl', 'rgb', 'hex'] | Choose color formats for the picker input, 'hsl', 'rgb' or 'hex',No input will be displayed if the array is empty. |
opacity | boolean | true | Support alpha channel and display opacity slider. |
preview | boolean | true | Preview the color. |
copyButton | boolean | true | Add/Remove a copy button. |
swatches | array<string> | [] | Array of color strings, invalid color strings will default to rgb(0,0,0). |
Note: In case you set the preset
to false
(using your own reference element), to access the color to change its background or any other property, add the css custom property to your styles --sc-color
.
E.g.
#my-reference-element {
background-color: var(--sc-color);
}
Events
Use the method on
, that takes two parameters, event
and handler
(callback function).
picker.on('event', function(argument) {
// ...
});
Event | Argument | Description |
---|---|---|
open | pickerData | Fires when the picker get opened |
close | pickerData | Fires when the picker get closed |
change | colorObject | Fires when an alternation to the color is committed by the user, similar to the DOM change event |
color | colorObject | Similar to the input event, fires every time the color changes |
ColorObject
Color object is the output of the color picker, its properties are:
- h:
number
- Color Hue. - s:
number
- Saturation in the HSV format. - v:
number
- Value in the HSV format. - a:
number
- Alpha (opacity) only ifOptions.opacity
is set to true.
and its methods
toRGB(asArray:
boolean
)Gets an RGB color object or Array if
asArray
is set to true, call the methodtoString()
on this method to get an RGB string.toHSL(asArray:
boolean
)Gets an HSL color object or Array if
asArray
is set to true, call the methodtoString()
on this method to get an HSL string.toHEX()
Gets a hex color.
// e.g.
picker.on('change', function(color) {
// RGB color.
color.toRGB() // output: { r: 0, g: 0, b: 0, a: 1}
color.toRGB(true) // output: [0, 0, 0, 0]
color.toRGB().toString() // output: rgba(0, 0, 0, 1)
// Hex color.
color.toHEX() // output: #000000
// HSL color.
color.toHSL() // output: { h: 0, s: 0, l: 0, a: 1 }
color.toHSL(true) // output: [0, 0, 0, 0]
color.toHSL().toString() // output: hsla(0, 0%, 0%, 1)
})
Methods
setColor(color:
string | object
) :object
Sets a color from a string or a color object, this method doesn't trigger
change
orcolor
events if you want to trigger events add.change()
or.color()
to it// Set color 'purple' and trigger 'change' event. picker.setColor('purple').change()
getColor() :
object
Returns the color object.
open()
Open/Show picker.
close()
Close/Hide picker.
isOpen() :
boolean
Returns the state of the picker opened
true
or closedfalse
.toggle()
Toggle picker, if its hidden show it else hide it.
setOptions(options:
object
)Sets one or more options for the picker.
getOptions():
object
Gets picker options.
trigger(event:
string
)Triggers or fires an event.
on(event:
string
, handler:callback
)Attaches an event handler function.
off(event:
string
, handler:callback
)Removes an event handler, if the handler argument is omitted then all handlers attach to this event will be removed, calling this method without arguments will remove all handlers of all events.
disable()
Disable picker (users won't be able to pick a color).
enable()
Enable picker.
updatePosition()
Updates picker position only if it's a popover.
addSwatch(color:
string
)Adds the color to the
Options.swatches
array, also created and append a swatch button to the swatches container.removeSwatch(swatch:
string | number
)Removes color from the
Options.swatches
array, also removes its button from the swatches container.Note: swatch argument can be a color string (must exist in
Options.swatches
array) or an index.reset()
Reset default color.
destroy()
Removes any HTML elements created by this instance and destroy all functionality (free up memory).