quantique-converter
Version:
Converters for all kind of data
35 lines (29 loc) • 782 B
JavaScript
// Convert Hex color code to RGBS Color Code
const convertHexToRGBA = (hex, parameters) => {
hex = hex.replace(/^#/, '');
if (hex.length === 3) {
hex = hex
.split('')
.map((char) => char + char)
.join('');
}
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
let rgbaValue = '';
if (parameters?.opacity) {
rgbaValue = `rgba(${r}, ${g}, ${b}, ${parameters?.opacity})`;
} else {
return {
isValid: false,
error: 'Please enter "opacity" value',
convertedValue: null,
};
}
return {
isValid: true,
error: '',
convertedValue: rgbaValue,
};
};
module.exports = convertHexToRGBA;