parseColor() NEW
Parses a CSS color string and returns its red, green, blue and alpha channels as numbers.
const [ r, g, b, a ] = utils.parseColor(colorString);
The formats accepted are the ones of the color value type: hex, rgb(), hsl(), oklab(), oklch() and color(). Colors outside the sRGB gamut are clipped. Other strings, like 'red' or 'lab(50% 40 59)', return [0, 0, 0, 1].
Parameters
| Name | Accepts |
|---|---|
| colorString | String |
Returns
An Array of four Number: red, green and blue from 0 to 255, and alpha from 0 to 1
utils.parseColor('#ff0000'); // [255, 0, 0, 1]
utils.parseColor('oklch(70% 0.2 30)'); // [255, 97, 77, 1]
utils.parseColor('rgb(0 128 255 / 50%)'); // [0, 128, 255, 0.5] parseColor() code example
import { utils } from 'animejs';
const [ $hex, $hsl, $oklch ] = utils.$('.label');
const [ $sq1, $sq2, $sq3 ] = utils.$('.square');
// Each square is colored with a different syntax
utils.set($sq1, { backgroundColor: '#FF4B4B' });
utils.set($sq2, { backgroundColor: 'hsl(44, 100%, 59%)' });
utils.set($sq3, { backgroundColor: 'oklch(70% 0.2 30 / .5)' });
// parseColor() reads the channels of every syntax
$hex.textContent = utils.parseColor(utils.get($sq1, 'backgroundColor')).join(', ');
$hsl.textContent = utils.parseColor(utils.get($sq2, 'backgroundColor')).join(', ');
$oklch.textContent = utils.parseColor(utils.get($sq3, 'backgroundColor')).join(', ');
<div class="medium row">
<div class="square"></div>
<div class="padded label"></div>
</div>
<div class="medium row">
<div class="square"></div>
<div class="padded label"></div>
</div>
<div class="medium row">
<div class="square"></div>
<div class="padded label"></div>
</div>