How to convert rgb() color string into an object in JavaScript ? Last Updated : 06 Apr, 2021 Comments Improve Suggest changes Like Article Like Report Given a color in the form of rgb( ) or rgba() and the task is to convert it into an object where the keys are the color names and values are the color values. Example:Input: rgb(242, 52, 110) Output: { red: 242, green: 52, blue: 110 } Input: rgba(255, 99, 71, 0.5) Output: { red: 255, green: 99, blue: 71, alpha: 0.5 } Approach: To achieve this we use the following approach. Store the color in a variable named rgb.Create an array named colors that contain the names of the colors red, green, blue, and alpha.Create a variable names colorArr in which we store the color values of the input rgb. For example: ["255", "99", "71", 0.5], to achieve this we slice the rgb from where the "(" present to from where the ")" present. Now you got the string "255, 99, 71, 0.5". Now split the array from where the ", " present. Now you get the array ["255", '99", "71", "0.5"].Now create an empty object.Apply forEach loop on the colorArr and for every iteration insert the name of color and value of the color to the object.Now print the object. JavaScript <script> let rgb = "rgba(255, 99, 71, 0.5)" let colors = ["red", "green", "blue", "alpha"] // Getting the index of "(" and ")" // by using the indexOf() method let colorArr = rgb.slice( rgb.indexOf("(") + 1, rgb.indexOf(")") ).split(", "); let obj = new Object(); // Insert the values into obj colorArr.forEach((k, i) => { obj[colors[i]] = k }) console.log(obj) </script> Output: { alpha: "0.5", blue: "71", green: "99", red: "255" }Wrap the logic inside a function JavaScript <script> function rgbToObj(rgb) { let colors = ["red", "green", "blue", "alpha"] let colorArr = rgb.slice( rgb.indexOf("(") + 1, rgb.indexOf(")") ).split(", "); let obj = new Object(); colorArr.forEach((k, i) => { obj[colors[i]] = k }) return obj; } console.log(rgbToObj("rgb(255, 99, 71)")) console.log(rgbToObj("rgb(255, 99, 71, 0.5)")) </script> Output: { blue: "71", green: "99", red: "255" } { alpha: "0.5", blue: "71", green: "99", red: "255" } Comment More infoAdvertise with us Next Article How to convert rgb() color string into an object in JavaScript ? D devi_johns Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Properties JavaScript-Methods JavaScript-Questions +1 More Similar Reads How to call function from it name stored in a string using JavaScript ? In this article, we will call a function from the string stored in a variable. There are two methods to call a function from a string stored in a variable. Using window object methodUsing eval() method Note: The eval() method is older and is deprecated. Method 1: Using the window object. The window 2 min read 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 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 How to Add Colors to JavaScript Console Outputs ? To Add Colors to JavaScript Console Outputs, we have multiple approaches In this article, we will learn to Add colors to Javascript console outputs. Below are the approaches used to add Colors to JavaScript Console Outputs: Table of Content Using %CUsing ANSI escape code Approach 1: Using %CThe %c p 1 min read How to add special characters to text to print in color in the console in JavaScript ? The purpose of this article is to add special characters to text to print in color in the console in JavaScript. Approach: The ANSI escape codes help change/specify the color of the output in the console. The color of the output of the console can be changed by adding these escape codes just before 2 min read Like