当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


d3.js color.darker()用法及代码示例


D3.js的color.darker()函数用于复制原始颜色中有些暗或明亮的颜色。

用法:

color.darker(k)

参数:它采用以下参数:

  • k:“k”是原始颜色所需的暗度。它是整数值。

返回值:它返回对象。

范例1:



当没有给出k的值时

<!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> 
  .originalColor{ 
    height:100px; 
    width:100px; 
  } 
  .darkerColor{ 
    height:100px; 
    width:100px; 
  } 
</style> 
<body> 
  Color without color.darker() property 
  <div class="originalColor"> 
  
  </div> 
  Color with color.darker() property 
  <div class="darkerColor"> 
  
  </div> 
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js"> 
  </script> 
  <script> 
    let originalColor= 
           document.querySelector(".originalColor"); 
    let darkerColor= 
           document.querySelector(".darkerColor"); 
  
    var color= d3.color("green"); 
    originalColor.style.backgroundColor= 
           `rgb(${color.r}, ${color.g}, ${color.b})`; 
    var color=color.darker() 
    darkerColor.style.backgroundColor= 
          `rgb(${color.r}, ${color.g}, ${color.b})`; 
  </script> 
</body> 
</html>

输出:

范例2:

当k的值> 0时

<!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> 
  .originalColor{ 
    height:100px; 
    width:100px; 
  } 
  .darkerColor{ 
    height:100px; 
    width:100px; 
  } 
</style> 
<body> 
  Color without color.darker() property 
  <div class="originalColor"> 
  
  </div> 
  Color with color.darker(2) property 
  <div class="darkerColor"> 
  
  </div> 
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js"> 
  </script> 
  <script> 
    let originalColor= 
document.querySelector(".originalColor"); 
    let darkerColor= 
document.querySelector(".darkerColor"); 
    var color= d3.color("green"); 
    originalColor.style.backgroundColor= 
`rgb(${color.r}, ${color.g}, ${color.b})`; 
    var color=color.darker(2) 
    darkerColor.style.backgroundColor= 
`rgb(${color.r}, ${color.g}, ${color.b})`; 
  </script> 
</body> 
</html>

输出:

范例3:

当k <0时

<!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> 
  .originalColor{ 
    height:100px; 
    width:100px; 
  } 
  .darkerColor{ 
    height:100px; 
    width:100px; 
  } 
</style> 
<body> 
  Color without color.darker() property 
  <div class="originalColor"> 
  
  </div> 
  Color with color.darker(-2) property 
  <div class="darkerColor"> 
  
  </div> 
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js"> 
  </script> 
  <script> 
    let originalColor= 
document.querySelector(".originalColor"); 
    let darkerColor= 
document.querySelector(".darkerColor"); 
    var color= d3.color("green"); 
    originalColor.style.backgroundColor= 
`rgb(${color.r}, ${color.g}, ${color.b})`; 
    var color=color.darker(-2) 
    darkerColor.style.backgroundColor= 
`rgb(${color.r}, ${color.g}, ${color.b})`; 
  </script> 
</body> 
</html>

输出:




相关用法


注:本文由纯净天空筛选整理自tarun007大神的英文原创作品 D3.js color.darker() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。