-
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.
feat: Add more utility functions (#32)
* feat: Add isCSSRGBColour * feat: Increase coverage of CSS RGB formats * feat: Add isHexColour #24 * feat: Add isCSSNamedColourDarkColour #25 * feat: Add isCSSRGBDarkColour #22
- Loading branch information
Showing
19 changed files
with
259 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": minor | ||
--- | ||
|
||
feat: Add isHexColour |
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": minor | ||
--- | ||
|
||
feat: Increase coverage of different CSS RGB formats |
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": minor | ||
--- | ||
|
||
feat: Add isCSSRGBDarkColour |
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": minor | ||
--- | ||
|
||
Add isCSSRGBColour |
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": minor | ||
--- | ||
|
||
feat: Add isCSSNamedDarkColour |
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,16 @@ | ||
import { | ||
cssRGBARegex, | ||
hexRegex, | ||
hexAlphaRegex, | ||
shortAlphaHexRegex, | ||
shortHexRegex, | ||
} from "./util/regexers.js"; | ||
|
||
export const isCSSRGBColour = (colour: string): boolean => | ||
!!colour.match(cssRGBARegex); | ||
|
||
export const isHexColour = (colour: string) => | ||
!!colour.match(hexRegex) || | ||
!!colour.match(hexAlphaRegex) || | ||
!!colour.match(shortAlphaHexRegex) || | ||
!!colour.match(shortHexRegex); |
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,25 @@ | ||
import { linearRGB } from "./util/index.js"; | ||
import type { RGBColour, WCAG } from "./types"; | ||
|
||
/** | ||
* Returns the relative luminance of a colour in the sRGB space. | ||
* | ||
* The calculations are compatible with WCAG 3.0 as it aligns with the sRGB spec | ||
* and difference to WCAG 2.1 is minimal in a 8 bit channel. | ||
* | ||
* https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef | ||
* | ||
* https://www.w3.org/WAI/GL/wiki/Relative_luminance | ||
* @param colour an hexadecimal colour | ||
*/ | ||
export function getSRGBLuminanceFromRGB( | ||
{ R, G, B }: RGBColour, | ||
standard?: WCAG, | ||
) { | ||
const isWCAG21 = standard === "WCAG2.1"; | ||
const r = linearRGB(R, isWCAG21); | ||
const g = linearRGB(G, isWCAG21); | ||
const b = linearRGB(B, isWCAG21); | ||
|
||
return (0.2126 * r) + (0.7152 * g) + (0.0722 * b); | ||
} |
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,17 @@ | ||
import { convertNamedCSSColourtoHex } from "./converters.js"; | ||
import { isHexDarkColour } from "./isHexDarkColour.js"; | ||
import type { NamedCSSColour, WCAG } from "./types"; | ||
|
||
/** | ||
* Evaluates if a named CSS colour is dark by measuring the contrast ratio against black and white | ||
* @param {string} name - A named CSS colour, ie: `hotpink` | ||
* @param {"WCAG2.1" | "WCAG3.0"} standard - Evaluate agains "WCAG2.1" or "WCAG3.0" | ||
* @returns {boolean | undefined} Returns `true`, `false` or `undefined` if name is not a valid CSS named colour | ||
*/ | ||
export function isCSSNamedDarkColour(name: NamedCSSColour, standard: WCAG): boolean | undefined { | ||
const hex = convertNamedCSSColourtoHex(name); | ||
if (hex) { | ||
return isHexDarkColour(hex, standard); | ||
} | ||
return undefined | ||
} |
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,14 @@ | ||
import { convertCSSRGBtoRGB } from "./converters.js"; | ||
import { getSRGBLuminanceFromRGB } from "./getSRGBLuminanceFromRGB.js"; | ||
import type { WCAG } from "./types"; | ||
|
||
export function isCSSRGBDarkColour(colour: string, standard: WCAG): boolean { | ||
const rgb = convertCSSRGBtoRGB(colour); | ||
const colourLuminance = getSRGBLuminanceFromRGB(rgb, standard); | ||
// We know white luminance is 1 so we can pre-calculate the whiteLuminance to 1.05 (1 + 0.05) | ||
const whiteContrast = 1.05 / colourLuminance; | ||
// We know black luminance is 0 so we can pre-calculate the blackLuminance to 0.05 (0 + 0.05) | ||
const blackContrast = colourLuminance / 0.05; | ||
|
||
return whiteContrast > blackContrast; | ||
} |
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,36 @@ | ||
import test from "ava"; | ||
import { isCSSRGBColour, isHexColour } from "../assertions.js"; | ||
|
||
test("assert true if string is in the CSS RGB format", ({ is }) => { | ||
is(isCSSRGBColour("rgb(12, 23, 111)"), true); | ||
}); | ||
|
||
test("assert false if string is not in the CSS RGB format", ({ is }) => { | ||
is(isCSSRGBColour("zzz( 23, 111, 87)"), false); | ||
}); | ||
|
||
test.skip("assert false if string doesn't have 3 colour values", ({ is }) => { | ||
is(isCSSRGBColour("rgb( 23, 111)"), false); | ||
}); | ||
|
||
test("assert true if string is a short hexadecimal colour", ({ is }) => { | ||
is(isHexColour("#333"), true); | ||
}); | ||
|
||
test("assert true if string is a six digital hexadecimal colour", ({ is }) => { | ||
is(isHexColour("#333000"), true); | ||
}); | ||
|
||
test("assert true if string is a short hexadecimal colour with alpha", ({ | ||
is, | ||
}) => { | ||
is(isHexColour("#333f"), true); | ||
}); | ||
|
||
test("assert true if string is an hexadecimal colour with alpha", ({ is }) => { | ||
is(isHexColour("#333000ff"), true); | ||
}); | ||
|
||
test("assert false if string is not a valid hexadecimal colour", ({ is }) => { | ||
is(isHexColour("#333HH0ff"), false); | ||
}); |
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,15 @@ | ||
import test from "ava"; | ||
import { isCSSNamedDarkColour } from "../isCSSNameDarkColour.js"; | ||
|
||
test("verify if `darkblue` is a dark colour", ({ is }) => { | ||
is(isCSSNamedDarkColour("darkblue", "WCAG2.1"), true); | ||
}); | ||
|
||
test("verify if `lightgoldenrodyellow` is a dark colour", ({ is }) => { | ||
is(isCSSNamedDarkColour("lightgoldenrodyellow", "WCAG2.1"), false); | ||
}); | ||
|
||
test("return undefined if named colour does not exist", ({ is }) => { | ||
/* @ts-ignore-line */ | ||
is(isCSSNamedDarkColour("rose", "WCAG2.1"), undefined); | ||
}); |
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,10 @@ | ||
import test from "ava"; | ||
import { isCSSRGBDarkColour } from "../isCSSRGBDarkColour.js"; | ||
|
||
test("verify if `rgb(20, 20, 20)` is a dark colour", ({ is }) => { | ||
is(isCSSRGBDarkColour("rgb(20, 20, 20)", "WCAG2.1"), true); | ||
}); | ||
|
||
test("verify if `rgb(200, 200, 200)` is not a dark colour", ({ is }) => { | ||
is(isCSSRGBDarkColour("rgb(200, 200, 200)", "WCAG2.1"), false); | ||
}); |
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