Git Product home page Git Product logo

parse-css-color's Introduction

parse-css-color

NPM Version License codecov Known Vulnerabilities Libraries.io dependency status for latest release Total alerts Language grade: JavaScript

Parse a CSS color string.

Supports

  • <color value>
    • Hexadecimal RGB value: #RGB #RRGGBB
    • #RGBA #RRGGBBAA (4 and 8-digit hexadecimal RGBA notation)
    • RGB/A color module level 3 and 4 (number, percentage)
    • HSL/A color module level 3 and 4 (number, deg, rad, turn)
  • <color keyword>
  • transparent
    • Shorthand for transparent black, rgba(0,0,0,0).

Does not support

  • currentColor
  • inherit

Installation

NPM

npm i parse-css-color

Or as a <script> tag from a CDN as parseCssColor:

Unpkg CDN

<script src="https://unpkg.com/parse-css-color"></script>

jsDelivr CDN

<script src="https://cdn.jsdelivr.net/npm/parse-css-color"></script>

Usage

import parse from 'parse-css-color'

// HEX/A
parse('#00f')
//> { type: 'rgb', values: [0, 0, 255], alpha: 1 }
parse('#00f8')
//> { type: 'rgb', values: [0, 0, 255], alpha: 0.5333333333333333 }
parse('#0000FF80')
//> { type: 'rgb', values: [0, 0, 255], alpha: 0.5019607843137255 }
parse('#00g')
//> null

// HSL/A
parse('hsl(270deg 60% 70% / 25%)')
//> { type: 'hsl', values: [270, 60, 70], alpha: 0.25 }
parse('hsl(4.71239rad 260% -70% / 0.5)') // clipped to
//> { type: 'hsl', values: [270, 100, 0], alpha: 0.5 }
parse('hsla(.75turn, 60%, 70%, 50%)')
//> { type: 'hsl', values: [270, 60, 70], alpha: 0.5 }
parse('hsla(100deg 0 0 / 0)') // error: missing percetanges
//> null

// RGB/A
parse('rgb(255 0 0 / 0.5)')
//> { type: 'rgb', values: [255, 0, 0], alpha: 0.5 }
parse('rgb(500 -100 0 / 200%)') // clipped to
//> { type: 'rgb', values: [255, 0, 0], alpha: 1 }
parse('rgba(255, 0, 255, 20%)')
//> { type: 'rgb', values: [255, 0, 255], alpha: 0.2 }
parse('rgba(100% 255 100% / 0)') // error: mixed percetange with integer
//> null

See tests for more cases.

Dev

npm install   # install dependencies
npm test      # run the tests (append `-- -w`) to watch
npm run dev   # watch for changes and rebuild

Related

  • mix-css-color - Mix two CSS colors together in variable proportion. Opacity is included in the calculations.
  • values.js - Get the tints and shades of a CSS color.

License

MIT ยฉ Noel Delgado

parse-css-color's People

Contributors

dependabot[bot] avatar lgtm-migrator avatar noeldelgado avatar simplylinn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

parse-css-color's Issues

Alpha should not be set if it is not defined

Hello. ๐Ÿ‘‹ For a project, I'm reviewing over a dozen color parsing libraries, all of which have various problems. Yours is one of the closest to correctness, only having this one issue. Nice!

The alpha is incorrectly being set to a default value when it is actually undefined. For example:

parse('#00f')
//> { type: 'rgb', values: [0, 0, 255], alpha: 1 }

While this alpha can be inferred as 1, for tokenization and parsing, alpha is not set, and should not be included in the parsed results. By forcing a default alpha, it removes the ability to know what the actual input was and precisely convert it.

For example, should converting #00f to rgb color mode be rgb(0 0 255) or rgb(0 0 255 / 1)? It's indeterminate from the result of parse. While it is true that they are equivalent in an rgb color model, at a parsing level they are not equivalent. To be a perfect 1:1 conversion in a parsing scenario, the output result must be rgb(0 0 255), but there's no way to infer that from the parsing result.

Expectation:

parse('#00f')
//> { type: 'rgb', values: [0, 0, 255], alpha: undefined }
parse('#00ff')
//> { type: 'rgb', values: [0, 0, 255], alpha: 1 }

Typescript compiler issue

node_modules/parse-css-color/dist/index.d.ts:2:3 - error TS2309: An export assignment cannot be used in a module with other exported elements.

2   export = parseCSSColor
    ~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in node_modules/parse-css-color/dist/index.d.ts:2

Happens at compiling, likely an issue in the index.d.ts file.

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.