"use client"; import React, { useState } from "react"; import Navbar from "../../components/Navbar"; import Footer from "../../components/Footer"; import Button from "../../components/Button"; export default function RgbToHex() { const [loading, setLoading] = useState(false); const [hex, setHex] = useState(""); const convertToHex = async () => { setLoading(true); const red = (document.getElementById("red") as HTMLInputElement).value; const green = (document.getElementById("green") as HTMLInputElement).value; const blue = (document.getElementById("blue") as HTMLInputElement).value; try { const response = await fetch( process.env.backend_url + "/api/color-convert", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ red: red, green: green, blue: blue }), }, ); if (!response.ok) { return new Error(`Error: ${response.statusText}`); } const hex: string = await response.text(); setHex(hex); } catch (error) { console.error("Error while converting:", error); alert("Error while converting"); } finally { setLoading(false); } }; const clearInAndOutput = () => { setHex(""); const red = document.getElementById("red") as HTMLInputElement; const green = document.getElementById("green") as HTMLInputElement; const blue = document.getElementById("blue") as HTMLInputElement; red.value = ""; green.value = ""; blue.value = ""; }; const checkInput = (event: React.ChangeEvent) => { const color = (document.getElementById(event.target.id) as HTMLInputElement); const colorValue = +color.value; if (colorValue < 0 || colorValue > 255 ) { alert("Invalid input. Please enter a number between 0 and 255."); } if (colorValue < 0) { color.value = "0"; } else if (colorValue > 255) { color.value = "255"; } } return (

rgb-to-hex

{hex}
); }