Create a Random Color Generator with Javascript

Developer from NYC. Current Tech Stack: React 👨🏽💻 SCSS 📜
Search for a command to run...

Developer from NYC. Current Tech Stack: React 👨🏽💻 SCSS 📜
No comments yet. Be the first to comment.
Hello World I welcome to you another tutorial and code along for building a countdown timer. To be able to follow along, it is best to have a good grasp of Javascript classes and its methods along with the following topics: Document Object Model(DOM...

Welcome to the first of this series: articles based on Side Hustles for dummies by Alan Simon. You can view this series as a read-along, a summarization, or a reference. I consider my work and time spent on this series as a way to acquire more knowle...

Recursion is one of those computer science topics that, once you first come across your head, your thinking… is this really necessary? Why do I really need to learn this, or is it mandatory? I usually lean towards the iterative approach. But after de...

The most important thing I’ve learned as a developer is that it’s really easy to get stuck in tutorial hell. You get comfortable with the fact that there’s somebody out there willing to teach you, write guides, roadmaps…etc, and then one day you real...

I created a Custom Build Number with Gradient. The main focus of this project was more on CSS, and the topic I dove into was linear gradients. I started by creating a custom build number, which I will be using for the duration of this project. I will...

This is a tutorial and code for building a random color generator that displays the hex color code of the current document background color. To be able to follow along, it is best to at least have an idea about the following topics:
Furthermore, I will explain the topics I’m using to build this web app. This is an excellent tutorial for javascript beginners, and here’s what the outcome should be.

The first step is to create the files we will be working with. The mandatory HTML and javascript files along with the CSS file. Don’t forget to link the .js and .css files within the .html file.

Since I will demonstrate mainly DOM manipulation and events, file path linking is the only work that needs to be done on the actual HTML file. Everything will be created and changed purely from within javascript, along with some extra styling within the CSS file.
We’re gonna be setting up essentially the bones of the project. Without this, there’s nothing.
We use the method from a built-in javascript class (document) to create elements. I like to keep things organized, so I usually make the main tag that will act as a container for all the content.
const main = document.createElement('main'), // A main to hold the main elements of the document
colorGeneratorButton = document.createElement('button'), // the button that changes the "background color" of the document
hexCodeHeading = document.createElement('h1'); // the text that will display the current document color
We can add text to elements with the following methods:
I use textContent when I want to set an element's text. But if I want to capture what's already within an element, I use innerHTML. The difference in the methods is what data is rendered or returned, as shown in the article linked for innerText.
With that said, let’s add some text to the button.
colorGeneratorButton.textContent = 'Next Color';
When you create an element, it signifies to the browser that the element can be used. It does not mean it will render unless specifically told to do so. So, we can use the following methods to add elements:
I’ve rarely used appendChild because I see it as a way to directly append an element to the end of the parentNode(usually container). While on the other hand, append allows you to add multiple node objects.
So, First, we will add the hexCodeHeading and button elements to the main element. And then add that same main element to the document element.
main.append(hexCodeHeading, colorGeneratorButton);
Tip: add the element in the order that you would want them nested in an HTML file
furthermore, we add the main element to the document like so:
document.body.append(main);
First, I usually like to get the app working fundamentally and then refactor it to make it work correctly the way it is intended. In doing so, I have been able to clean up the code and learn new ways of doing things. So we can start by attempting to understand what we need. Background color can compose the actual color’s name, RGB() colors, hue saturation colors, etc. For this project, the goal was to obtain RGB colors and convert them to hex.
We will focus on generating RGB colors. The RGB has three sets of numbers separated by commas ranging from 1 to 255. The goal is to create a function that returns a value at a minimum of 1 up to and including 255. For this, we will use a couple more javascript built-in methods called random(), ceil(), and floor(), all of which comes from the Math object.
const getRandomInt = (min, max) ⇒ {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor( Math.random() * (max - min + 1));
}
There’s a lot to unpack here, but the methods are as follows:
Now that we can generate numbers, let us create a function that makes the RGB() needed.
We create a function(changeColor) that will later be used in an event listener.
const changeColor = () ⇒ {
redColor = getRandomInteger(1, 255),
greenColor = getRandomInteger(1, 255),
blueColor = getRandomInteger(1,255);
}
At this point, we have created a function that can return 3 different sets of numbers, all between 1 to 255. Now our next goal is to convert the RGB numbers into a hex color code.
Hex color code is preferable when coding because:
So to create a function that converts RGB to Hex, once again, we must understand the core concept of a hex code and RGB color code.
A hex color code is:
An RGB goes by the decimal number system, based on base-10, which includes 10 unique characters, 0 - 9. So each character 1 - 15 in the decimal number system corresponds to a hexadecimal system.
The first step is to create a system for which we can look up numbers when it's time to convert. Create an object with the key-value pairs being decimal:hex.
const system = {
0: 0,
1 : 1,
2 : 2,
3 : 3,
4 : 4,
5 : 5,
6 : 6,
7 : 7,
8 : 8,
9 : 9,
10 : "A",
11 : "B",
12 : "C",
13 : "D",
14 : "E",
15 : "F",
}
For this function, we will use template literals and string interpolation.
Template literals are created with interpolation is a great way to insert a variable/expression/function within a string.
They are created using back-tick(`) characters. To use interpolation, we use a dollar sign and open closing brackets with the expression inside(${functionExpressionName}).
We will create a function that takes in a value which is the random color integer from rgb.
So the pseudocode is as follows:
take input value
divide input value by 16 which usually results in decimal
first digit is number before decimal point
second digit is the number after decimal point * 16
save first number to a variable
save second number to a variable
use both numbers to look up in the system
the value to the key is the hex color code number
const rgbToHex = x => {
character = ((x / 16).toFixed(2)).toLocaleString().split('.');
firstCode = Number(character[0]), secondCode = (Number(`.${character[1]}`))*16;
return `${system[firstCode]}${system[secondCode.toFixed(0)]}`;
};
In this section, we will focus on actually creating the web app's interactivity. But before that, let’s refactor the changeColor function. We will place the rgbToHex function with the return value of the random integers as the input.
There are two ways to do this.
We can create separate variables and place the variables within the function like this:
const changeColor = () => {
let redColor = rgbToHex(getRandomInteger(1,255)),
greenColor = rgbToHex(getRandomInteger(1,255)),
blueColor = rgbToHex(getRandomInteger(1,255));
let hexRed = rgbToHex(redColor),
hexGreen = rgbToHex(greenColor),
blueColor = rgbToHex(blueColor);
}
Or, as a way to be more concise and less verbose, we can call the function within the parameter of the rgbToHex like so:
const changeColor = () => {
let redColor = rgbToHex(getRandomInteger(1,255)),
greenColor = rgbToHex(getRandomInteger(1,255)),
blueColor = rgbToHex(getRandomInteger(1,255));
}
We then create a variable that will display the randomized hex color code value using template literal and string interpolation. We will eventually set the document's background color to that hexCode value.
hexCode = `#${redColor}${greenColor}${blueColor}`;
document.body.style.backgroundColor = hexCode;
Ultimately we display the actual hex color code number using the textcontent method on the heading element we created earlier.
hexCodeHeading.textContent = hexCode;
Now we add the functionality of the web app, the event listener. We take the button, addEventListener, the event is "click," and add the function we want to run once the event occurs, which is the changeColor function.
So our changeColor function should look like this:
const changeColor = () => {
let redColor = rgbToHex(getRandomInteger(1,255)),
greenColor = rgbToHex(getRandomInteger(1,255)),
blueColor = rgbToHex(getRandomInteger(1,255));
hexCode = `#${redColor}${greenColor}${blueColor}`;
document.body.style.backgroundColor = hexCode;
hexCodeHeading.textContent = hexCode;
};
And last but not least, our eventListener:
colorGeneratorButton.addEventListener('click', changeColor);
This is our entire javascript file up to this point:
const main = document.createElement('main'), // A main to hold the main elements of the document
colorGeneratorButton = document.createElement('button'), // the button that changes the "background color" of the document
hexCodeHeading = document.createElement('h1'); // the text that will display the current document color
main.classList.add('main');
colorGeneratorButton.classList.add('btn');
hexCodeHeading.classList.add('hexcode-heading');
colorGeneratorButton.textContent = 'Next Color';
main.append(hexCodeHeading, colorGeneratorButton);
document.body.append(main);
const getRandomInteger = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1));
};
const system = {
0: 0,
1 : 1,
2 : 2,
3 : 3,
4 : 4,
5 : 5,
6 : 6,
7 : 7,
8 : 8,
9 : 9,
10 : "A",
11 : "B",
12 : "C",
13 : "D",
14 : "E",
15 : "F",
}
const rgbToHex = x => {
character = ((x / 16).toFixed(2)).toLocaleString().split('.');
firstCode = Number(character[0]), secondCode = (Number(`.${character[1]}`))*16;
return `${system[firstCode]}${system[secondCode.toFixed(0)]}`;
};
//ADD EVENT LISTENER TO THE BUTTON
const changeColor = () => {
let redColor = rgbToHex(getRandomInteger(1,255)),
greenColor = rgbToHex(getRandomInteger(1,255)),
blueColor = rgbToHex(getRandomInteger(1,255));
hexCode = `#${redColor}${greenColor}${blueColor}`;
document.body.style.backgroundColor = hexCode;
hexCodeHeading.textContent = hexCode;
};
colorGeneratorButton.addEventListener('click', changeColor);
Now we will just make the web app create a better user experience. So we have to tweak the javascript a bit more to add color dynamics to the heading and button elements. So if the background color is darker, then the heading and button would be a lighter color that is complementary and vice versa.
We will use the HSP(http://alienryderflex.com/hsp.html) system. In summary, it uses 3 floating point numbers that represent effectiveness from a human's perspective in regards to the brightness of the color. A few mathematical operations will determine whether to use a dark or light shade.
This is the pseudocode for what we want to do:
So now let’s build our function. We’ll name it lightOrDark. The first step is to convert the hex color code to RGB form. Which we will also create an extra function for.
In order to convert the hex color code to rgb form, we take every two characters, multiply the first half by 16 and add it to the second half. For Example, take #4A18E2. We take 4A, 18, E2. 4 * 16 = 64 + 8 is 74. That would be the red color in RGB. And we continue that process two more times.The pseudocode in regards to the converter would be:
This is the converter function:
const hexToRgb = (x) => {
splitInput = x.split('');
firstChar = splitInput[0], secondChar = splitInput[1];
for(let key in system) {
if(system[key] == firstChar) firstChar = Number(key);
if(system[key] == secondChar) secondChar = Number(key);
};
return (firstChar * 16) + secondChar;
}
This is the light or dark function:
const lightOrDark = (red, green, blue) => {
red = hexToRgb(red);
green = hexToRgb(green);
blue = hexToRgb(blue);
hsp = Math.sqrt(
.299 * (red * red) +
.587 * (green * green) +
.114 * (blue * blue));
if(hsp > 127.5) return 'light'
else return 'dark';
};
These functions should be placed before our primary "changeColor" function. Now we just tweak our change color function to consider the light or dark function.
This is now what our change color function will now look like:
const changeColor = () => {
let redColor = rgbToHex(getRandomInteger(1,255)),
greenColor = rgbToHex(getRandomInteger(1,255)),
blueColor = rgbToHex(getRandomInteger(1,255));
hexCode = `#${redColor}${greenColor}${blueColor}`;
document.body.style.backgroundColor = hexCode;
hexCodeHeading.textContent = hexCode;
let colorType = lightOrDark(redColor, greenColor, blueColor);
if(colorType == 'light') {
hexCodeHeading.style.color = '#000000';
colorGeneratorButton.style.backgroundColor = '#000000';
colorGeneratorButton.style.color = '#FFFFFF';
colorGeneratorButton.style.borderColor = '#000000'
}
else {
hexCodeHeading.style.color = '#FFFFFF';
colorGeneratorButton.style.backgroundColor = '#FFFFFF';
colorGeneratorButton.style.color = '#214469';
colorGeneratorButton.style.borderColor = '#FFFFFF'
}
};
The contrast showing now is excellent for light background colors as well as dark background colors.
And to finish things off, in this section, we will just style the elements for a better user design.
The next block of code will do the following:
*{
box-sizing: border-box;
margin:0;
padding:0;
}
@import url('https://fonts.googleapis.com/css2?family=Anton&display=swap');
html{
font-family: 'Anton', sans-serif;
};
body{
display:flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: auto;
};
h1{
font-size:128px;
};
button{
height:40px;
width:128px;
font-size:22px;
border: 2px solid;
font-weight:bold;
cursor: pointer;
};
main{
display:flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
};
And here it is, the end of this article. An explanation/tutorial on how to create a random color generator with Javascript. Project-based learning, where the developer gets out there and puts the knowledge to use, is the most significant way to learn, in my opinion at least. This project provides that necessary challenge to put into practice the use of DOM, Events, and a couple of bonus topics we needed to finish the color generator.
Check out the live preview of the finished project and its source code over at my codepen.
Any questions or feedback? Please don't hesitate to reach out to me via my website or Twitter.