mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-16 12:33:48 +00:00
Merge pull request #566 from acelaya-forks/feature/nil-optimization
Fixed unintended usage of false where only null or undefined should m…
This commit is contained in:
@@ -1,10 +1,14 @@
|
|||||||
|
import { isNil } from 'ramda';
|
||||||
import { rangeOf } from '../utils';
|
import { rangeOf } from '../utils';
|
||||||
import LocalStorage from './LocalStorage';
|
import LocalStorage from './LocalStorage';
|
||||||
|
|
||||||
const HEX_COLOR_LENGTH = 6;
|
const HEX_COLOR_LENGTH = 6;
|
||||||
|
const HEX_DIGITS = '0123456789ABCDEF';
|
||||||
|
const LIGHTNESS_BREAKPOINT = 128;
|
||||||
|
|
||||||
const { floor, random, sqrt, round } = Math;
|
const { floor, random, sqrt, round } = Math;
|
||||||
const letters = '0123456789ABCDEF';
|
const buildRandomColor = () =>
|
||||||
const buildRandomColor = () => `#${rangeOf(HEX_COLOR_LENGTH, () => letters[floor(random() * letters.length)]).join('')}`;
|
`#${rangeOf(HEX_COLOR_LENGTH, () => HEX_DIGITS[floor(random() * HEX_DIGITS.length)]).join('')}`;
|
||||||
const normalizeKey = (key: string) => key.toLowerCase().trim();
|
const normalizeKey = (key: string) => key.toLowerCase().trim();
|
||||||
const hexColorToRgbArray = (colorHex: string): number[] =>
|
const hexColorToRgbArray = (colorHex: string): number[] =>
|
||||||
(colorHex.match(/../g) ?? []).map((hex) => parseInt(hex, 16) || 0);
|
(colorHex.match(/../g) ?? []).map((hex) => parseInt(hex, 16) || 0);
|
||||||
@@ -44,10 +48,10 @@ export default class ColorGenerator {
|
|||||||
public readonly isColorLightForKey = (key: string): boolean => {
|
public readonly isColorLightForKey = (key: string): boolean => {
|
||||||
const colorHex = this.getColorForKey(key).substring(1);
|
const colorHex = this.getColorForKey(key).substring(1);
|
||||||
|
|
||||||
if (!this.lights[colorHex]) {
|
if (isNil(this.lights[colorHex])) {
|
||||||
const rgb = hexColorToRgbArray(colorHex);
|
const rgb = hexColorToRgbArray(colorHex);
|
||||||
|
|
||||||
this.lights[colorHex] = perceivedLightness(...rgb) >= 128;
|
this.lights[colorHex] = perceivedLightness(...rgb) >= LIGHTNESS_BREAKPOINT;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.lights[colorHex];
|
return this.lights[colorHex];
|
||||||
|
|||||||
Reference in New Issue
Block a user