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

color-name-to-code

v1.0.1

Published

A javascript library that returns a color code for a given color name.

Downloads

23

Readme

Color Name to Code

npm Package Version License MIT GitHub Repo Native Typescript Support Coveralls Coverage GitHub Workflow Status

A javascript library that returns a color code for a given color name.

It supports all color names from the list of named CSS colors from W3C.

It includes also the enum ColorName with a full list of all known color names.

The library can return hex color values, CSS rgb(…) values or an array of numeric RGB color values.


Installation

This library is published to npm registry as color-name-to-code.

You can install it:

# with npm
npm install --save color-name-to-code

# with yarn
yarn add color-name-to-code

ℹ️ HINT: This library is a pure ESM package. (You may want to read this.)

Usage

import { colorNameToCode } from 'color-name-to-code';

// color name to hex value
colorNameToCode('red'); // -> '#FF0000'

// color name to lowercase hex value
colorNameToCode('red', { lowercase: true }); // -> '#ff0000

// color name to short hex value
colorNameToCode('red', { short: true }); // -> '#f00'

// color name to hex value without leading hash
colorNameToCode('red', { hash: false }); // -> 'FF0000'

// color name to CSS `rgb(…)` value
colorNameToCode('red', { format: 'rgb' }); // -> 'rgb(255, 0, 0)'

// color name to CSS `rgba(…)` value
colorNameToCode('red', { format: 'rgb', alpha: 0.5 }); // -> 'rgba(255, 0, 0, 0.5)'

// color name to numeric RGB array
colorNameToCode('red', { format: 'array' }); // -> [255, 0, 0]

// color names will be sanitized before matching
colorNameToCode('"white!"'); // -> '#FFFFFF'
colorNameToCode('Black'); // -> '#000000'
colorNameToCode('RED'); // -> '#FF0000'
colorNameToCode('Dodger Blue'); // -> '#1E90FF'
colorNameToCode('dark-slate-gray'); // -> '#2F4F4F'
colorNameToCode('DARK_SLATE_GREY'); // -> '#2F4F4F'
colorNameToCode('LeMoN cHiFfOn'); // -> '#FFFACD'

// an unknown color name will be transformed to a fallback color
colorNameToCode('Foo Bar'); // -> '#FFBBAA'
colorNameToCode(undefined); // -> '#DEFE0D'
colorNameToCode('UNKNOWN'); // -> '#000000'

// if fallback is disabled and color name is unknown, an error is thrown
colorNameToCode('Foo Bar', { fallback: false }); // -> ERROR: no matching color found for 'Foo Bar'

API

colorNameToCode(name: string, options: Partial<Options>): string | [number, number, number];

Options

interface Options {
  format: 'hex' | 'rgb' | 'array'; // default: 'hex'
  fallback: boolean; // default: true
  short: boolean; // default: false
  hash: boolean; // default: true
  lowercase: boolean; // default: false
  alpha: number; // default: 1
}
  • format: 'hex' | 'rgb' | 'array' (default: 'hex')
    …defines the output format.

    Possible values:

    • 'hex' defines a hex value as output format
    • 'rgb' defines a CSS rgb(…) value as output format
    • 'array' defines a numeric array of RGB values as output format
  • fallback: boolean (default: true)
    …tries to generate a color value from the given name input if a matching color name could not be found.

    To achieve this, all non-hex characters are removed from the given string and up to the first six left characters are interpreted as hex color value.

    If fallback is false and no matching color name is found, colorNameToCode will throw an error.

  • short: boolean (default: false)
    …returns a hex value as short version if possible and set to true.

    (This option only takes effect in combination with format: 'hex'.)

  • hash: boolean (default: true)
    …returns a hex value with or without leading hash.

    (This option only takes effect in combination with format: 'hex'.)

  • lowercase: boolean (default: false)
    …return a hex value with lowercase letters if set to true.

    (This option only takes effect in combination with format: 'hex'.)

  • alpha: number (default: 1)
    …returns a CSS rgba(…) value with alpha value, if alpha is lower than 1.

    (This option only takes effect in combination with format: 'rgb'.)

License

MIT © Simon Lepel