A simple Hex Color Generator that:
✅ Changes the background color randomly on button click.
✅ Displays the HEX code of the generated color.
✅ Allows users to copy the HEX code with a button.
✅ Includes smooth animations for UI feedback.
- HTML – Page structure.
- CSS – Styling and animations.
- JavaScript – Logic and event handling.
This function generates a random HEX color and updates the background.
🔹 Breaking Down the HEX Logic
Math.random() * 1677716
Math.random() generates a number between 0 and 1 (e.g., 0.234, 0.897).
Multiplying by 1677716 ensures the range is from 0 to 1677715.
Why 1677716?
HEX codes use #RRGGBB, and max hexadecimal (FFFFFF) = 16777215 (decimal).
If you use 16777215, it covers full RGB, but 1677716 slightly reduces the range.
🔹 Convert to HEX
.toString(16)
Converts decimal to hexadecimal.
Example: 255..toString(16) → "ff"
If you use .toString(4), it converts to base 4, which isn’t valid for colors.
🔹 Ensure 6 Digits
.padStart(6, "0")
If the random number gives a shorter hex (like #A4F), it adds extra 0s to make it 6 digits.
---
2️⃣ copyToClipboard() - Copy HEX Code
This function copies the generated HEX code to the clipboard and provides visual feedback.
Uses navigator.clipboard.writeText() to copy the text.
Changes the button color to indicate success.
Uses setTimeout() to reset the button after 1 second.
---
3️⃣ .toString(base) - Base Conversions
.toString(base) converts numbers to different bases.
---
🎯 Features Summary
✔ Random HEX color generation
✔ Copy HEX code with one click
✔ Smooth animations & button feedback
✔ Fully responsive for all devices
---
📌 How to Use?
1️⃣ Click the "Click me" button to generate a new HEX color.
2️⃣ Click "Copy" to copy the color code.
3️⃣ Background and color code updates instantly.
---
📝 Future Enhancements
🚀 Add RGB to HEX conversion feature.
🚀 Implement gradient color generation.
🚀 Dark mode support.
---
📜 License
This project is free to use and open-source. Feel free to modify and improve it! 😃
---
🔗 Connect With Me
GitHub: https://github.com/urnjoya
Dynamic Browser Run: https://urnjoya.github.io/colorpanel/