-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgetContrast.ts
More file actions
26 lines (23 loc) · 791 Bytes
/
Copy pathgetContrast.ts
File metadata and controls
26 lines (23 loc) · 791 Bytes
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
// taken from:
// https://github.com/styled-components/polished/blob/0764c982551b487469043acb56281b0358b3107b/src/color/getContrast.js
import getLuminance from './getLuminance';
/**
* Returns the contrast ratio between two colors based on the WCAG contrast
* ratio formula.
*
* ```js
* getContrast('#444', '#fff'); // 9.739769120526205
* ```
*
* @param color1 The first color.
* @param color2 The second color.
* @returns The contrast ratio between the two colors.
*/
function getContrast(color1: string, color2: string): number {
const luminance1 = getLuminance(color1);
const luminance2 = getLuminance(color2);
return luminance1 > luminance2
? (luminance1 + 0.05) / (luminance2 + 0.05)
: (luminance2 + 0.05) / (luminance1 + 0.05);
}
export default getContrast;