Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
Design Joke Generator App in HTML CSS & JavaScript
Next article icon

Design Random Color Generator using HTML CSS and JavaScript

Last Updated : 08 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A Random Color Generator app is used to create random colors with a simple button click, displaying both HEX and RGB codes that users can copy. It's built using HTML, CSS, and JavaScript, making it a handy tool for designers or developers who need quick color ideas. The app can also include a Dark Mode feature to switch between light and dark themes for easier viewing.

Approach to Design a Random Color Generator

Here’s a step-by-step approach for building this Random Color Generator using HTML, CSS, and JavaScript:

  • HTML Layout:
    Create a basic interface with a title, a color display box, and buttons to generate random colors, copy HEX/RGB codes, and toggle between Light and Dark modes.
  • CSS Design:
    Use CSS to center the layout on the page. Style the color box to show the generated color clearly. Add a Dark Mode feature by changing the background and text colors when toggled.
  • Generating Random Colors:
    Use the genColorFn() function in JavaScript to generate a random HEX color, convert it to RGB, and update the display with the new color and its corresponding codes.
  • Copying Color Codes:
    Implement the cpyFn() function to allow users to copy either the HEX or RGB color code to the clipboard, along with an animation confirming the successful copy action.
  • Dark Mode Switch:
    The darkFn() function toggles Dark Mode by switching the background and text colors, providing a more comfortable viewing option in darker environments.
