當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


d3.js interpolateLab()用法及代碼示例

D3.js中的interpolateLab()函數用於返回兩種給定顏色之間的CIELAB顏色空間插值器。作為參數給出的顏色不需要為CIELAB格式。稍後可以通過內置函數d3.lab()將簡單的顏色名稱轉換為CIELAB格式。

用法:

d3.interpolateLab(a, b);

參數:它采用以下兩個參數:

  • a:它是數據類型字符串的顏色的名稱。
  • b:它是數據類型字符串的顏色的名稱。

返回值:它返回內插器函數。

下麵給出了上述函數的一些示例。



範例1:在控製台中打印顏色。

<!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>

輸出:

範例2:在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>

輸出:




相關用法


注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js interpolateLab() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。