-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor repeated code (#110)
- Loading branch information
Showing
5 changed files
with
39 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@sardine/colour": patch | ||
--- | ||
|
||
Optimise bundle size by refactoring repeated code. Saved 0.02 kB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { expect, it } from "vitest"; | ||
import { calculateContrastRatio } from "./calculateContrastRatio"; | ||
|
||
it("should calculate the contrast ratio between two luminances", () => { | ||
expect(calculateContrastRatio(0.05780543019106723, 0.16288822420427432)).toBe( | ||
1.974, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Calculate the contrast ratio between two colours | ||
* @param luminance1 The luminance of the first colour | ||
* @param luminance2 The luminance of the second colour | ||
* @returns The contrast ratio between the two colours truncated to 3 decimal places | ||
*/ | ||
export function calculateContrastRatio( | ||
luminance1: number, | ||
luminance2: number, | ||
): number { | ||
// Determine which colour is lighter and which is darker | ||
const lighter = Math.max(luminance1, luminance2); | ||
const darker = Math.min(luminance1, luminance2); | ||
|
||
// Calculate the contrast ratio | ||
// The spec sets the ratio as (L1 + 0.05) / (L2 + 0.05) where L1 is the lighter colour and L2 is the darker colour | ||
// https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html | ||
const ratio = (lighter + 0.05) / (darker + 0.05); | ||
|
||
// Return the contrast ratio truncated to 3 decimal places | ||
return Math.floor(ratio * 1000) / 1000; | ||
} |