![]() ![]() There are certainly other ways to achieve the same result so do check them out and give them a try. You can find the complete code on my GitHub repository. We've successfully built a random hex color generator using HTML, CSS and a few lines of JavaScript. Then the cycle repeats itself until the 6th round is over at which time a full 6-digit hex code will have been generated.Īll that's left is to set the textContent of the span tag and the background color of the page to be the value of hex. I can now access the hexValues array item in the position corresponding to the generated index number using bracket notation, then add it to the end of the hex variable i.e hex += hexValues. ![]() The random whole number generated is appended to a new variable index. This makes sure that any number generated is a valid hexValues index. It basically rounds it down to the nearest whole number. So what do we do if we want a whole number larger than 1? We multiply it by the number we want (in this case, the length of the hexValues array) and wrap it within the Math.floor() function which returns the largest integer less than or equal to a given number. Now, the Math.random() function picks a random number between 0 and 1 (not including 1) and returns a decimal but we don't want decimals. Then it loops over the hexValues array 6 times and each time generates a random number using Math.random(). RGB to HEX RGB to RGBA RGB to ARGB RGB to HSL HSL to RGB HSL to HEX HSL to RGBA HSL to ARGB ARGB to HEX ARGB to RGB ARGB to RGBA ARGB to HSL RGBA to HEX RGBA to RGB RGBA to ARGB RGBA to HSL. Enter fullscreen mode Exit fullscreen modeĮvery time the button is clicked, the changeHex function is called which creates a variable hex and sets its value to #.
0 Comments
Leave a Reply. |