UNPKG

@onesy/utils

Version:
22 lines (19 loc) 1.06 kB
import is from './is'; import isValid from './isValid'; import castParam from './castParam'; import clamp from './clamp'; const rgbToRgba = function (value) { let opacity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; let array = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; if (isValid('color-rgb', value)) { let values = value.replace(/rgb|a|\(|\s|\)/g, '').split(',').map((value_, index) => { if (index < 3) return Math.round(castParam(value_)); return castParam(value_); }); const a = opacity !== undefined ? +(opacity > 1 ? (opacity / 100).toFixed(2) : clamp(opacity, 0, 1)) : values[3]; values = [...values.slice(0, 3).map(item => Math.round(castParam(item))), is('number', a) && +a]; const [r, g, b] = values; return array ? values.filter(value_ => is('number', value_)) : "rgb".concat(is('number', a) ? 'a' : '', "(").concat(r, ", ").concat(g, ", ").concat(b).concat(is('number', a) ? ", ".concat(a) : '', ")"); } }; export default rgbToRgba;