You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
868 B
33 lines
868 B
<div id="grid"></div> |
|
|
|
<script> |
|
const G = 9; |
|
const grid = document.getElementById("grid"); |
|
|
|
// set up grid style |
|
grid.style.display = "grid"; |
|
grid.style.gridTemplateColumns = `repeat(${G},9px)`; |
|
grid.style.gridAutoRows = "9px"; |
|
|
|
// --- compute maximum number of 1s dynamically --- |
|
let maxOnes = 0; |
|
for (let i = 0; i < G*G; i++) { |
|
const ones = (i.toString(3).match(/1/g) || []).length; |
|
if (ones > maxOnes) maxOnes = ones; |
|
} |
|
|
|
// --- create grid cells --- |
|
for (let i = 0; i < G*G; i++) { |
|
const ones = (i.toString(3).match(/1/g) || []).length; |
|
const d = document.createElement("div"); |
|
d.style.width = "9px"; |
|
d.style.height = "9px"; |
|
|
|
// autoscale color |
|
const hue = 120 - (ones / maxOnes) * 120; // green → red |
|
d.style.background = `hsl(${hue},100%,50%)`; |
|
|
|
grid.appendChild(d); |
|
console.log(ones / maxOnes); |
|
} |
|
</script>
|
|
|