Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

D3.js | d3.rgb() Function

  • Last Updated : 29 Nov, 2019

The d3.rgb() function in D3.js is used to return RGB value of the specified CSS color.

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

d3.rgb(color);

Parameters: This function accepts single parameter color which is used to specify the CSS color.

Return Value: This function returns RGB value of the given CSS color.



Below programs illustrate the d3.rgb() function in D3.js:

Example 1:




<!DOCTYPE html>
<html>
  
<head>
    <title>d3.rgb() function</title>
  
    <script src='https://d3js.org/d3.v4.min.js'></script>
</head>
  
<body>
    <script>
          
        // Calling the d3.rgb() function function
        // with some parameters
        var color1 = d3.rgb("red");
        var color2 = d3.rgb("green");
        var color3 = d3.rgb("blue");
        
        // Getting the RGB values
        console.log(color1);
        console.log(color2);
        console.log(color3);
    </script>
</body>
  
</html>

Output:

{"r":255, "g":0, "b":0, "opacity":1}
{"r":0, "g":128, "b":0, "opacity":1}
{"r":0, "g":0, "b":255, "opacity":1}

Example 2:




<!DOCTYPE html>
<html>
  
<head>
    <title>d3.rgb() function</title>
  
    <script src='https://d3js.org/d3.v4.min.js'></script>
</head>
  
<body>
    <script>
          
        // Calling the d3.rgb() function function
        // without any parameters
        var color = d3.rgb();
        
        // Getting the RGB values
        console.log(color);
    </script>
</body>
  
</html>

Output:

{"r":null, "g":null, "b":null, "opacity":1}

Reference: https://devdocs.io/d3~5/d3-color#rgb




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!