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