lundi 29 mars 2021

I get an occasional error when I generate a random colour in JS how do I get it to generate rr,gg,bb colour? [duplicate]

I occasionaly get the following error in the console:-

The specified value "#b6aaf" does not conform to the required format. The format is "#rrggbb" where rr, gg, bb are two-digit hexadecimal numbers. randomGradient @ script.js:25

I understand why I am getting the error how do I get it to return colour in the correct format?

var css = document.querySelector("h3");
var color1 = document.querySelector(".color1");
var color2 = document.querySelector(".color2");
var body = document.getElementById("gradient");
var randcol = document.getElementById("randcol");

function setGradient() {
    body.style.background = 
    "linear-gradient(to right, " 
    + color1.value 
    + ", " 
    + color2.value 
    + ")";

    css.textContent = body.style.background + ";";
}

function randomColor() {
        var newColor = '#' + (Math.random() * 0xFFFFFF << 0).toString(16);
        return newColor;
}

function randomGradient() {
    color1.value = randomColor();
    color2.value = randomColor();
    setGradient();
}

randcol.addEventListener("click",randomGradient);

color1.addEventListener("input", setGradient);

color2.addEventListener("input", setGradient);



Aucun commentaire:

Enregistrer un commentaire