1.4.2 • Published 3 years ago

generate-colors v1.4.2

Weekly downloads
28
License
MIT
Repository
github
Last release
3 years ago

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 "unique@example.com" --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 git@github.com:<your_username>/generate-colors
cd generate-colors
npm install
  1. The source code exists in /src directory.

Available Scripts

ScriptDescription
testRun all the tests
lintTo run the linter
coverageTo create the test coverage report
devTo start the development with docs website
releaseTo release a new version

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

Licence

MIT

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.5

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.0

6 years ago