Code Monkey home page Code Monkey logo

csscolor's Introduction

CSS color

build CodeQL npm (scoped)

Resolve, parse, convert CSS color.

Install

npm i @asamuzakjp/css-color

Usage

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

const rgb = resolve('color-mix(in srgb, red, blue)');
// 'rgb(128, 0, 128)'

const xyz = parse('lab(46.2775% -47.5621 48.5837)');
const hex = convert.xyzToHex(xyz);
// '#008000'

resolve(color, opt)

Resolve CSS color.

Parameters

  • color string color value
  • opt object? options
    • opt.currentColor string? color to use for currentcolor keyword
    • opt.format string? output format, one of rgb(default), array, hex or hexAlpha
      • hexAlpha gets hex color with alpha channel, i.e. #rrggbbaa
    • opt.key any? key e.g. CSS property background-color

Returns (string? | Array) rgba?(), [r, g, b, a], #rrggbb(aa)?, null, or if key is specified, [key, rgba?()|[r, g, b, a]|#rrggbb(aa)?|null]

  • In rgb:
    • r, g, b values are rounded.
    • Returns empty string for unknown / unsupported color name.
  • In array:
    • Values are floats.
    • Returns array filled with undefined for unknown / unsupported color name, i.e. [undefined, undefined, undefined, undefined].
  • In hex:
    • transparent returns null.
    • Also returns null for unknown / unsupported color name.
    • currentcolor resolves as #000000 if opt.currentColor is not specified.
  • In hexAlpha:
    • transparent resolves as #00000000.
    • currentcolor resolves as #00000000 if opt.currentColor is not specified.
    • Returns null for unknown / unsupported color name.

parse(color, opt)

Parse CSS color.

Parameters

Returns Array<number> [x, y, z, a] x|y|z|a: 0..1

convert

Contains various conversion functions as properties.

convert.numberToHex(n)

Convert number to hex string.

Parameters

Returns string hex string 00..ff

convert.hexToRgb(hex)

Convert hex color to rgb color array.

Parameters

Returns Array<number> [r, g, b, a] r|g|b: 0..255 a: 0..1

convert.rgbToHex(rgb)

Convert rgb to hex color notation.

Parameters

Returns string #rrggbbaa

convert.xyzToHex(xyz)

Convert xyz to hex color notation.

Parameters

Returns string #rrggbbaa

convert.xyzToHsl(xyz)

Convert xyz to hsl color array.

Parameters

Returns Array<number> [h, s, l, a] h: 0..360 s|l: 0..100 a: 0..1

convert.xyzToHwb(xyz)

Convert xyz to hwb color array.

Parameters

Returns Array<number> [h, w, b, a] h: 0..360 w|b: 0..100 a: 0..1

convert.xyzToOklab(xyz)

Convert xyz to oklab color array.

Parameters

Returns Array<number> [l, a, b, A] l|A: 0..1 a|b: -0.4..0.4

convert.xyzToOklch(xyz)

Convert xyz to oklch color array.

Parameters

Returns Array<number> [l, c, h, a] l|a: 0..1 c: 0..0.4 h: 0..360

convert.xyzToRgb(xyz)

Convert xyz to rgb color array.

Parameters

Returns Array<number> [r, g, b, a] r|g|b: 0..255 a: 0..1

convert.xyzToXyzD50(xyz)

Convert xyz to xyz-d50 color array.

Parameters

Returns Array<number> xyz - [x, y, z, a] x|y|z|a: 0..1

convert.xyzD50ToLab(xyz)

Convert xyz-d50 to lab color array.

Parameters

Returns Array<number> [l, a, b, A] l: 0..100 a|b: -125..125 A: 0..1

convert.xyzD50ToLch(xyz)

Convert xyz-d50 to lch color array.

Parameters

Returns Array<number> [l, c, h, a] l: 0..100 c: 0..150 h: 0..360 a: 0..1


Copyright (c) 2024 asamuzaK (Kazz)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.