diff --git a/Client/src/Components/Label/index.css b/Client/src/Components/Label/index.css index 945ab62d1..e0c82b062 100644 --- a/Client/src/Components/Label/index.css +++ b/Client/src/Components/Label/index.css @@ -1,3 +1,5 @@ .label { border: 1px solid #000; + display: inline-flex; + justify-content: center; } diff --git a/Client/src/Components/Label/index.jsx b/Client/src/Components/Label/index.jsx index 0d9a75c39..921178c6a 100644 --- a/Client/src/Components/Label/index.jsx +++ b/Client/src/Components/Label/index.jsx @@ -2,38 +2,47 @@ import PropTypes from "prop-types"; import "./index.css"; import { useTheme } from "@mui/material"; +// Produces a lighter color based on a hex color and a percent +// lightenColor("#067647", 20) will produce a color 20% lighter than #067647 +const lightenColor = (color, percent) => { + let r = parseInt(color.substring(1, 3), 16); + let g = parseInt(color.substring(3, 5), 16); + let b = parseInt(color.substring(5, 7), 16); + + const amt = Math.round((255 * percent) / 100); + + r = r + amt <= 255 ? r + amt : 255; + g = g + amt <= 255 ? g + amt : 255; + b = b + amt <= 255 ? b + amt : 255; + + r = r.toString(16).padStart(2, "0"); + g = g.toString(16).padStart(2, "0"); + b = b.toString(16).padStart(2, "0"); + + return `#${r}${g}${b}`; +}; + /** * @typedef {Object} Props * @property {string} label - The label of the button + * @property {string} color - The base color of the label */ const Label = ({ label, color }) => { const theme = useTheme(); - const config = { - orange: { - borderColor: theme.palette.labelOrange.borderColor, - bgColor: theme.palette.labelOrange.bgColor, - textColor: theme.palette.labelOrange.textColor, - }, - gray: { - borderColor: theme.palette.labelGray.borderColor, - bgColor: theme.palette.labelGray.bgColor, - textColor: theme.palette.labelGray.textColor, - }, - purple: { - borderColor: theme.palette.labelPurple.borderColor, - bgColor: theme.palette.labelPurple.bgColor, - textColor: theme.palette.labelPurple.textColor, - }, - green: { - borderColor: theme.palette.labelGreen.borderColor, - bgColor: theme.palette.labelGreen.bgColor, - textColor: theme.palette.labelGreen.textColor, - }, - }; + // If an invalid color is passed, default to the labelGray color + if ( + typeof color !== "string" || + !/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(color) + ) { + color = theme.palette.labelGray.textColor; + console.log(color); + } - const { borderColor, bgColor, textColor } = config[color]; + // Calculate lighter shades for border and bg + const borderColor = lightenColor(color, 20); + const bgColor = lightenColor(color, 75); const { borderRadius } = theme.shape; return (