1import React from 'react';
2
3import { Chip } from '@material-ui/core';
4import { common } from '@material-ui/core/colors';
5import {
6 darken,
7 getContrastRatio,
8} from '@material-ui/core/styles/colorManipulator';
9
10import { Color } from '../gqlTypes';
11
12import { LabelFragment } from './fragments.generated';
13
14const _rgb = (color: Color) =>
15 'rgb(' + color.R + ',' + color.G + ',' + color.B + ')';
16
17// Minimum contrast between the background and the text color
18const contrastThreshold = 2.5;
19// Guess the text color based on the background color
20const getTextColor = (background: string) =>
21 getContrastRatio(background, common.white) >= contrastThreshold
22 ? common.white // White on dark backgrounds
23 : common.black; // And black on light ones
24
25// Create a style object from the label RGB colors
26const createStyle = (color: Color) => ({
27 backgroundColor: _rgb(color),
28 color: getTextColor(_rgb(color)),
29 borderBottomColor: darken(_rgb(color), 0.2),
30 margin: '3px',
31});
32
33type Props = { label: LabelFragment };
34function Label({ label }: Props) {
35 return (
36 <Chip
37 size={'small'}
38 label={label.name}
39 style={createStyle(label.color)}
40 ></Chip>
41 );
42}
43export default Label;