Skip to content

Commit

Permalink
chore: Refactor repeated code (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marabyte authored Oct 28, 2024
1 parent e6628b2 commit 4c803cc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-numbers-grab.md
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
13 changes: 2 additions & 11 deletions src/getContrastRatioFromCSSRGB.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { convertCSSRGBtoRGB } from "./convertCSSRGBtoRGB";
import { getSRGBLuminanceFromRGB } from "./getSRGBLuminanceFromRGB";
import type { WCAG } from "./types";
import { calculateContrastRatio } from "./util/calculateContrastRatio";

/**
* Calculates the contrast ratio between two colours in CSSRGB format.
Expand All @@ -20,15 +21,5 @@ export function getContrastRatioFromCSSRGB(
const luminance1 = getSRGBLuminanceFromRGB(rgbColour1, standard);
const luminance2 = getSRGBLuminanceFromRGB(rgbColour2, standard);

// 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;
return calculateContrastRatio(luminance1, luminance2);
}
13 changes: 2 additions & 11 deletions src/getContrastRatioFromHex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getSRGBLuminanceFromHex } from "./getSRGBLuminanceFromHex";
import type { WCAG } from "./types";
import { calculateContrastRatio } from "./util/calculateContrastRatio";

/**
* Calculates the contrast ratio between two colours in hexadecimal format.
Expand All @@ -17,15 +18,5 @@ export function getContrastRatioFromHex(
const luminance1 = getSRGBLuminanceFromHex(colour1, standard);
const luminance2 = getSRGBLuminanceFromHex(colour2, standard);

// 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;
return calculateContrastRatio(luminance1, luminance2);
}
8 changes: 8 additions & 0 deletions src/util/calculateContrastRatio.test.ts
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,
);
});
22 changes: 22 additions & 0 deletions src/util/calculateContrastRatio.ts
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;
}

0 comments on commit 4c803cc

Please sign in to comment.