-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [TASK] add contrasting color node * [TASK] update test case, change wcag * [TASK] update test cases to use WcagVersion * [TASK] add changeset --------- Co-authored-by: SorsOps <[email protected]>
- Loading branch information
Showing
10 changed files
with
297 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,6 @@ | ||
--- | ||
"@tokens-studio/graph-engine": minor | ||
"@tokens-studio/graph-engine-ui": patch | ||
--- | ||
|
||
Add new node for contrasting color supporting wcag 2.1 and 3.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
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,87 @@ | ||
/** | ||
* Performs a contrast calculation between two colors using APCA-W3 calcs | ||
* | ||
* @packageDocumentation | ||
*/ | ||
|
||
import { NodeDefinition, NodeTypes } from "#/types.js"; | ||
import { calcAPCA } from "apca-w3"; | ||
import chroma from "chroma-js"; | ||
|
||
export const type = NodeTypes.CONTRASTING; | ||
|
||
export type State = { | ||
a: string, | ||
b: string, | ||
background: string, | ||
wcag: WcagVersion, | ||
threshold: number, | ||
contrast: number | ||
} | ||
|
||
type contrastingValues = { | ||
color: string, | ||
sufficient: boolean, | ||
contrast: number | ||
} | ||
|
||
export enum WcagVersion { | ||
V2 = "2.1", | ||
V3 = "3.0" | ||
} | ||
|
||
export const defaults: State = { | ||
a: "#000000", | ||
b: "#ffffff", | ||
background: "#ffffff", | ||
wcag: WcagVersion.V3, | ||
threshold: 60, | ||
contrast: 0 | ||
} | ||
|
||
export const process = (input, state: State): contrastingValues => { | ||
const final = { | ||
...state, | ||
...input, | ||
}; | ||
|
||
let a,b; | ||
|
||
if (final.wcag == WcagVersion.V2) { | ||
a = chroma.contrast(final.a, final.background); | ||
b = chroma.contrast(final.b, final.background); | ||
|
||
} else { | ||
a = Math.abs(calcAPCA(final.a, final.background)); | ||
b = Math.abs(calcAPCA(final.b, final.background)); | ||
} | ||
|
||
let contrast: contrastingValues; | ||
|
||
if ( a > b ) { | ||
contrast = { | ||
color: final.a, | ||
sufficient: a >= final.threshold, | ||
contrast: a | ||
}; | ||
} else { | ||
contrast = { | ||
color: final.b, | ||
sufficient: b >= final.threshold, | ||
contrast: b | ||
}; | ||
} | ||
|
||
return contrast; | ||
}; | ||
|
||
export const mapOutput = (input, state, processed: contrastingValues) => { | ||
return processed; | ||
}; | ||
|
||
export const node: NodeDefinition<State, State> = { | ||
defaults, | ||
type, | ||
process, | ||
mapOutput, | ||
}; |
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 |
---|---|---|
@@ -1,9 +1,19 @@ | ||
import { node as advancedBlend } from "./advancedBlend.js"; | ||
import { node as blend } from "./blend.js"; | ||
import { node as contrasting } from "./contrasting.js"; | ||
import { node as create } from "./create.js"; | ||
import { node as extract } from "./extract.js"; | ||
import { node as scale } from "./scale.js"; | ||
import { node as convert } from "./convert.js"; | ||
import { node as wheel } from "./wheel.js"; | ||
|
||
export const nodes = [blend, scale, create, advancedBlend, extract, convert, wheel]; | ||
export const nodes = [ | ||
blend, | ||
scale, | ||
contrasting, | ||
create, | ||
advancedBlend, | ||
extract, | ||
convert, | ||
wheel, | ||
]; |
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
67 changes: 67 additions & 0 deletions
67
packages/graph-engine/tests/suites/nodes/color/contrasting.test.ts
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,67 @@ | ||
import { node, WcagVersion } from "#/nodes/color/contrasting.js"; | ||
import { executeNode } from "#/core.js"; | ||
|
||
describe("color/blend", () => { | ||
it("should return the more contrasting color correctly with WCAG 3", async () => { | ||
const output = await executeNode({ | ||
input: { | ||
a: "#000000", | ||
b: "#ffffff", | ||
background: "#ffffff", | ||
wcag: WcagVersion.V3, | ||
threshold: 60 | ||
}, | ||
node, | ||
state: node.defaults, | ||
nodeId: "", | ||
}); | ||
|
||
expect(output).toEqual({ | ||
color: "#000000", | ||
sufficient: true, // assuming contrast value is above 60 | ||
contrast: 106.04067321268862 | ||
}); | ||
}); | ||
|
||
it("should return the more contrasting color correctly with WCAG 2", async () => { | ||
const output = await executeNode({ | ||
input: { | ||
a: "#000000", | ||
b: "#ffffff", | ||
background: "#000000", | ||
wcag: WcagVersion.V2, | ||
threshold: 4.5 | ||
}, | ||
node, | ||
state: node.defaults, | ||
nodeId: "", | ||
}); | ||
|
||
expect(output).toEqual({ | ||
color: "#ffffff", | ||
sufficient: true, // assuming contrast value is above 4.5 | ||
contrast: 21 | ||
}); | ||
}); | ||
|
||
it("should return false for sufficient contrast if below threshold", async () => { | ||
const output = await executeNode({ | ||
input: { | ||
a: "#dddddd", | ||
b: "#bbbbbb", | ||
background: "#ffffff", | ||
wcag: WcagVersion.V3, | ||
threshold: 60 | ||
}, | ||
node, | ||
state: node.defaults, | ||
nodeId: "", | ||
}); | ||
|
||
expect(output).toEqual({ | ||
color: "#bbbbbb", | ||
sufficient: false, // assuming contrast value is below 60 | ||
contrast: 36.717456545363994 | ||
}); | ||
}); | ||
}); |
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
108 changes: 108 additions & 0 deletions
108
packages/ui/src/components/flow/nodes/color/contrastingNode.tsx
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,108 @@ | ||
import { Button, DropdownMenu, Label, Stack, Text, TextInput } from '@tokens-studio/ui'; | ||
import { Handle, HandleContainer } from '#/components/flow/handles.tsx'; | ||
import { PreviewColor } from '../../preview/color.tsx'; | ||
import { WrapNode, useNode } from '../../wrapper/nodeV2.tsx'; | ||
import { node, WcagVersion } from '@tokens-studio/graph-engine/nodes/color/contrasting.js'; | ||
import PreviewNumber from '../../preview/number.tsx'; | ||
import React, {useCallback} from 'react'; | ||
import {PreviewBoolean} from "../../preview/boolean.tsx"; | ||
import {PreviewAny} from "../../preview/any.tsx"; | ||
|
||
const ContrastingNode = () => { | ||
const { input, output, state, setState } = useNode(); | ||
const setWcag = useCallback((ev) => { | ||
const version = WcagVersion[ev.currentTarget.dataset.key as keyof typeof WcagVersion]; | ||
setState((state) => ({ | ||
...state, | ||
wcag: version, | ||
})); | ||
}, [setState]); | ||
|
||
const setThreshold = useCallback((ev) => { | ||
const threshold = ev.target.value; | ||
setState((state) => ({ | ||
...state, | ||
threshold, | ||
})); | ||
}, [setState]); | ||
|
||
return ( | ||
<Stack direction="row" gap={4}> | ||
<HandleContainer type="target"> | ||
<Handle id="a"> | ||
<Text>Color A</Text> | ||
<Text> | ||
<PreviewColor value={input.a} /> | ||
</Text> | ||
</Handle> | ||
<Handle id="b"> | ||
<Text>Color B</Text> | ||
<Text> | ||
<PreviewColor value={input.b} /> | ||
</Text> | ||
</Handle> | ||
<Handle id="background"> | ||
<Text>Background</Text> | ||
<Text> | ||
<PreviewColor value={input.background} /> | ||
</Text> | ||
</Handle> | ||
<Handle id="wcag"> | ||
<Label>WCAG Version</Label> | ||
|
||
{input.wcag !== undefined ? ( | ||
<Text>{input.wcag}</Text> | ||
) : ( | ||
<DropdownMenu> | ||
<DropdownMenu.Trigger asChild> | ||
<Button variant="secondary" asDropdown size="small"> | ||
{state.wcag} | ||
</Button> | ||
</DropdownMenu.Trigger> | ||
|
||
<DropdownMenu.Portal> | ||
<DropdownMenu.Content> | ||
{Object.entries(WcagVersion).map(([key, value]) => ( | ||
<DropdownMenu.Item key={key} onClick={setWcag} data-key={key}> | ||
{value} | ||
</DropdownMenu.Item> | ||
))} | ||
</DropdownMenu.Content> | ||
</DropdownMenu.Portal> | ||
</DropdownMenu> | ||
)} | ||
</Handle> | ||
<Handle id="threshold"> | ||
<Stack direction="row" justify="between" gap={3} align="center"> | ||
<Label>Threshold</Label> | ||
{input.threshold !== undefined ? ( | ||
<PreviewNumber value={input.threshold} /> | ||
) : ( | ||
<TextInput onChange={setThreshold} value={state.threshold} /> | ||
)} | ||
</Stack> | ||
</Handle> | ||
</HandleContainer> | ||
|
||
<HandleContainer type="source"> | ||
<Handle id="color"> | ||
<Text>Color</Text> | ||
<PreviewColor value={output?.color} /> | ||
</Handle> | ||
<Handle id="contrast"> | ||
<Text>Contrast</Text> | ||
<PreviewAny value={output?.contrast} /> | ||
</Handle> | ||
<Handle id="sufficient"> | ||
<Text>Sufficient</Text> | ||
<PreviewBoolean value={output?.sufficient} /> | ||
</Handle> | ||
</HandleContainer> | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default WrapNode(ContrastingNode, { | ||
...node, | ||
title: 'Contrast', | ||
}); |
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