Added color lightening function, removed hardcoded colors from theme

This commit is contained in:
Alex Holliday committed 2024-05-07 11:44:27 -07:00
1 parent 76e34149e9
commit 6bd8fec119
4 files changed
+48 -51

No files matched your search

+2
View File
@@ -1,3 +1,5 @@
.label {
border: 1px solid #000;
display: inline-flex;
justify-content: center;
}
+34 -24
View File
@@ -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 (
<div
@@ -42,7 +51,8 @@ const Label = ({ label, color }) => {
borderRadius: borderRadius,
borderColor: borderColor,
backgroundColor: bgColor,
color: textColor,
color: color,
padding: theme.spacing(1 * 0.75, 2),
}}
>
{label}