Sunday, December 14, 2025
HomeLanguagesJavascriptD3.js | d3.lab() Function

D3.js | d3.lab() Function

The d3.lab() function in D3.js is used to construct a new Lab color and returns ‘l’, ‘a’ and ‘b’ properties of the specified color taken as the parameter of the function.

Syntax:

d3.lab(color);

Parameters: This function accepts single parameter color which is specified CSS color.

Return Value: This function returns ‘l’, ‘a’ and ‘b’ properties of the specified CSS color taken as the parameter of the function.

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

Example 1:




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


Output:

{"l":53.24079414130722, "a":80.09245959641109, "b":67.20319651585301, "opacity":1}
{"l":46.22743146876261, "a":-51.69849552989111, "b":49.8968460010561, "opacity":1}
{"l":32.297010932850725, "a":79.18751984512224, "b":-107.8601617541481, "opacity":1}

Example 2:




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


Output:

{"l":null, "a":null, "b":null, "opacity":1}

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

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

1 COMMENT

Most Popular

Dominic
32448 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6817 POSTS0 COMMENTS
Nicole Veronica
11954 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6954 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6899 POSTS0 COMMENTS
Umr Jansen
6883 POSTS0 COMMENTS