Add tag-adding logic
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
46
src/utilities/color.ts
Normal file
46
src/utilities/color.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { darkTheme, lightTheme } from '../theme';
|
||||
|
||||
const getContrastColor = (hexColor: string) => {
|
||||
if (hexColor.startsWith('#')) {
|
||||
hexColor = hexColor.slice(1);
|
||||
}
|
||||
|
||||
const r = Number.parseInt(hexColor.slice(0, 2), 16);
|
||||
const g = Number.parseInt(hexColor.slice(2, 4), 16);
|
||||
const b = Number.parseInt(hexColor.slice(4, 6), 16);
|
||||
const brightness = (r * 299 + g * 587 + b * 114) / 1000;
|
||||
|
||||
return brightness > 128
|
||||
? lightTheme.colors.onSurface
|
||||
: darkTheme.colors.onSurface;
|
||||
};
|
||||
|
||||
const isHexColor = (color: string) => {
|
||||
return /^#([\da-f]{6})$/i.test(color);
|
||||
};
|
||||
|
||||
const isRgbColor = (color: string) => {
|
||||
return /^rgb\((\d{1,3}), ?(\d{1,3}), ?(\d{1,3})\)$/i.test(color);
|
||||
};
|
||||
|
||||
const isValidColor = (color: string) => {
|
||||
return isHexColor(color) || isRgbColor(color);
|
||||
};
|
||||
|
||||
const rgbToHex = (rgb: string) => {
|
||||
const [r, g, b] = rgb
|
||||
.replaceAll(/[^\d,]/g, '')
|
||||
.split(',')
|
||||
.map(value => Number.parseInt(value, 10));
|
||||
|
||||
return `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`;
|
||||
};
|
||||
|
||||
const generateRandomColor = () => {
|
||||
const r = Math.floor(Math.random() * 256).toString(16).padStart(2, '0');
|
||||
const g = Math.floor(Math.random() * 256).toString(16).padStart(2, '0');
|
||||
const b = Math.floor(Math.random() * 256).toString(16).padStart(2, '0');
|
||||
return `#${r}${g}${b}`;
|
||||
};
|
||||
|
||||
export { getContrastColor, isHexColor, isRgbColor, isValidColor, rgbToHex, generateRandomColor };
|
Reference in New Issue
Block a user