npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

palettron

v1.0.0

Published

Manipulate palettes of colors in wonderful ways

Downloads

10

Readme

Manipulate palettes of colors in wonderful ways.

Immutable. Particularly useful for working with chart colors.

Uses Colord for color value manipulation under the hood.

Installation

npm install palettron

Usage

import { palettron } from "palettron"

// Values can be a css color string or Colord object.
const palette = palettron([
    "#ff0000",
    "#00ff00",
    "#0000ff",
    "#ffff00",
    "#ff00ff",
    "#00ffff",
])

/**
 * - Swap the order of the first two colors.
 * - Shift the palette one index to the right, wrapping the last to the first.
 * - Darken the colors.
 * - Desaturate a bit.
 * - Set the alpha.
 * - Pick every other color.
 * - Return an array of rgb string values.
 */
const colors = palette
    .swap(0, 1)
    .shift(1)
    .darken(0.1)
    .desaturate(0.2)
    .alpha(0.75)
    .pick([0, 2, 4])
    .toRgb()
/* [
  'rgba(20, 184, 184, 0.75)',
  'rgba(184, 20, 20, 0.75)',
  'rgba(184, 184, 20, 0.75)',
] */

API

/**
 * The number of colors in the palette.
 */
get size(): number;

/**
 * Set the alpha of a palette.
 */
alpha: (a: number) => Palettron;

/**
 * Darken the colors of a palette.
 */
darken: (x: number) => Palettron;

/**
 * Desaturate the colors of a palette.
 */
desaturate: (x: number) => Palettron;

/**
 * Convert a palette to grayscale.
 */
grayscale: () => Palettron;

/**
 * Invert the colors of a palette.
 */
invert: () => Palettron;

/**
 * Lighten the colors of a palette.
 */
lighten: (x: number) => Palettron;

/**
 * Rotate the hue of a palette.
 */
rotate: (x: number) => Palettron;

/**
 * Saturate the colors of a palette.
 */
saturate: (x: number) => Palettron;

/**
 * Add one or more colors to the palette.
 */
add: (...colors: readonly (Colord | string)[]) => Palettron;

/**
 * Concatenate one or more palettes with this one.
 */
concat: (...palettes: readonly (Palettron | readonly string[])[]) => Palettron;

/**
 * Iterate each Colord object in the palette.
 */
each: (cb: (color: Colord, index: number, array: readonly Colord[]) => void) => void;

/**
 * Filter colors using the underlying Colord object.
 */
filter: (cb: (color: Colord, index: number, array: readonly Colord[]) => boolean) => Palettron;

/**
 * Map colors using the underlying Colord object.
 */
map: (cb: (color: Colord, index: number, array: readonly Colord[]) => Colord | string) => Palettron;

/**
 * Merge one or more palettes with this one, removing duplicates.
 */
merge: (...palettes: readonly (Palettron | readonly string[])[]) => Palettron;

/**
 * Modify a specific color in a palette as a Colord object.
 */
modify: (indices: number | readonly number[], cb: (c: Colord, index: number) => Colord) => Palettron;

/**
 * Reduce a palette to the specified indicies.
 */
pick: (indices: number | readonly number[]) => Palettron;

/**
 * Replace one color in a palette with another given color.
 */
replace: (index: number, color: string | Colord) => Palettron;

/**
 * Reverse the order of items in a palette.
 */
reverse: () => Palettron;

/**
 * Randomly shuffle the palette based on a random seed.
 */
shuffle: (seed?: number | string) => Palettron;

/**
 * Shift the palette by x indices left or right, with wrapping.
 */
shift: (x: number) => Palettron;

/**
 * Slice a palette like an array.
 */
slice: (a?: number, b?: number) => Palettron;

/**
 * Sort a palette based on its underlying Colord object.
 */
sort: (cb: (a: Colord, b: Colord) => number) => Palettron;

/**
 * Swap two items in a palette by index.
 */
swap: (a: number, b: number) => Palettron;

/**
 * Return the palette as an array of css hex string values.
 */
toHex: () => string[];

/**
 * Return the palette as an array of css rgb/rgba string values.
 */
toRgb: () => string[];

/**
 * Return the palette as an array of css hsl string values.
 */
toHsl: () => string[];

/**
 * Convert colors in the palette to an array of arbitrary values.
 */
toAny: <T>(cb: (color: Colord, index: number, array: readonly Colord[]) => T) => T[];