Facebook
From Sazzad, 2 Weeks ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 122
  1. import { useState } from "react";
  2.  
  3. export default function RandomColor() {
  4.   const [typeofColor, setTypeOfColor] = useState("hex");
  5.   const [color, setColor] = useState("#0000ff");
  6.  
  7.   function createRandomHexColor() {
  8.     const hex = [0,1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"];
  9.     let hexColor = "#";
  10.     for (let i = 0; i < 6; i++) {
  11.       hexColor = hexColor + hex[Math.floor(Math.random() * hex.length)];
  12.     }
  13.    
  14.     setColor(hexColor);
  15.     console.log(color);
  16.   }
  17.  
  18.   function createRandomRgbColor() {}
  19.  
  20.   return (
  21.     <div
  22.       className={`w-[700px] h-[700px] bg-[${color}] flex justify-center items-start gap-4`}
  23.     >
  24.       <button  => setTypeOfColor("hex")} className="bg-white">
  25.         Create Hex Color
  26.       </button>
  27.       <button  => setTypeOfColor("rgb")} className="bg-white">
  28.         Create RGB Color
  29.       </button>
  30.       <button
  31.          
  32.           typeofColor === "hex" ? createRandomHexColor : createRandomRgbColor
  33.         }
  34.         className="bg-white"
  35.       >
  36.         Generate Random Color
  37.       </button>
  38.     </div>
  39.   );
  40. }
  41.