1import { common } from '@material-ui/core/colors';
2import { makeStyles } from '@material-ui/core/styles';
3import {
4 getContrastRatio,
5 darken,
6} from '@material-ui/core/styles/colorManipulator';
7import React from 'react';
8
9import { LabelFragment } from './fragments.generated';
10import { Color } from '../gqlTypes';
11
12// Minimum contrast between the background and the text color
13const contrastThreshold = 2.5;
14
15// Guess the text color based on the background color
16const getTextColor = (background: string) =>
17 getContrastRatio(background, common.white) >= contrastThreshold
18 ? common.white // White on dark backgrounds
19 : common.black; // And black on light ones
20
21const _rgb = (color: Color) =>
22 'rgb(' + color.R + ',' + color.G + ',' + color.B + ')';
23
24// Create a style object from the label RGB colors
25const createStyle = (color: Color) => ({
26 backgroundColor: _rgb(color),
27 color: getTextColor(_rgb(color)),
28 borderBottomColor: darken(_rgb(color), 0.2),
29});
30
31const useStyles = makeStyles(theme => ({
32 label: {
33 ...theme.typography.body1,
34 padding: '1px 6px 0.5px',
35 fontSize: '0.9em',
36 fontWeight: 500,
37 margin: '0.05em 1px calc(-1.5px + 0.05em)',
38 borderRadius: '3px',
39 display: 'inline-block',
40 borderBottom: 'solid 1.5px',
41 verticalAlign: 'bottom',
42 },
43}));
44
45type Props = { label: LabelFragment };
46function Label({ label }: Props) {
47 const classes = useStyles();
48 return (
49 <span className={classes.label} style={createStyle(label.color)}>
50 {label.name}
51 </span>
52 );
53}
54
55export default Label;