Thursday, October 23, 2025
HomeLanguagesJavascriptD3.js interpolateLab() Function

D3.js interpolateLab() Function

The interpolateLab() function in D3.js is used to return a CIELAB color space interpolator between two given colors. The colors given in as parameters are not required to be in CIELAB format. Simple color names can be given later on they are converted by the inbuilt function d3.lab() into CIELAB format.

Syntax:

d3.interpolateLab(a, b);

Parameters: It takes the following two parameters:

  • a: It is the name of the color of datatype string.
  • b: It is the name of the color of datatype string.

Returns: It returns the interpolator function.

Below given are a few examples of the above function.

Example 1: Printing color in the console.




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, 
                 initial-scale=1.0">
  <title>Document</title>
</head>
<style>
</style>
<body>
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js">
  </script>
  <script>
    // Printing the return type of the function
    console.log("Type of the function is: ",
 typeof(d3.interpolateLab("green", "yellow")))
    console.log("A RGB string: ", 
d3.interpolateLab("green", "yellow")(0.435))
    console.log("A RGB string",
 d3.interpolateLab("green", "white")(0.888996))
  </script>
</body>
</html>


Output:

Example 2: Using function with HTML.




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width, 
                 initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  .box1, .box2{
    display: flex;
    margin-right: 40px;
    width: 150px;
    height: 150px;
  }
  div{
    display: flex;
  }
</style>
<body>
  D3.interpolateLab()
  <div>
    <div class="box1">
    </div>
    <div class="box2">
    </div>
  </div>
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js">
  </script>
  <script>
    let b1=document.querySelector(".box1");
    let b2=document.querySelector(".box2");
    let color=d3.interpolateLab("green", "yellow")(0.435);
    let color2=d3.interpolateLab("green", "white")(0.888996); 
    b1.style.backgroundColor=color;
    b2.style.backgroundColor=color2;
  </script>
</body>
</html>


Output:

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