Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/basementuniverse/parsecolor
A function for parsing colors and turning them into RGBA values.
https://github.com/basementuniverse/parsecolor
Last synced: 23 days ago
JSON representation
A function for parsing colors and turning them into RGBA values.
- Host: GitHub
- URL: https://github.com/basementuniverse/parsecolor
- Owner: basementuniverse
- License: mit
- Created: 2017-01-09T03:31:04.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-29T20:56:37.000Z (about 2 years ago)
- Last Synced: 2024-04-27T02:40:30.100Z (9 months ago)
- Language: JavaScript
- Size: 45.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# parseColor
A small (~5kb) function for parsing various valid CSS color formats and turning them into RGBA values.
The function will return an object that looks like this:
```
{ r: 255, g: 0, b: 0, a: 1 }
```The returned object's properties will always have the correct range (ie. 0-255) and precision (ie. r/g/b will be integers, alpha value will have 0-2 decimal places).
Some examples of valid inputs:
* red
* #f00
* #ff0000
* #ff0000ff
* rgb(255, 0, 0)
* rgb(100%, 0%, 0%)
* rgba(255, 0, 0, 1)
* rgba(100%, 0%, 0%, 1)
* hsl(0, 100%, 50%)
* hsla(0, 100%, 50%, 1)If the input cannot be parsed, the returned color will be transparent (ie.
{ r: 0, g: 0, b: 0, a: 0 }
).## How to use
Include the script in your page, then call the function:
```
var myColour = parseColor("red");
console.log(myColour); // { r: 255, g: 0, b: 0, a: 1 }
```