D3.js | color.toString() Function

The color.toString() function in D3.js is used to return a string representing the color according to the CSS Object Model specification.

Syntax:

color.toString()

Parameters: This function does not accept any parameters.



Return Value: This function returs a string representing the color according to the CSS Object Model specification.

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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>color.toString() function</title>
  
    <script src='https://d3js.org/d3.v4.min.js'></script>
</head>
  
<body>
    <script>
          
        // Calling the d3.color() function fuction
        // with some parameters
        var color1 = d3.color("red");
        var color2 = d3.color("green");
        var color3 = d3.color("blue");
          
        // Calling the color.toString() function
        var A = color1.toString();
        var B = color2.toString();
        var C = color3.toString();
          
        // Getting the string representing
        console.log(A);
        console.log(B);
        console.log(C);
    </script>
</body>
  
</html>

chevron_right


Output:

rgb(255, 0, 0)
rgb(0, 128, 0)
rgb(0, 0, 255)

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

rgb(0, 0, 0)

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.