mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-12-11 00:33:47 -06:00
Make text of light tags legible
This commit is contained in:
parent
5b7f1ef18a
commit
1e03eed6c0
@ -1,5 +1,9 @@
|
|||||||
.tag {
|
.tag {
|
||||||
color: #fff;
|
color: #fff;
|
||||||
|
|
||||||
|
&.light-generated-bg {
|
||||||
|
color: #000;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.tag:not(:last-child) {
|
.tag:not(:last-child) {
|
||||||
|
|||||||
@ -13,7 +13,7 @@ interface TagProps {
|
|||||||
|
|
||||||
const Tag: FC<TagProps> = ({ text, children, clearable, className = '', colorGenerator, onClick, onClose }) => (
|
const Tag: FC<TagProps> = ({ text, children, clearable, className = '', colorGenerator, onClick, onClose }) => (
|
||||||
<span
|
<span
|
||||||
className={`badge tag ${className}`}
|
className={`badge tag ${className} ${colorGenerator.isColorLightForKey(text) ? 'light-generated-bg' : ''}`}
|
||||||
style={{ backgroundColor: colorGenerator.getColorForKey(text), cursor: clearable || !onClick ? 'auto' : 'pointer' }}
|
style={{ backgroundColor: colorGenerator.getColorForKey(text), cursor: clearable || !onClick ? 'auto' : 'pointer' }}
|
||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
>
|
>
|
||||||
|
|||||||
@ -2,16 +2,21 @@ import { rangeOf } from '../utils';
|
|||||||
import LocalStorage from './LocalStorage';
|
import LocalStorage from './LocalStorage';
|
||||||
|
|
||||||
const HEX_COLOR_LENGTH = 6;
|
const HEX_COLOR_LENGTH = 6;
|
||||||
const { floor, random } = Math;
|
const { floor, random, sqrt, round } = Math;
|
||||||
const letters = '0123456789ABCDEF';
|
const letters = '0123456789ABCDEF';
|
||||||
const buildRandomColor = () => `#${rangeOf(HEX_COLOR_LENGTH, () => letters[floor(random() * letters.length)]).join('')}`;
|
const buildRandomColor = () => `#${rangeOf(HEX_COLOR_LENGTH, () => letters[floor(random() * letters.length)]).join('')}`;
|
||||||
const normalizeKey = (key: string) => key.toLowerCase().trim();
|
const normalizeKey = (key: string) => key.toLowerCase().trim();
|
||||||
|
const hexColorToRgbArray = (colorHex: string): number[] => (colorHex.match(/../g) || []).map(hex => parseInt(hex, 16) || 0);
|
||||||
|
// HSP by Darel Rex Finley https://alienryderflex.com/hsp.html
|
||||||
|
const perceivedLightness = (r: number = 0, g: number = 0, b: number = 0) => round(sqrt(0.299 * r ** 2 + 0.587 * g ** 2 + 0.114 * b ** 2));
|
||||||
|
|
||||||
export default class ColorGenerator {
|
export default class ColorGenerator {
|
||||||
private readonly colors: Record<string, string>;
|
private readonly colors: Record<string, string>;
|
||||||
|
private readonly lights: Record<string, boolean>;
|
||||||
|
|
||||||
public constructor(private readonly storage: LocalStorage) {
|
public constructor(private readonly storage: LocalStorage) {
|
||||||
this.colors = this.storage.get<Record<string, string>>('colors') ?? {};
|
this.colors = this.storage.get<Record<string, string>>('colors') ?? {};
|
||||||
|
this.lights = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly getColorForKey = (key: string) => {
|
public readonly getColorForKey = (key: string) => {
|
||||||
@ -34,4 +39,15 @@ export default class ColorGenerator {
|
|||||||
|
|
||||||
return color;
|
return color;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public readonly isColorLightForKey = (key: string) => {
|
||||||
|
const colorHex = this.getColorForKey(key).substring(1);
|
||||||
|
|
||||||
|
if (this.lights[colorHex] == undefined) {
|
||||||
|
const rgb = hexColorToRgbArray(colorHex);
|
||||||
|
this.lights[colorHex] = perceivedLightness(...rgb) >= 128;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.lights[colorHex];
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user