Socket
Socket
Sign inDemoInstall

parse-css-color

Package Overview
Dependencies
2
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    parse-css-color

parse a css color string


Version published
Weekly downloads
164K
decreased by-8.3%
Maintainers
1
Install size
39.4 kB
Created
Weekly downloads
 

Changelog

Source

[0.2.1] - #16 - 2022-04-07

Fixed

  • index.d.ts #15

Added

  • README: badges(librariesio, lgtm) f36810e

Readme

Source

parse-css-color

NPM Version License 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
  • 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

Keywords

FAQs

Last updated on 07 Apr 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc