Skip to content

Commit

Permalink
feat: allow custom bg color in hasBadContrast
Browse files Browse the repository at this point in the history
Co-authored-by: Novolokov <[email protected]>

Squashed commit of the following:

commit 32afa52
Merge: d0b4620 37af5b7
Author: Rico Kahler <[email protected]>
Date:   Sun Feb 13 19:41:05 2022 -0500

    Merge branch 'main' into patch-1

commit d0b4620
Author: Novolokov <[email protected]>
Date:   Sat Dec 4 13:04:24 2021 +0100

    allow custom background color
  • Loading branch information
ricokahler committed Feb 14, 2022
1 parent 31b79a1 commit d6bcddf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/hasBadContrast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ const guidelines = {
};

/**
* Returns whether or not a color has bad contrast against
* a white background according to a given standard
* Returns whether or not a color has bad contrast against a background
* according to a given standard.
*/
function hasBadContrast(
color: string,
standard: 'decorative' | 'readable' | 'aa' | 'aaa' = 'aa'
standard: 'decorative' | 'readable' | 'aa' | 'aaa' = 'aa',
background: string = '#fff'
): boolean {
return getContrast(color, '#fff') < guidelines[standard];
return getContrast(color, background) < guidelines[standard];
}

export default hasBadContrast;
8 changes: 6 additions & 2 deletions website/getDocInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ test('all of them', async () => {
"returnType": "number",
},
Object {
"description": "<p>Returns whether or not a color has bad contrast against
a white background according to a given standard</p>",
"description": "<p>Returns whether or not a color has bad contrast against a background
according to a given standard.</p>",
"functionName": "hasBadContrast",
"id": "has-bad-contrast",
"params": Array [
Expand All @@ -203,6 +203,10 @@ test('all of them', async () => {
"name": "standard",
"type": "'decorative' | 'readable' | 'aa' | 'aaa' = 'aa'",
},
Object {
"name": "background",
"type": "string = '#fff'",
},
],
"returnType": "boolean",
},
Expand Down

0 comments on commit d6bcddf

Please sign in to comment.