Random color generator in JavaScript16 Feb 2025 | 6 min read Colors are an integral part of the look and feel of the website. Whether it might be online or offline, colors make the websites look beautiful and appealing. In JavaScript and HTML elements, color is applied to the background, text, and other elements. Colors are most important because there are several websites designed solely based on color palettes and gradients. Now in this article, let us see how to generate random colors using different ways in JavaScript. Color RepresentationIn CSS, colors can be represented with their direct numbers, formatted like RGB (Red, Blue, Green) colors. And also sometimes represented by the hexadecimal values and HSL-specific notations. So, the types in which colors are represented are:
Math.random() MethodTo generate random numbers, in JavaScript we use Math.random() method function. The function is mainly responsible for the randomly generating of numbers which are pseudo floating point numbers. These pseudo floating point numbers are less than 1 and greater than 0. We can generate numbers in two cases like from 0 to 100 or from 100 to 255. Generating Random ColorsNow let's generate random numbers which are then represented in the color format using JavaScript and also using different techniques available with CSS. Let's see the top three scenarios where we can implement the generation of random colors. Increasing any number by 0 will give us 0, and duplicating it by 1 will give us a similar number back. So duplicating our ideal most extreme worth - for instance 255 - by anything somewhere in the range of 0 and 1 will give us a number somewhere in the range of 0 and 255. Notwithstanding, since Math.random() never gives us 1 precisely, we should utilize another stunt to get 255. This should be possible by inside expanding the most extreme breaking point by 1. 1. Hexa-Decimal ValuesFor generating random color using Hexa decimal values, we can make use of Math.random() and Math.floor() for getting the integer values in the range between 0 and 255. Then the random values are generated for colors red, green, blue, and other components. We then can convert these integer values to the two-digit hexadecimal string type such that they are the two digits. Then with the process of concatenation, the two string type hexadecimal are concatenated to form the color code and will be returned as a random color. Example: Code: Output: ![]() 2. RGB ColorIn RGB values, the RGB indicates the value of colors Red, Green, and Blue. And, the value will be ranged from 0 to 255. Generating in RGB is the same as generating for a hexadecimal method for green, red, and blue components. Here the method used for it is template literals which are constructed and returned as an RGB color string. Example: Code: Output: ![]() 3. HSL ColorThe notation of HSL consists of three numbers. The three numbers determine three different colors. The first value determines the hue of the color determined by an angular degree value. The second value is a specific percentage of the saturation of the color and the third one is simply the percentage of the lightness of the color. Here the values of the percentages range from 0 to 100 and the angular degree specifically ranges to 360 from 0. In our below logic, strings are constructed and an HSL color string is returned using the template literals. Example: Code: Output: ![]() Now let's see the final implementation of the random color generator in JavaScript using HTML and CSS also. Here, in the below mentioned logic, the random colors are generated using Math.floor function() and using of one of the above techniques. That is using hexadecimal color generation. LogicBy using the above logic, and also HTML tags we will implement the full code in the below program. CSS styles are also added for the visual appeal of the overall look and feel of the random color generator. Full Code ImplementationCode Output: ![]() Conclusion:This is how we generate random colors in JavaScript using different strategies like hexadecimal values, RGB color schemes, and HSL color notations. All the methods are easy to implement and provide practice of CSS and an understanding of the importance of the visual appearance of a website. The important takeaway from the random color generator is knowing about how Math.random() function works and how different floating-point values are converted into string type literals such that the color is generated is a crucial part of the exercise. Next TopicJS setTimeout() method |
JavaScript, often simply called 'Javascript' or 'JS', is one of the web languages that includes HTML and CSS. This versatility gives developers the power to create dynamic websites, server-side apps, and mobile or desktop apps. So, if you are an aspiring developer in the tech world,...
6 min read
Recursion in JavaScript is a programming method wherein a function calls itself at once or indirectly to resolve a problem. Recursion is an effective concept that helps function to be defined in terms of themselves which allows elegant solutions to certain styles of issues. There are some...
7 min read
JavaScript is a flexible programming language that is mainly used for online development, but it can also be used with simple programs like Notepad. Even though advanced Integrated Development Environments (IDEs) are readily available, learning the foundations of coding in a simple setting promotes a...
6 min read
JavaScript developers may also produce dynamic, interactive web pages. Of all its functions, the event-pushed version is one of the maximum essential ones. JavaScript activities talk to specific activities or activities, which include user engagements (clicks, scrolls, form submissions) or browser nation changes (loading, resizing, etc.)....
11 min read
A key component of web development is JavaScript which is well-known for its adaptability and dynamic quality. Understanding the pass by value and pass by reference procedures that are utilized to pass variables across functions is a basic component of programming in JavaScript. Developers may...
6 min read
What is Discord? Discord is a free app that allows users to communicate with others in real-time using text, voice, or video chat. It is a popular way for people with similar interests to connect online and is especially popular among the gamers. It is available on many...
6 min read
The visibility property in JavaScript Style lets users show or hide an element. This element's visibility on the webpage is defined by its visibility property. Similar to visibility, another JavaScript property called hidden can be used to hide an element without removing the space it currently...
6 min read
What is JavaScript? It is a programming language that is used to add interactivity, animation, and dynamic content to websites. or we can say JavaScript is a scripting language that is run on the user's web browser rather than on the web server. JavaScript is also used so...
6 min read
In JavaScript, typecasting is the process of converting the value of one data type into another data type. For example, converting a data type number to a string. What is the data type? It is a type of data that a variable can hold. There are eight main data...
8 min read
The offsetHeight is an HTML DOM property, which is used by JavaScript programming language. It returns the visible height of an element in pixels that includes the height of visible content, border, padding, and scrollbar if present. The offsetHeight is often used with offsetWidth property....
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India