@ricokahler/parse-to-rgba v0.3.2
Parse to RGBA
Converts any color to RGBA using canvas.
Installation
npm install --save @ricokahler/parse-to-rgba
Why canvas?
The browser already has the ability to parse colors via canvas. Other color libs use javascript to parse and transform colors to RGBA.
The result? Significancy smaller bundle sizes:
polished/parseToRgb
: 10.7kB@ricokahler/parse-to-rgba
: 439B
More than 20x smaller!
Why not canvas?
Booting up canvas is slower than parsing via javascript.
On my 2019 16" MacBook Pro:
- Canvas: 58,588 ops/sec
- JavaScript: 3,335,123 ops/sec
Note: the canvas ops/sec metric does not include the creation of the canvas context.
However, the numbers above aren't the full story. The javascript implementation I'm referencing (polished
) has a ~10kB bundle size. That extra codes takes significantly more time to parse and download.
Node vs Browser environments
This lib will work in both browser and node environments. In a node environment it will use the package canvas
instead of the browser's native canvas.
Note: For node/SSR environments, you will need to install the package
canvas