simonwep / pickr

🎨 Flat, simple, multi-themed, responsive and hackable Color-Picker library. No dependencies, no jQuery. Compatible with all CSS Frameworks e.g. Bootstrap, Materialize. Supports alpha channel, rgba, hsla, hsva and more!
https://simonwep.github.io/pickr
MIT License
4.31k stars 285 forks source link

Addition: getSwatches() and setSwatches() #241

Closed GreenFootballs closed 4 years ago

GreenFootballs commented 4 years ago

(This isn't really a feature request but it seemed like the best place to put it.)

In an app I'm developing we needed a way to easily save and load sets of color swatches, and since other people may also find this useful, here's the code I implemented to add these functions to Pickr.

Pickr.prototype.getSwatches = function() {
    return this._swatchColors.reduce( (arr, swatch) => {
        arr.push(swatch.color.toRGBA().toString(0))
        return arr
    }, [] )
}
Pickr.prototype.setSwatches = function(swatches) {
    if (!swatches.length) return
    for (let i = this._swatchColors.length - 1; i > -1; i--) {
        this.removeSwatch(i)
    }
    swatches.forEach( swatch => this.addSwatch(swatch) )
}

You pass an array of CSS colors to setSwatches(), same format as the swatches array in Pickr's create options. getSwatches() returns an array in the same format.

simonwep commented 4 years ago

Great addition :) But as stated in the readme features won't be added anymore, maybe we could move that to EXAMPLES.md?

GreenFootballs commented 4 years ago

Sure, that's OK by me.