Simonwep / pickr
- вторник, 12 июня 2018 г. в 00:14:18
JavaScript
Flat, simple and hackable Color-Picker. No dependencies, no jQuery, compatible with all CSS Frameworks e.g. Bootstrap, Materialize.
Via npm
$ npm install pickr-widget --save
Link styles and add scripts
<link rel="stylesheet" href="node_modules/pickr/dist/pickr.min.css"/>
<script src="node_modules/pickr/dist/pickr.min.js"></script>
const pickr = Pickr.create({
el: '.color-picker'
});
const pickr = new Pickr({
// Selector or element which will be replaced with the actual color-picker
el: '.color-picker',
// Default color
default: 'fff',
// Defines the position of the color-picker. Available options are
// top, left and middle relativ to the picker button.
position: 'middle',
// Show or hide specific components.
// By default only the palette (and the save button) is visible.
components: {
preview: true, // Left side color comparison
opacity: true, // Opacity slider
hue: true, // Hue slider
// Bottom output bar, theoretically you could use 'true' as propery.
// But this would also hide the save-button.
output: {
hex: true, // hex option (hexadecimal representation of the rgb value)
rgba: true, // rgba option (red green blue and alpha)
hsla: true, // hsla option (hue saturation lightness and alpha)
hsva: true, // hsva option (hue saturation value and alpha)
cmyk: true, // cmyk option (cyan mangenta yellow key )
input: true // input / output element
},
},
// User has changed the color
onChange(hsva, instance) {
hsva; // HSVa color object
instance; // Current Pickr instance
},
// User has clicked the save button
onSave(hsva, instance) {
// same as onChange
}
});
As default color representation is hsva (hue
, saturation
, value
and alpha
) used, but you can also convert it to other formats as listed below.
The toString()
is overriden so you can get a color representaion string.
hsva.toRGBA(); // Returns [r, g, b, a]
hsva.toRGBA().toString(); // Returns rgba(r, g, b, a)
:Number
,s:Number
,v:Number
,a:Float
) - Set an color.:String
) - Parses a string which represents a color:HTMLElement
- Returns the root DOM-Element of the color-picker.:HSVaColor
- Returns the current HSVaColor object.Pickr
:Object
):Pickr
- Creates a new instance.Pickr.utils
:HTMLElement
, event:String
, fn:Function
[, options :Object
]) - Attach an event handler function.:HTMLElement
, event:String
, fn:Function
[, options :Object
]) - Remove an event handler.:String
):HTMLElement
- Creates an new HTML Element out of this string.:Event
):[HTMLElement]
- A polyfill for the event-path event propery.