werkzeugkiste/backend/src/routes/colorconvert.route.ts
2025-02-20 20:49:24 +01:00

30 lines
922 B
TypeScript

import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
interface RequestBody {
red: string;
green: string;
blue: string;
}
export async function colorConvert(app: FastifyInstance) {
app.post(
"/api/color-convert",
async (
request: FastifyRequest<{ Body: RequestBody }>,
reply: FastifyReply,
) => {
try {
const data = request.body;
if (!data) {
return reply.status(400).send({ error: "No RGB declared!" });
}
const hex =
`#${(+data.red).toString(16).padStart(2, "0")}${(+data.green).toString(16).padStart(2, "0")}${(+data.blue).toString(16).padStart(2, "0")}`.toUpperCase();
reply.header("Content-Type", "text/plain").status(200).send(hex);
} catch (error) {
console.error("Convert error:", error);
reply.status(500).send({ error: "Error while converting!" });
}
},
);
}