Thursday, November 20, 2025
HomeLanguagesJavascriptD3.js | d3.hcl() Function

D3.js | d3.hcl() Function

The d3.hcl() function in D3.js is used to construct a new HCL color and returns HCL properties of the specified color taken as the parameter of the function.

Syntax:

d3.hcl(color);

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

Return Value: This function returns HCL properties of the specified CSS color taken as the parameter of the function.

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

Example 1:




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


Output:

{"h":39.99901061253295,"c":104.55176567686985,"l":53.24079414130722,"opacity":1}
{"h":136.01595303206318,"c":71.8500499715016,"l":46.22743146876261,"opacity":1}
{"h":306.2849380699878,"c":133.80761485376166,"l":32.297010932850725,"opacity":1}

Example 2:




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


Output:

{"h":null,"c":null,"l":null,"opacity":1}

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

RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6775 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6903 POSTS0 COMMENTS
Ted Musemwa
7159 POSTS0 COMMENTS
Thapelo Manthata
6859 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS