diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-10-05 14:45:21 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-10-05 14:45:21 +0200 |
| commit | b7e39b59fd0fc7b5610ebff29035bf622079e0d8 (patch) | |
| tree | 64be84ebbdac9f7ceced983390c53b10d575af5c /old-apps/web-shared/src/lib/colors.ts | |
| parent | 2001c035fbb417ab0a3d42cfb04d17420bde4086 (diff) | |
| download | greatoffice-b7e39b59fd0fc7b5610ebff29035bf622079e0d8.tar.xz greatoffice-b7e39b59fd0fc7b5610ebff29035bf622079e0d8.zip | |
refactor: Change file structure
Diffstat (limited to 'old-apps/web-shared/src/lib/colors.ts')
| -rw-r--r-- | old-apps/web-shared/src/lib/colors.ts | 47 |
1 files changed, 0 insertions, 47 deletions
diff --git a/old-apps/web-shared/src/lib/colors.ts b/old-apps/web-shared/src/lib/colors.ts deleted file mode 100644 index c2da03d..0000000 --- a/old-apps/web-shared/src/lib/colors.ts +++ /dev/null @@ -1,47 +0,0 @@ -export function generate_random_hex_color(skip_contrast_check = false) { - let hex = __generate_random_hex_color(); - if (skip_contrast_check) return hex; - while ((__calculate_contrast_ratio("#ffffff", hex) < 4.5) || (__calculate_contrast_ratio("#000000", hex) < 4.5)) { - hex = __generate_random_hex_color(); - } - - return hex; -} - -// Largely copied from chroma js api -function __generate_random_hex_color(): string { - let code = "#"; - for (let i = 0; i < 6; i++) { - code += "0123456789abcdef".charAt(Math.floor(Math.random() * 16)); - } - return code; -} - -function __calculate_contrast_ratio(hex1: string, hex2: string): number { - const rgb1 = __hex_to_rgb(hex1); - const rgb2 = __hex_to_rgb(hex2); - const l1 = __get_luminance(rgb1[0], rgb1[1], rgb1[2]); - const l2 = __get_luminance(rgb2[0], rgb2[1], rgb2[2]); - const result = l1 > l2 ? (l1 + 0.05) / (l2 + 0.05) : (l2 + 0.05) / (l1 + 0.05); - return result; -} - -function __hex_to_rgb(hex: string): number[] { - if (!hex.match(/^#([A-Fa-f0-9]{6})$/)) return []; - if (hex[0] === "#") hex = hex.substring(1, hex.length); - return [parseInt(hex.substring(0, 2), 16), parseInt(hex.substring(2, 4), 16), parseInt(hex.substring(4, 6), 16)]; -} - -function __get_luminance(r, g, b) { - // relative luminance - // see http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef - r = __luminance_x(r); - g = __luminance_x(g); - b = __luminance_x(b); - return 0.2126 * r + 0.7152 * g + 0.0722 * b; -} - -function __luminance_x(x) { - x /= 255; - return x <= 0.03928 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4); -} |