HTML
<!DOCTYPE html> <html>  <head>     <link rel="stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css">     <link rel="stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">     <link href= "https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap"            rel="stylesheet">     <link rel="stylesheet" href="styles.css"> </head>  <body>     <div class="container">         <h1 class="app-title">GeeksforGeeks Random Color Generator</h1>          <div id="colorDisplay"               class="color-card animate__animated animate__fadeIn">             <p id="colorHex">#FFFFFF</p>             <p id="colorRgb">RGB(255, 255, 255)</p>         </div>          <div class="buttons">             <button onclick="genColorFn()">                 <i class="fas fa-random"></i>                  Generate Color</button>             <button onclick="cpyFn('colorHex')">                 <i class="far fa-copy"></i>                  Copy HEX</button>             <button onclick="cpyFn('colorRgb')">                 <i class="far fa-copy"></i>                  Copy RGB</button>             <button onclick="darkFn()">                 <i class="fas fa-adjust"></i>                  Dark Mode</button>         </div>          <p id="copyMessage" class="animate__animated"></p>     </div>      <script src="app.js"></script>     <script>         document.addEventListener('DOMContentLoaded', genColorFn);     </script> </body>  </html> 
CSS
/* Write CSS Here */body {     font-family: 'Montserrat', sans-serif;     text-align: center;     padding: 20px;     transition: background-color 0.3s ease-in-out; }  .container {     max-width: 400px;     margin: auto; }  .app-title {     margin-bottom: 20px; }  .color-card {     width: 200px;     height: 200px;     margin: 20px auto;     background-color: #ffffff;     border: 2px solid #000; }  .buttons button {     padding: 10px 20px;     margin: 10px;     cursor: pointer; }  #copyMessage {     margin-top: 10px;     font-size: 1.1rem; }  .dark-mode {     background-color: #333;     color: white; }  .light-mode {     background-color: white;     color: black; } 
JavaScript
// Function to generate random color function genColorFn() {     let randomColor = Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');     let hexColor = `#${randomColor}`;     let rgbColor = hexToRgb(hexColor);      // Update the displayed color values     document.getElementById('colorDisplay').style.backgroundColor = hexColor;     document.getElementById('colorHex').innerText = hexColor;     document.getElementById('colorRgb').innerText = `RGB(${rgbColor})`; }  // Function to convert HEX to RGB function hexToRgb(hex) {     let bigint = parseInt(hex.slice(1), 16);     let r = (bigint >> 16) & 255;     let g = (bigint >> 8) & 255;     let b = bigint & 255;     return `${r}, ${g}, ${b}`; }  // Function to copy color code function cpyFn(type) {     let copyText = document.getElementById(type).innerText;     navigator.clipboard.writeText(copyText).then(() => {         // Display a message for successful copy         let copyMessage = document.getElementById('copyMessage');         copyMessage.innerText = `${type === 'colorHex' ? 'HEX' : 'RGB'} copied!`;         copyMessage.classList.add('animate__fadeIn');         setTimeout(() => {             copyMessage.classList.remove('animate__fadeIn');             copyMessage.innerText = '';         }, 2000);     }); }  // Function to toggle Dark Mode function darkFn() {     document.body.classList.toggle('dark-mode'); } 

Output :

Output


Next Article
Design Joke Generator App in HTML CSS & JavaScript

G

gpancomputer
Improve
Article Tags :
  • Project
  • Web Technologies
  • Dev Scripter
  • JavaScript-Projects
  • Dev Scripter 2024

Similar Reads

  • Random Quote Generator Using HTML, CSS and JavaScript
    A Random Quote Generator is capable of pulling quotes randomly from an API, a database, or simply from an array. We will be designing a Random Quote Generator from scratch using HTML, CSS, JavaScript, and type.fit API. The webpage displays a random quote from a collection and upon the click of a but
    8 min read
  • How to create RGB color generator using HTML CSS and JavaScript ?
    In this article, we will create a RGB color generator using HTML, CSS, and JavaScript. Using RGB color generator, we can construct all the colors from the combination of Red, Green, Blue colors. Each color is represented by the range of decimal numbers from 0 to 255 (256 levels for each color). So,
    3 min read
  • How to create a Color Generator using HTML CSS and JavaScript ?
    In this article, we will develop a Color Generator application using HTML, CSS, and JavaScript. In this application, we have created the Multi Format color generator. Users can select Colour Types like RGB, HEX, and CMYK. Use the sliders to customize the color and Inout field in case of HEX and colo
    6 min read
  • How to create Hex color generator using HTML CSS and JavaScript?
    Hex color codes are six-digit combinations representing the amount of red, green, and blue (RGB) in a color. These codes are essential in web design and development for specifying colors in HTML and CSS. The hex color generator provides the hex code for any selected color, making it a valuable tool
    2 min read
  • Design Joke Generator App in HTML CSS & JavaScript
    We will go to learn how can we create a Joke generator app using HTML, CSS, and JavaScript. We will also add a feature to copy the generated joke. We will use API to fetch the jokes and will show those jokes on the screen. PrerequisitesHTMLCSSJavaScriptApproachCreate the Joke Generator Application U
    3 min read
  • Number Sequences Generator using HTML CSS and JavaScript
    In this article, we are going to implement a number sequence generator based on HTML, CSS, and JavaScript. In this program, we have three different types of sequences: the Fibonacci Series, the Prime Number Series, and the Even Odd Number Series. Preview of final output: Let us have a look at how th
    5 min read
  • How to randomly change image color using HTML CSS and JavaScript ?
    In this article, we will create a webpage where we can change image color randomly by clicking on the image using HTML, CSS, and JavaScript. The image color will change randomly using the JavaScript Math random() function. Approach: First of all select your image using HTML <img> tag.In JavaSc
    2 min read
  • Random Image Generator using JavaScript
    In this article, we will learn how to generate random images in fixed intervals using only HTML, CSS, and JavaScript. Approach: The pictures which are going to be generated randomly on the website should be stored in the form of an array, these pictures are then displayed to the user within a regula
    4 min read
  • QR Code Generator using HTML, CSS and jQuery
    In this article, we will learn how to build a QR generator using HTML, CSS, and jQuery. A QR code generator is an application that stores any required textual data into a QR code which can be later scanned with a QR code scanner to reveal the stored information. This code generator consists of an in
    3 min read
  • Design Background color changer using HTML CSS and JavaScript
    Background color changer is a project which enables to change the background color of web pages with ease. There are color boxes on a web page when the user clicks on any one of them, then the resultant color will appear in the background of the web page. It makes web pages look attractive. File str
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences