Skip to content

Instantly share code, notes, and snippets.

@Steellgold
Created November 26, 2023 15:37
Show Gist options
  • Save Steellgold/de49ed7dd36f4d2dab16a2ae835ee99f to your computer and use it in GitHub Desktop.
Save Steellgold/de49ed7dd36f4d2dab16a2ae835ee99f to your computer and use it in GitHub Desktop.
Your text need to be dark or white?
const textIsWhite = (color: string): boolean => {
const hex = color.replace("#", "");
const c_r = parseInt(hex.slice(0, 2), 16);
const c_g = parseInt(hex.slice(2, 4), 16);
const c_b = parseInt(hex.slice(4, 6), 16);
const brightness = ((c_r * 299) + (c_g * 587) + (c_b * 114)) / 1000;
return brightness < 155;
};
export const Role: Component<RoleProps> = ({ name, color }) => {
return (
<Badge style={{ backgroundColor: color, color: textIsWhite(color) ? "white" : "black" }}>
{name}
</Badge>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment