Saturday, October 25, 2025
HomeLanguagesJavascriptD3.js | color.rgb() Function

D3.js | color.rgb() Function

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

Syntax:

color.rgb()

Parameters: This function does not accept any parameters.

Return Value: This function returns the RGB equivalent of the specified color.

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

Example 1:




<!DOCTYPE html>
<html>
  
<head>
    <title>color.rgb() function</title>
  
    <script src='https://d3js.org/d3.v4.min.js'></script>
</head>
  
<body>
    <script>
          
        // Calling the d3.color() function function
        // with some parameters
        var color1 = d3.color("red");
        var color2 = d3.color("green");
        var color3 = d3.color("blue");
          
        // Calling the color.rgb() function
        var A = color1.rgb();
        var B = color2.rgb();
        var C = color3.rgb();
          
        // Getting the RGB equivalent of the specified color
        console.log(A);
        console.log(B);
        console.log(C);
    </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>color.rgb() function</title>
  
    <script src='https://d3js.org/d3.v4.min.js'></script>
</head>
  
<body>
    <script>
          
        // Calling the d3.rgb() function
        // without any parameters
        var color1 = d3.rgb();
          
        // Calling the color.rgb() function
        var A = color1.rgb();
          
        // Getting the RGB equivalent of the specified color
        console.log(A);
    </script>
</body>
  
</html>


Output:

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

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS