How to convert Hex to RGBA value using JavaScript ?
Last Updated :
25 Aug, 2023
Given a color hex code and the task is to convert Hex Value to RGBA value. Here are few of the mostly techniques discussed with the help of JavaScript.
Approach 1:
- First check the passed Hex value is valid or not through a Regular Expression.
- Then get the hex value content after '#' by using .slice() method and use .split() method to get the every character of the hex value in the array.
- If the length of the array is 3(eg. #fff, #aaa) then store them as(eg. #ffffff, #aaaaaa respectively) to make the length 6.
- Transform the code to Hexadecimal as the first 2 character belongs to Red Value, the mid 2 character belongs to Green value, and so on.
Example: This example uses the approach discussed above.
JavaScript
const hexValue = '#fbafff';
function convertHexToRgbA(hexVal) {
let ret;
// If the hex value is valid.
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hexVal)) {
// Getting the content after '#',
// eg. 'ffffff' in case of '#ffffff'
ret = hexVal.slice(1);
// Splitting each character
ret = ret.split('');
// Checking if the length is 3
// then make that 6
if (ret.length == 3) {
let ar = [];
ar.push(ret[0]);
ar.push(ret[0]);
ar.push(ret[1]);
ar.push(ret[1]);
ar.push(ret[2]);
ar.push(ret[2]);
ret = ar;
}
// Starts with '0x'(in hexadecimal)
ret = '0x' + ret.join('');
// Converting the first 2 characters
// from hexadecimal to r value
let r = (ret >> 16) & 255;
// Converting the second 2 characters
// to hexadecimal to g value
let g = (ret >> 8) & 255;
// Converting the last 2 characters
// to hexadecimal to b value
let b = ret & 255;
// Appending all of them to make
// the RGBA value
return 'rgba(' + [r, g, b].join(',') + ',1)';
}
}
function myFunc() {
console.log("The RGBA value of '"
+ hexValue + "' is '" +
convertHexToRgbA(hexValue) + "'");
}
myFunc();
[tabbyending]
OutputThe RGBA value of '#fbafff' is 'rgba(251,175,255,1)'
Approach 2:
- Use .slice() method to get the first 2 characters and convert them to Hexadecimal.
- Repeat step 1 for every 2 characters for the rest of the code.
- Append them together and return the final value.
Example: This example uses the approach discussed above.
JavaScript
const hexValue = '#fbafff';
function convertHexToRgbA(hex, a) {
// Convert the first 2 characters to hexadecimal
let r = parseInt(hex.substring(1, 3), 16),
// Convert the middle 2 characters to hexadecimal
g = parseInt(hex.substring(3, 5), 16),
// Convert the last 2 characters to hexadecimal
b = parseInt(hex.substring(5, 7), 16);
// Append them all
return "rgba(" + r + ", " + g + ", "
+ b + ", " + a + ")";
}
function myFunc() {
console.log("The RGBA value of '"
+ hexValue + "' is '" +
convertHexToRgbA(hexValue, 1) + "'");
}
myFunc();
[tabbyending]
OutputThe RGBA value of '#fbafff' is 'rgba(251, 175, 255, 1)'
Similar Reads
How to convert Color Names to Hexcode using JavaScript ? Given a color name and the task is to get the HexCode of the color name. There are a few of the techniques discussed with the help of JavaScript. Approach 1: Store the HexCodes of all possible color names in a JavaScript Object.Compare the given color name with a list of color objects and if a match
4 min read
How to convert decimal to hex in JavaScript ? Given a number and the task is to convert the number from decimal to hex. This can be done by using toString() method. It takes the parameter which is the base of the converted string. In this case, the base will be 16. Syntax: decimalNumber.toString( radix ) Parameters: decimalNumber: It holds the
1 min read
How to get hex color value of RGB value ? Given the RGB color value and the task is to get the Hex Code of that color using JavaScript. Approach 1: Call the convert() user-defined function and use RGB value as a parameter.Use the match() method to select the value of Red, Green, and Blue. The value of RGB is stored in the form of array.The
2 min read
Convert Hex to RGBa for background opacity using SASS Sass rgba function uses Red-green-blue-alpha model to describe colours where alpha is used to add opacity to the color. It's value ranges between 0.0 (completely transparent) to 1.0 (completely opaque). The function takes two input values- the hex color code and alpha and converts Hex code to RGBa f
2 min read
How to create Hex color generator using HTML CSS and JavaScript? Hex color codes are six-digit combinations representing the amount of red, green, and blue (RGB) in a color. These codes are essential in web design and development for specifying colors in HTML and CSS. The hex color generator provides the hex code for any selected color, making it a valuable tool
2 min read
How to create RGB color generator using HTML CSS and JavaScript ? In this article, we will create a RGB color generator using HTML, CSS, and JavaScript. Using RGB color generator, we can construct all the colors from the combination of Red, Green, Blue colors. Each color is represented by the range of decimal numbers from 0 to 255 (256 levels for each color). So,
3 min read