Using d3plus.color.text provides the ability to dynamically assign a text color (either light or dark) depending on a background color.
Featured on D3plus.org
Using d3plus.color.text provides the ability to dynamically assign a text color (either light or dark) depending on a background color.
Featured on D3plus.org
| <!DOCTYPE html> | |
| <meta charset="utf-8"> | |
| <script src="//d3plus.org/js/d3.js"></script> | |
| <script src="//d3plus.org/js/d3plus.js"></script> | |
| <style> | |
| div#grid { | |
| margin: 25px auto; | |
| text-align: center; | |
| width: 650px; | |
| } | |
| div.color { | |
| display: inline-block; | |
| font: 10px "Helvetica", "Arial", sans-serif; | |
| margin: 15px; | |
| padding: 25px 10px; | |
| width: 50px; | |
| } | |
| </style> | |
| <div id="grid"></div> | |
| <script> | |
| var colors = d3plus.color.scale.range() | |
| colors = colors.concat(["#525252", "#737373", "#969696", "#bdbdbd", "#d9d9d9", "#f7f7f7"]) | |
| var squares = d3.select("#grid").selectAll("div.color") | |
| .data( colors ) | |
| .enter().append("div") | |
| .attr("class","color") | |
| .attr("id", String) | |
| .style("background-color", String) | |
| .style("color", function(d){ | |
| return d3plus.color.text(d) | |
| }) | |
| .text(String) | |
| </script> |