-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathuseContrastText.ts
155 lines (144 loc) · 3.79 KB
/
useContrastText.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import Color from 'tinycolor2';
import { useToken } from './useToken';
import { useAccessibleColors } from '../core/color-mode/hooks';
import { useNativeBaseConfig } from '../core/NativeBaseContext';
export function useContrastText(bg: string, color?: string) {
const [
contrastThreshold,
trueDarkText,
trueLightText,
trueBg,
trueColor,
] = useToken('colors', [
'contrastThreshold',
'darkText',
'lightText',
bg,
color ?? '',
]);
const suppressColorAccessibilityWarning = useNativeBaseConfig(
'NativeBaseConfigProvider'
).config.suppressColorAccessibilityWarning;
const [accessibleColors] = useAccessibleColors();
if (useNativeBaseConfig('NativeBaseConfigProvider').disableContrastText) {
return trueColor;
}
if (typeof bg !== 'string') {
return;
}
const [bgThemeColorVariant, bgShade] = bg.split('.');
const textColor =
!accessibleColors &&
bgThemeColorVariant &&
themeColorsThresholdShades[bgThemeColorVariant]
? getContrastThemeColor(bgThemeColorVariant, bgShade)
: getAccessibleContrastColor(
contrastThreshold,
trueDarkText,
trueLightText,
trueBg,
trueColor,
bg,
color,
suppressColorAccessibilityWarning
);
return textColor;
}
function getContrastThemeColor(bgThemeColorVariant: string, bgShade: string) {
const shadeThreshold = themeColorsThresholdShades[bgThemeColorVariant];
if (
bgShade &&
shadeThreshold &&
((bgShade >= shadeThreshold && bgThemeColorVariant !== 'dark') ||
(bgThemeColorVariant === 'dark' && bgShade < shadeThreshold))
) {
return 'lightText';
}
return 'darkText';
}
function getAccessibleContrastColor(
contrastThreshold: number,
trueDarkText: string,
trueLightText: string,
trueBg: string,
trueColor: string,
bg: string,
color?: string,
suppressColorAccessibilityWarning?: boolean
) {
if (typeof trueBg !== 'string') {
trueBg = bg;
}
let trueContrastColor;
let contrastColorToken;
const darkTextConstrast = getContrastRatio(trueBg, trueDarkText);
const lightTextConstrast = getContrastRatio(trueBg, trueLightText);
if (
darkTextConstrast >= contrastThreshold ||
darkTextConstrast > lightTextConstrast
) {
trueContrastColor = trueDarkText;
contrastColorToken = 'darkText';
} else {
trueContrastColor = trueLightText;
contrastColorToken = 'lightText';
}
if (process.env.NODE_ENV !== 'production') {
const contrast = getContrastRatio(
trueBg,
trueColor ? trueColor : trueContrastColor
);
if (contrast < 3 && !suppressColorAccessibilityWarning) {
console.warn(
[
`NativeBase: The contrast ratio of ${contrast}:1 for ${
color ? color : contrastColorToken
} on ${bg}`,
'falls below the WCAG recommended absolute minimum contrast ratio of 3:1.',
'https://www.w3.org/TR/2008/REC-WCAG20-20081211/#visual-audio-contrast-contrast',
].join('\n')
);
}
}
return contrastColorToken;
}
function getContrastRatio(foreground: string, background: string) {
const lumA = Color(foreground).getLuminance();
const lumB = Color(background).getLuminance();
return (Math.max(lumA, lumB) + 0.05) / (Math.min(lumA, lumB) + 0.05);
}
const themeColorsThresholdShades: any = {
rose: 500,
pink: 500,
fuchsia: 800,
purple: 700,
violet: 600,
indigo: 500,
blue: 400,
lightBlue: 400,
cyan: 300,
teal: 300,
emerald: 300,
tertiary: 300,
green: 400,
lime: 600,
yellow: 800,
amber: 500,
orange: 500,
red: 500,
warmGray: 500,
trueGray: 500,
gray: 500,
coolGray: 500,
blueGray: 500,
dark: 500,
danger: 500,
error: 500,
success: 400,
warning: 500,
muted: 500,
primary: 500,
info: 400,
secondary: 500,
light: 500,
};