I hope you are familiar with RGB color.
An RGB color consists of red, green and blue. It specifies with rgb(red, green, blue) where each parameter defines the intensity of that color as the integer that can be between 0 to 255.
var rgbColor = 'rgb(46, 123, 14)'; rgbArr = rgbColor.substring(4, rgbColor.length-1).replace(/ /g, '').split(','); console.log(rgbArr);
Now if you run the above code, you will get an array that will consist of the RGB color components. Below is the result we see in the console if we run the code:
["46", "123", "14"]
In our code, we take the RGB component part only using the substring() method. We split RGB color components by “comma” using the split() method. Thus, we able to get an array of RGB color items.
Now it is very easy to take each item from our array which will contain a color item of RGB color. Below is how we can get red, green and blue component from an RGB color and display it on the console:
console.log(rgbArr); // Red component console.log(rgbArr); // Green component console.log(rgbArr); //Blue component