-
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 pickHexColorContrast (#10)
* feat: Add pickHexColorContrast * chore: Export pickHexColorContrast * Update publish-rc.yml * Create thin-dolls-shout.md
- Loading branch information
Showing
8 changed files
with
90 additions
and
8 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 `pickHexColorContrast` function |
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
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,33 @@ | ||
import { getSRGBLuminanceFromHex } from "./getSRGBLuminanceFromHex.js"; | ||
import type { WCAG } from "./types"; | ||
|
||
type ColorArgs = { | ||
backgroundColour: string; | ||
optionOneColour: string; | ||
optionTwoColour: string; | ||
} | ||
|
||
const relativeContrast = (firstColour: number, secondColour: number) => { | ||
if (firstColour > secondColour) { | ||
return firstColour / secondColour; | ||
} | ||
return secondColour / firstColour; | ||
} | ||
|
||
export const pickHexColorContrast = ( | ||
{backgroundColour, optionOneColour, optionTwoColour}: ColorArgs, | ||
standard: WCAG | ||
): string => { | ||
const backgroundColourLuminance = getSRGBLuminanceFromHex(backgroundColour, standard) + 0.05; | ||
const optionOneColourLuminance = getSRGBLuminanceFromHex(optionOneColour, standard) + 0.05; | ||
const optionTwoColourLuminance = getSRGBLuminanceFromHex(optionTwoColour, standard) + 0.05; | ||
|
||
const optionOneContrast = relativeContrast(optionOneColourLuminance, backgroundColourLuminance); | ||
const optionTwoContrast = relativeContrast(optionTwoColourLuminance, backgroundColourLuminance); | ||
|
||
if (optionOneContrast > optionTwoContrast) { | ||
return optionOneColour; | ||
} | ||
return optionTwoColour; | ||
|
||
}; |
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,35 @@ | ||
import test from "ava"; | ||
import { pickHexColorContrast } from "../pickHexColorContrast.js"; | ||
|
||
test("should return #FFFFFF as the best colour for a #333333 background", ({ | ||
is, | ||
}) => { | ||
const colours = { | ||
backgroundColour: "#333333", | ||
optionOneColour: "#FFFFFF", | ||
optionTwoColour: "#000000", | ||
}; | ||
is(pickHexColorContrast(colours, "WCAG2.1"), "#FFFFFF"); | ||
}); | ||
|
||
test("should return #000000 as the best colour for a #BED background", ({ | ||
is, | ||
}) => { | ||
const colours = { | ||
backgroundColour: "#BED", | ||
optionOneColour: "#FFFFFF", | ||
optionTwoColour: "#000000", | ||
}; | ||
is(pickHexColorContrast(colours, "WCAG2.1"), "#000000"); | ||
}); | ||
|
||
test("should return #000000 as the best colour for a #DD337F background", ({ | ||
is, | ||
}) => { | ||
const colours = { | ||
backgroundColour: "#DD337F", | ||
optionOneColour: "#FFFFFF", | ||
optionTwoColour: "#000000", | ||
}; | ||
is(pickHexColorContrast(colours, "WCAG2.1"), "#000000"); | ||
}); |
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 |
---|---|---|
|
@@ -48,3 +48,5 @@ export interface HueHelper { | |
} | ||
|
||
export type ColourSpace = "sRGB"; | ||
|
||
export type WCAG = "WCAG2.1" | "WCAG3.0"; |
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