Skip to content

Instantly share code, notes, and snippets.

@PROPHESSOR
Created August 8, 2018 23:51
Show Gist options
  • Select an option

  • Save PROPHESSOR/efb894666e1c78dd4ea364446c5c8e72 to your computer and use it in GitHub Desktop.

Select an option

Save PROPHESSOR/efb894666e1c78dd4ea364446c5c8e72 to your computer and use it in GitHub Desktop.
Генератор нужного количества случайных цветов в HEX на JavaScript
// Copyright (c) PROPHESSOR 2018
const mult = 128;
const count = 256;
function random(min, max) {
return Math.floor(Math.random() * max) + min;
}
function generate() {
return `#${random(0, 255).toString(16)}${random(0, 255).toString(16)}${random(0, 255).toString(16)}`;
}
for(let i = 0; i < count; i++) {
const div = document.createElement('div');
const hex = generate();
div.style=`position:absolute; top: 0; left: ${i * mult}px; background: ${hex}; width: ${mult}px; height: ${mult}px`;
div.innerHTML = hex;
document.querySelector('#test').appendChild(div);
}
@idd2000

idd2000 commented Nov 6, 2024

Copy link
Copy Markdown

В 11 сточке не хватает .padStart(2, '0')
return #${random(0, 255).toString(16).padStart(2, '0')}${random(0, 255).toString(16).padStart(2, '0')}${random(0, 255).toString(16).padStart(2, '0')};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment