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

@asamuzakjp/css-color

v2.6.7

Published

CSS color - Resolve and convert CSS colors.

Downloads

1,145

Readme

CSS color

build CodeQL npm (scoped)

Resolve and convert CSS colors.

Install

npm i @asamuzakjp/css-color

Usage

import { convert, isColor, resolve } from '@asamuzakjp/css-color';

const resolvedValue =
  resolve('color-mix(in oklab, lch(67.5345 42.5 258.2), color(srgb 0 0.5 0))');
// 'oklab(0.620754 -0.0931934 -0.00374881)'

const convertedValue = covert.colorToHex('lab(46.2775% -47.5621 48.5837)');
// '#008000'

const result = isColor('green');
// true

resolve(color, opt)

resolves CSS color

Parameters

  • color string color value

    • system colors are not supported
  • opt object? options (optional, default {})

    • opt.currentColor string?
      • color to use for currentcolor keyword
      • if omitted, it will be treated as a missing color, i.e. rgb(none none none / none)
    • opt.customProperty object?
      • custom properties
      • pair of -- prefixed property name as a key and it's value, e.g.
        const opt = {
          customProperty: {
            '--some-color': '#008000',
            '--some-length': '16px'
          }
        };
      • and/or callback function to get the value of the custom property, e.g.
        const node = document.getElementById('foo');
        const opt = {
          customProperty: {
            callback: node.style.getPropertyValue
          }
        };
    • opt.dimension object?
      • dimension, e.g. for converting relative length to pixels
      • pair of unit as a key and number in pixels as it's value, e.g. suppose 1em === 12px, 1rem === 16px and 100vw === 1024px, then
        const opt = {
          dimension: {
            em: 12,
            rem: 16,
            vw: 10.24
          }
        };
      • and/or callback function to get the value as a number in pixels, e.g.
        const opt = {
          dimension: {
            callback: (unit) => {
              switch (unit) {
                case 'em':
                  return 12;
                case 'rem':
                  return 16;
                case 'vw':
                  return 10.24;
                default:
                  return;
              }
            }
          }
        };
    • opt.format string?
      • output format, one of below
        • computedValue (default), computed value of the color
        • specifiedValue, specified value of the color
        • hex, hex color notation, i.e. rrggbb
        • hexAlpha, hex color notation with alpha channel, i.e. #rrggbbaa
    • opt.key any?
      • key to return with the value, e.g. CSS property background-color

Returns (string? | Array) one of rgba?(), #rrggbb(aa)?, color-name, '(empty-string)', color(color-space r g b / alpha), color(color-space x y z / alpha), lab(l a b / alpha), lch(l c h / alpha), oklab(l a b / alpha), oklch(l c h / alpha), null, or [key, rgba?()] etc. if key is specified

  • in computedValue, values are numbers, however rgb() values are integers
  • in specifiedValue, returns empty string for unknown and/or invalid color
  • in rgb, values are rounded to integers, and returns rgba(0, 0, 0, 0) for unknown and/or invalid color
  • in hex, returns null for transparent, and also returns null if any of r, g, b, alpha is not a number
  • in hexAlpha, returns #00000000 for transparent, however returns null if any of r, g, b, alpha is not a number

convert

Contains various color conversion functions.

convert.numberToHex(value)

convert number to hex string

Parameters

Returns string hex string: 00..ff

convert.colorToHex(value, opt)

convert color to hex

Parameters

  • value string color value
  • opt object? options (optional, default {})
    • opt.alpha boolean? return in #rrggbbaa notation
    • opt.customProperty object?
      • custom properties, see resolve() function above
    • opt.dimension object?
      • dimension, see resolve() function above

Returns string #rrggbb(aa)?

convert.colorToHsl(value, opt)

convert color to hsl

Parameters

  • value string color value
  • opt object? options (optional, default {})
    • opt.customProperty object?
      • custom properties, see resolve() function above
    • opt.dimension object?
      • dimension, see resolve() function above

Returns Array<number> [h, s, l, alpha]

convert.colorToHwb(value, opt)

convert color to hwb

Parameters

  • value string color value
  • opt object? options (optional, default {})
    • opt.customProperty object?
      • custom properties, see resolve() function above
    • opt.dimension object?
      • dimension, see resolve() function above

Returns Array<number> [h, w, b, alpha]

convert.colorToLab(value, opt)

convert color to lab

Parameters

  • value string color value
  • opt object? options (optional, default {})
    • opt.customProperty object?
      • custom properties, see resolve() function above
    • opt.dimension object?
      • dimension, see resolve() function above

Returns Array<number> [l, a, b, alpha]

convert.colorToLch(value, opt)

convert color to lch

Parameters

  • value string color value
  • opt object? options (optional, default {})
    • opt.customProperty object?
      • custom properties, see resolve() function above
    • opt.dimension object?
      • dimension, see resolve() function above

Returns Array<number> [l, c, h, alpha]

convert.colorToOklab(value, opt)

convert color to oklab

Parameters

  • value string color value
  • opt object? options (optional, default {})
    • opt.customProperty object?
      • custom properties, see resolve() function above
    • opt.dimension object?
      • dimension, see resolve() function above

Returns Array<number> [l, a, b, alpha]

convert.colorToOklch(value, opt)

convert color to oklch

Parameters

  • value string color value
  • opt object? options (optional, default {})
    • opt.customProperty object?
      • custom properties, see resolve() function above
    • opt.dimension object?
      • dimension, see resolve() function above

Returns Array<number> [l, c, h, alpha]

convert.colorToRgb(value, opt)

convert color to rgb

Parameters

  • value string color value
  • opt object? options (optional, default {})
    • opt.customProperty object?
      • custom properties, see resolve() function above
    • opt.dimension object?
      • dimension, see resolve() function above

Returns Array<number> [r, g, b, alpha]

convert.colorToXyz(value, opt)

convert color to xyz

Parameters

  • value string color value
  • opt object? options (optional, default {})
    • opt.customProperty object?
      • custom properties, see resolve() function above
    • opt.dimension object?
      • dimension, see resolve() function above
    • opt.d50 boolean? xyz in d50 white point

Returns Array<number> [x, y, z, alpha]

convert.colorToXyzD50(value, opt)

convert color to xyz-d50

Parameters

  • value string color value
  • opt object? options (optional, default {})
    • opt.customProperty object?
      • custom properties, see resolve() function above
    • opt.dimension object?
      • dimension, see resolve() function above

Returns Array<number> [x, y, z, alpha]

isColor(color)

is valid color type

Parameters

  • color string color value
    • system colors are not supported

Returns boolean


Copyright (c) 2024 asamuzaK (Kazz)