aboutsummaryrefslogtreecommitdiffstats
path: root/webui/src/Label.js
blob: fc8a3a221b8230f16303811c82accf065229efd2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React from 'react';
import gql from 'graphql-tag';
import { makeStyles } from '@material-ui/styles';
import {
  getContrastRatio,
  darken,
} from '@material-ui/core/styles/colorManipulator';
import { common } from '@material-ui/core/colors';

// Minimum contrast between the background and the text color
const contrastThreshold = 2.5;

// Guess the text color based on the background color
const getTextColor = background =>
  getContrastRatio(background, common.white) >= contrastThreshold
    ? common.white // White on dark backgrounds
    : common.black; // And black on light ones

const _rgb = color => 'rgb(' + color.R + ',' + color.G + ',' + color.B + ')';

// Create a style object from the label RGB colors
const createStyle = color => ({
  backgroundColor: _rgb(color),
  color: getTextColor(_rgb(color)),
  borderBottomColor: darken(_rgb(color), 0.2),
});

const useStyles = makeStyles(theme => ({
  label: {
    ...theme.typography.body2,
    padding: '0 6px',
    fontSize: '0.9em',
    margin: '0 1px',
    borderRadius: '3px',
    display: 'inline-block',
    borderBottom: 'solid 1.5px',
    verticalAlign: 'bottom',
  },
}));

function Label({ label }) {
  const classes = useStyles();
  return (
    <span className={classes.label} style={createStyle(label.color)}>
      {label.name}
    </span>
  );
}

Label.fragment = gql`
  fragment Label on Label {
    name
    color {
      R
      G
      B
    }
  }
`;

export default Label;