console
function generateDistinctColors(count) {
if (count > 10 || count < 1) {
throw new Error("Count must be between 1 and 9");
}
const colors = [];
while (colors.length < count) {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
const luminance = 0.299 * r + 0.587 * g + 0.114 * b;
if (luminance < 30 || luminance > 225) {
continue;
}
const newColor = { r, g, b };
let isDistinct = true;
for (const color of colors) {
const distance = Math.sqrt(
Math.pow(color.r - newColor.r, 2) +
Math.pow(color.g - newColor.g, 2) +
Math.pow(color.b - newColor.b, 2)
);
if (distance < 80) {
isDistinct = false;
break;
}
}
if (isDistinct) {
colors.push(newColor);
}
}
return colors.map(color => `rgb(${color.r},${color.g},${color.b})`);
}
function init() {
let container = document.getElementById('container');
let color = generateDistinctColors(10);
color.forEach(i => {
const newDiv = document.createElement('div');
newDiv.textContent = `Div ${i}`;
newDiv.style.backgroundColor = i;
newDiv.style.margin = '10px';
newDiv.style.padding = '20px';
newDiv.style.border = '1px solid #ccc';
newDiv.style.textAlign = 'center';
newDiv.style.fontSize = '16px';
container.appendChild(newDiv);
})
}
init();
<div id="container"></div>