HomeLanguagesJavascriptD3.js | d3.color() Function

D3.js | d3.color() Function

The d3.color() function in D3.js is used to parse the specified CSS color used as parameter of the function and return RGB or HSL color. If specifier is not given, then null is returned.

Syntax:

d3.color(color);

Parameters: This function accepts single parameter color which specifies the CSS color.

Return Value: This function returns the RGB or HSL color value of the specified CSS color taken as the parameter of the function.

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

Example 1:




<!DOCTYPE html>
<html>
  
<head>
    <title>d3.color() 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");
        
        // Getting the RGB or HSL 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.color() function</title>
  
    <script src='https://d3js.org/d3.v4.min.js'></script>
</head>
  
<body>
    <script>
          
        // Calling the d3.color() function function
        // without any parameters
        var color = d3.color();
        
        // Getting the RGB or HSL values
        console.log(color);
    </script>
</body>
  
</html>


Output:

null

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

RELATED ARTICLES

Most Popular

Dominic
32712 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7088 POSTS0 COMMENTS
Nicole Veronica
12228 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12310 POSTS0 COMMENTS
Shaida Kate Naidoo
7203 POSTS0 COMMENTS
Ted Musemwa
7470 POSTS0 COMMENTS
Thapelo Manthata
7189 POSTS0 COMMENTS
Umr Jansen
7171 POSTS0 COMMENTS