aboutsummaryrefslogtreecommitdiffstats
path: root/webui/src/Label.js
diff options
context:
space:
mode:
authorAmine Hilaly <hilalyamine@gmail.com>2019-05-16 20:46:30 +0200
committerQuentin Gliech <quentingliech@gmail.com>2019-05-22 20:38:00 +0200
commit511ef0105cbc6a08298cd63320283bf41090d4e3 (patch)
tree665a66ebb6aca07a7e82364161263f10b8075e33 /webui/src/Label.js
parentaf8216cf35855c39787c920316308a313ceea2c2 (diff)
downloadgit-bug-511ef0105cbc6a08298cd63320283bf41090d4e3.tar.gz
Webui: use grahql response to create labels colors
Diffstat (limited to 'webui/src/Label.js')
-rw-r--r--webui/src/Label.js36
1 files changed, 9 insertions, 27 deletions
diff --git a/webui/src/Label.js b/webui/src/Label.js
index 826d21f5..de2ccd61 100644
--- a/webui/src/Label.js
+++ b/webui/src/Label.js
@@ -4,43 +4,25 @@ import {
getContrastRatio,
darken,
} from '@material-ui/core/styles/colorManipulator';
-import * as allColors from '@material-ui/core/colors';
import { common } from '@material-ui/core/colors';
-// JS's modulo returns negative numbers sometimes.
-// This ensures the result is positive.
-const mod = (n, m) => ((n % m) + m) % m;
-
// Minimum contrast between the background and the text color
const contrastThreshold = 2.5;
-// Filter out the "common" color
-const labelColors = Object.entries(allColors)
- .filter(([key, value]) => value !== common)
- .map(([key, value]) => value);
-
-// Generate a hash (number) from a string
-const hash = string =>
- string.split('').reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0);
-
-// Get the background color from the label
-const getColor = label =>
- labelColors[mod(hash(label), labelColors.length)][500];
-
// 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 _genStyle = background => ({
- backgroundColor: background,
- color: getTextColor(background),
- borderBottomColor: darken(background, 0.2),
-});
+const _rgb = color => 'rgb(' + color.R + ',' + color.G + ',' + color.B + ')';
-// Generate a style object (text, background and border colors) from the label
-const genStyle = label => _genStyle(getColor(label));
+// 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: {
@@ -58,8 +40,8 @@ const useStyles = makeStyles(theme => ({
function Label({ label }) {
const classes = useStyles();
return (
- <span className={classes.label} style={genStyle(label)}>
- {label}
+ <span className={classes.label} style={createStyle(label.color)}>
+ {label.name}
</span>
);
}