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

generate-colors

v1.4.2

Published

Get colors for a string with accessibility compliant

Downloads

284

Readme

Get colors for strings

Use case

To differentiate between UI elements of different categories, we can use colors. For example, a unique color per event type can be used in a Calendar UI to differentiate between different type of events. Sometimes, we may need to show a unique background color for user's avatar when we don't have a profile picture.

Install

npm i -S generate-colors

Usage

Usage are pretty simple. Just import the getColorForString method from the module and call it with a string that uniquely identifies an object (User Account, Event Type). The method returns an array with [R, G, B] values.

import { getColorForString } from "generate-colors"

const color = getColorForString("First Name and Last Name")
// color = [90, 39, 50]

Passing the same string again will result in the same color.

Available Method

/**
 * Generate the color a string
 * @param {string} str String for which we want to get the colors
 * @param {{ brightness?: number | ((defaultBrightness: number) => number), saturation?: number | ((defaultSaturation: number) => number) }} [config={ contrast: 30, saturation: undefined }] - Configuration for the color
 * @returns {Array<number>} Array with R,G,B values in sequence
 */
function getColorForString(str, config): [number, number, number]

When you pass custom values for configuration (brightness/saturation), you will be responsible for accessibility. You will receive the default values for each option when passing a function to these configuration values.

Here are some examples to set the configuration values using the default values.

getColorForString("string", {
  brightness: (defaultBrightness) => {
    // let's keep the brightness between 20 to 60
    if (defaultBrightness <= 20) return 20
    if (defaultBrightness >= 60) return 60
    return defaultBrightness
  },
  saturation: (defaultSaturation) => {
    // let's keep the saturation between 50 to 90
    if (defaultSaturation <= 50) return 50
    if (defaultSaturation >= 90) return 90
    return defaultSaturation
  },
})

Color Caching

If you are generating thousands of colors, calls to getColorForString may slow down your processing. If you have fixed numbers of strings, you should use caching.

import { makeGetColorForOptions } from "generate-colors"

// now you can use this
const getColorForString = makeGetColorForOptions({ brightness: 50 })

// Now this function cache generated colors for each string
// NOTE: It will NOT accept configuration options
generateColors("string")

Visit Playground to see in section.

CLI

This module also exposes a CLI tool to generate colors from command line itself.

npx generate-colors "[email protected]" --brightness=35 --saturation=70

Contribution

Any kind of contribution is most welcome!!

Setup

  1. Fork the repository to your account.
  2. Clone the repository and install dependencies.
git clone [email protected]:<your_username>/generate-colors
cd generate-colors
npm install
  1. The source code exists in /src directory.

Available Scripts

| Script | Description | | :------- | :----------------------------------------- | | test | Run all the tests | | lint | To run the linter | | coverage | To create the test coverage report | | dev | To start the development with docs website | | release | To release a new version |

Any script can be executed by calling npm run <script_name>.

Licence

MIT