Кольори для головної діагоналі
Головна діагональ:
Вище головної діагоналі:
Нижче головної діагоналі:
Створюйте таблиці з різними варіантами розфарбування: діагоналі, четверті або їх комбінації
Нижче наведено код основних функцій, які використовуються в цьому генераторі таблиць.
function createTable(size) {
const table = document.createElement('table');
for (let i = 0; i < size; i++) {
const row = document.createElement('tr');
for (let j = 0; j < size; j++) {
const cell = document.createElement('td');
cell.textContent = `${i+1},${j+1}`;
row.appendChild(cell);
}
table.appendChild(row);
}
return table;
}
function colorByQuartersBetweenDiagonals(table) {
const rows = table.getElementsByTagName('tr');
const size = rows.length;
for (let i = 0; i < rows.length; i++) {
const cells = rows[i].getElementsByTagName('td');
for (let j = 0; j < cells.length; j++) {
const onMainDiagonal = (i === j);
const onSecondaryDiagonal = (i + j === size - 1);
if (onMainDiagonal) {
// Головна діагональ
cells[j].style.backgroundColor = mainDiagonalColor;
cells[j].style.color = getContrastColor(mainDiagonalColor);
} else if (onSecondaryDiagonal) {
// Побічна діагональ
cells[j].style.backgroundColor = secondaryDiagonalColor;
cells[j].style.color = getContrastColor(secondaryDiagonalColor);
} else if (i < j && i + j < size - 1) {
// Верхня чверть
cells[j].style.backgroundColor = topQuarterColor;
cells[j].style.color = getContrastColor(topQuarterColor);
} else if (i < j && i + j > size - 1) {
// Права чверть
cells[j].style.backgroundColor = rightQuarterColor;
cells[j].style.color = getContrastColor(rightQuarterColor);
} else if (i > j && i + j > size - 1) {
// Нижня чверть
cells[j].style.backgroundColor = bottomQuarterColor;
cells[j].style.color = getContrastColor(bottomQuarterColor);
} else if (i > j && i + j < size - 1) {
// Ліва чверть
cells[j].style.backgroundColor = leftQuarterColor;
cells[j].style.color = getContrastColor(leftQuarterColor);
}
}
}
}