Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/randomize color #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,31 @@ const colors = [

const elementWord = document.querySelectorAll(".word");

function randomize(arr) {
const item = Math.floor(Math.random() * arr.length - 5);
const value = arr.splice(item, 1);
arr.push(value);
function randomize(arr, last) {
const filteredArr = arr.filter((value) => value !== last)
const item = Math.floor(Math.random() * filteredArr.length);

return value;
return filteredArr[item];
}

function intToHex(c) {
const hex = c.toString(16).toUpperCase();
return hex.length == 1 ? "0" + hex : hex;
}

function rgbToHex(rgb) {
const arr = eval('[' + rgb.slice(4,-1) + ']')
return "#" + intToHex(arr[0]) + intToHex(arr[1]) + intToHex(arr[2]);
}

function setColor(id) {
id.style.setProperty('--color', randomize(colors));
const currentColor = rgbToHex(getComputedStyle(id).backgroundColor)
id.style.backgroundColor = randomize(colors, currentColor);
}

function populate(element) {
setColor(element);
element.innerHTML = randomize(sources[element.id]);
element.textContent = randomize(sources[element.id], element.textContent);
}

function setChange() {
Expand Down