animejs
Version:
JavaScript animation engine
103 lines (93 loc) • 2.75 kB
JavaScript
/**
* Anime.js - core - CJS
* @version v4.3.6
* @license MIT
* @copyright 2026 - Julian Garnier
*/
;
var consts = require('./consts.cjs');
var helpers = require('./helpers.cjs');
/**
* @import {
* ColorArray,
* } from '../types/index.js'
*/
/**
* RGB / RGBA Color value string -> RGBA values array
* @param {String} rgbValue
* @return {ColorArray}
*/
const rgbToRgba = rgbValue => {
const rgba = consts.rgbExecRgx.exec(rgbValue) || consts.rgbaExecRgx.exec(rgbValue);
const a = !helpers.isUnd(rgba[4]) ? +rgba[4] : 1;
return [
+rgba[1],
+rgba[2],
+rgba[3],
a
]
};
/**
* HEX3 / HEX3A / HEX6 / HEX6A Color value string -> RGBA values array
* @param {String} hexValue
* @return {ColorArray}
*/
const hexToRgba = hexValue => {
const hexLength = hexValue.length;
const isShort = hexLength === 4 || hexLength === 5;
return [
+('0x' + hexValue[1] + hexValue[isShort ? 1 : 2]),
+('0x' + hexValue[isShort ? 2 : 3] + hexValue[isShort ? 2 : 4]),
+('0x' + hexValue[isShort ? 3 : 5] + hexValue[isShort ? 3 : 6]),
((hexLength === 5 || hexLength === 9) ? +(+('0x' + hexValue[isShort ? 4 : 7] + hexValue[isShort ? 4 : 8]) / 255).toFixed(3) : 1)
]
};
/**
* @param {Number} p
* @param {Number} q
* @param {Number} t
* @return {Number}
*/
const hue2rgb = (p, q, t) => {
if (t < 0) t += 1;
if (t > 1) t -= 1;
return t < 1 / 6 ? p + (q - p) * 6 * t :
t < 1 / 2 ? q :
t < 2 / 3 ? p + (q - p) * (2 / 3 - t) * 6 :
p;
};
/**
* HSL / HSLA Color value string -> RGBA values array
* @param {String} hslValue
* @return {ColorArray}
*/
const hslToRgba = hslValue => {
const hsla = consts.hslExecRgx.exec(hslValue) || consts.hslaExecRgx.exec(hslValue);
const h = +hsla[1] / 360;
const s = +hsla[2] / 100;
const l = +hsla[3] / 100;
const a = !helpers.isUnd(hsla[4]) ? +hsla[4] : 1;
let r, g, b;
if (s === 0) {
r = g = b = l;
} else {
const q = l < .5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = helpers.round(hue2rgb(p, q, h + 1 / 3) * 255, 0);
g = helpers.round(hue2rgb(p, q, h) * 255, 0);
b = helpers.round(hue2rgb(p, q, h - 1 / 3) * 255, 0);
}
return [r, g, b, a];
};
/**
* All in one color converter that converts a color string value into an array of RGBA values
* @param {String} colorString
* @return {ColorArray}
*/
const convertColorStringValuesToRgbaArray = colorString => {
return helpers.isRgb(colorString) ? rgbToRgba(colorString) :
helpers.isHex(colorString) ? hexToRgba(colorString) :
helpers.isHsl(colorString) ? hslToRgba(colorString) :
[0, 0, 0, 1];
};
exports.convertColorStringValuesToRgbaArray = convertColorStringValuesToRgbaArray;