Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcnuttandrew committed Jan 11, 2024
1 parent b402c66 commit 1a5d406
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 62 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Test
on: [push]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/[email protected]

- name: Install and test
run: |
yarn
- name: Test
run: |
yarn test
npm run check
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"check": "svelte-check --tsconfig ./tsconfig.json",
"types": "tsc --noEmit --watch",
"prep-data": "cp node_modules/vega-datasets/data/* public/data/",
"test": "vitest"
"test": "vitest run",
"test:watch": "vitest"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^3.0.0",
Expand Down Expand Up @@ -49,4 +50,4 @@
"vega-embed": "^6.23.0",
"vega-lite": "^5.16.3"
}
}
}
1 change: 1 addition & 0 deletions src/components/DoubleRangeSlider.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
// @ts-nocheck
import { draggable, clamp } from "../lib/utils";
// from https://svelte.dev/repl/75d34e46cbe64bb68b7c2ac2c61931ce?version=4.2.8
export let start = 0;
Expand Down
1 change: 1 addition & 0 deletions src/components/Tooltip.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
// @ts-nocheck
import Portal from "svelte-portal";
export let top: string = "2rem";
export let onClose: () => void = () => {};
Expand Down
1 change: 1 addition & 0 deletions src/components/VerticalDoubleRangeSlider.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
// @ts-nocheck
import { draggable, clamp } from "../lib/utils";
// from https://github.com/mhkeller/svelte-double-range-slider/tree/master
export let start = 0;
Expand Down
5 changes: 3 additions & 2 deletions src/content-modules/Background.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script lang="ts">
import colorStore from "../stores/color-store";
import chroma from "chroma-js";
import { Color } from "../lib/Color";
import { Color, colorFromHex } from "../lib/Color";
import ColorChannelPicker from "../components/ColorChannelPicker.svelte";
import Tooltip from "../components/Tooltip.svelte";
$: bg = $colorStore.currentPal.background;
$: colorSpace = $colorStore.currentPal.colors[0].spaceName || "lab";
function onChange(color: Color) {
colorStore.setBackground(color);
}
Expand All @@ -22,7 +23,7 @@
on:change={(e) => {
// @ts-ignore
const newColor = chroma(e.target.value);
colorStore.setBackground(bg.fromChroma(newColor));
colorStore.setBackground(colorFromHex(newColor.hex(), colorSpace));
}}
/>
<ColorChannelPicker color={bg} onColorChange={onChange} />
Expand Down
5 changes: 3 additions & 2 deletions src/content-modules/SwatchTooltipContent.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<script lang="ts">
import ColorChannelPicker from "../components/ColorChannelPicker.svelte";
import chroma from "chroma-js";
import { Color } from "../lib/Color";
import { Color, colorFromHex } from "../lib/Color";
import colorStore from "../stores/color-store";
import focusStore from "../stores/focus-store";
import { buttonStyle } from "../lib/styles";
export let idx: number;
export let color: Color;
export let closeTooltip: () => void;
$: colors = $colorStore.currentPal.colors;
$: colorSpace = colors[0].spaceName || "lab";
function updateColor(color: Color) {
const updatedColors = [...colors];
Expand All @@ -24,7 +25,7 @@
on:change={(e) => {
// @ts-ignore
const newColor = chroma(e.target.value);
updateColor(color.fromChroma(newColor));
updateColor(colorFromHex(newColor.hex(), colorSpace));
}}
/>

Expand Down
12 changes: 4 additions & 8 deletions src/lib/Color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ export class Color {
toChannels(): [number, number, number] {
return Object.values(this.channels) as [number, number, number];
}
fromChroma(color: ChromaColor): Color {
// @ts-ignore
const channels = color[this.spaceName]();
return this.fromChannels(channels);
}
setChannel(channel: keyof typeof this.channels, value: number) {
this.channels[channel] = value;
}
Expand Down Expand Up @@ -84,6 +79,9 @@ export class Color {
symmetricDeltaE(color: Color): number {
return 0.5 * (this.deltaE(color) + color.deltaE(this));
}
copy(): Color {
return this.fromChannels(this.toChannels());
}
}

export class CIELAB extends Color {
Expand Down Expand Up @@ -178,14 +176,12 @@ export function colorFromHex(
colorSpace: keyof typeof colorDirectory
): Color {
if (colorHexCache.has(hex)) {
return { ...colorHexCache.get(hex) } as Color;
return colorHexCache.get(hex)!.copy() as Color;
}
const color = new ColorIO(hex).to(colorSpace);
const outColor = new colorDirectory[colorSpace]().fromChannels(color.coords);
colorHexCache.set(hex, outColor);
return outColor;
// const color = chroma(hex);
// return new colorDirectory[colorSpace]().fromChroma(color);
}

export function colorFromChannels(
Expand Down
21 changes: 4 additions & 17 deletions src/lib/ColorLint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ function makePalFromHexes(hexes: string[]): Palette {
const unique = <T>(arr: T[]): T[] => [...new Set(arr)];

test("ColorLint - ColorNameDiscriminability", async () => {
const examplePal = makePalFromHexes(["#006cc6", "#005ebe"]);
const examplePal = makePalFromHexes(["#5260d1", "#005ebe"]);
const exampleLint = new ColorNameDiscriminability(examplePal);
expect(exampleLint.passes).toBe(false);
expect(exampleLint.message).toBe(
"Color Name discriminability check failed. The following color names are repeated: Denim (#006cc6, #005ebe)"
"Color Name discriminability check failed. The following color names are repeated: Royalblue (#5260d1, #005ebe)"
);
const fix = await exampleLint.suggestFix();
const oldColorNames = unique<string>(
Expand All @@ -34,7 +34,6 @@ test("ColorLint - ColorNameDiscriminability", async () => {
expect(oldColorNames.length).toBe(1);
const colorNames = unique<string>(fix.colors.map((x) => getName(x)));
expect(colorNames.length).toBe(2);
expect(exampleLint.suggestVisualAnnotation()).toStrictEqual([]);
});

test("ColorLint - MaxColors", async () => {
Expand All @@ -46,7 +45,6 @@ test("ColorLint - MaxColors", async () => {
);
const fix = await exampleLint.suggestFix();
expect(fix.colors.length).toBe(9);
expect(exampleLint.suggestVisualAnnotation()).toStrictEqual([]);
});

test("ColorLint - ColorBlind", async () => {
Expand Down Expand Up @@ -76,18 +74,7 @@ test("ColorLint - ColorBlind", async () => {
expect(exampleLint3.message).toMatchSnapshot();
});

const ughWhat = [
"#00ffff",
"#ffb282",
"#ffebc7",
"#ffaa8a",
"#00faff",
"#ffc7c3",
"#00e4ff",
"#fdfdfc",
"#ffc89e",
"#00ffff",
];
const ughWhat = ["#00ffff", "#00faff", "#00e4ff", "#fdfdfc", "#00ffff"];
test("ColorLint - BackgroundDifferentiability", async () => {
const examplePal = makePalFromHexes(ughWhat);
const exampleLint = new BackgroundDifferentiability(examplePal);
Expand All @@ -102,7 +89,7 @@ test("ColorLint - BackgroundDifferentiability", async () => {
const exampleLint2 = new BackgroundDifferentiability(examplePal);
expect(exampleLint2.passes).toBe(false);
expect(exampleLint2.message).toBe(
"This palette has some colors (#00faff, #00e4ff) that are close to the background color"
"This palette has some colors (#0ff, #00faff, #00e4ff, #0ff) that are close to the background color"
);
const fix2 = await exampleLint2.suggestFix();
expect(fix2.colors.map((x) => x.toHex())).toMatchSnapshot();
Expand Down
32 changes: 11 additions & 21 deletions src/lib/__snapshots__/ColorLint.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,26 @@

exports[`ColorLint - BackgroundDifferentiability 1`] = `
[
"#00ffff",
"#ffb282",
"#ffebc7",
"#ffaa8a",
"#0ff",
"#00faff",
"#ffc7c3",
"#00e4ff",
"#fffffe",
"#ffc89e",
"#00ffff",
"#777776",
"#0ff",
]
`;

exports[`ColorLint - BackgroundDifferentiability 2`] = `
[
"#00ffff",
"#ffb282",
"#ffebc7",
"#ffaa8a",
"#00ebf1",
"#ffc7c3",
"#34f2ff",
"#fffffe",
"#ffc89e",
"#00ffff",
"#007070",
"#006f72",
"#006e7e",
"#fdfdfc",
"#007070",
]
`;

exports[`ColorLint - ColorBlind 1`] = `"This palette is not colorblind friendly for deuteranopia color blindness. The following pairs are undifferentiable: (#0078b4 and #8c69bc), (#ff7e0e and #c4bc27), (#e179c1 and #00becf)"`;
exports[`ColorLint - ColorBlind 1`] = `"This palette is not colorblind friendly for deuteranopia color blindness (ie can't see green). The following pairs are undifferentiable: (#0078b4 and #8c69bc), (#ff7e0e and #3d9f2f)"`;

exports[`ColorLint - ColorBlind 2`] = `"This palette is not colorblind friendly for protanopia color blindness. The following pairs are undifferentiable: (#0078b4 and #8c69bc), (#ff7e0e and #3d9f2f), (#ff7e0e and #c4bc27)"`;
exports[`ColorLint - ColorBlind 2`] = `"This palette is not colorblind friendly for protanopia color blindness (ie can't see red). The following pairs are undifferentiable: (#ff7e0e and #c4bc27), (#da2827 and #8e564b), (#8c69bc and #00becf)"`;

exports[`ColorLint - ColorBlind 3`] = `"This palette is not colorblind friendly for tritanopia color blindness. The following pairs are undifferentiable: (#ff7e0e and #e179c1), (#8c69bc and #7f7f7f)"`;
exports[`ColorLint - ColorBlind 3`] = `"This palette is not colorblind friendly for tritanopia color blindness (ie can't see blue). The following pairs are undifferentiable: (#8c69bc and #7f7f7f)"`;
10 changes: 2 additions & 8 deletions src/lib/lints/ColorLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ function AIFix(palette: Palette, message: string, engine: string) {
}

export class ColorLint<CheckData, ParamType> {
name: string;
taskTypes: TaskType[];
name: string = "";
taskTypes: TaskType[] = [];
passes: boolean;
checkData: CheckData;
palette: Palette;
Expand All @@ -36,8 +36,6 @@ export class ColorLint<CheckData, ParamType> {
level: "error" | "warning" = "error";

constructor(Palette: Palette) {
this.name = "";
this.taskTypes = [];
this.palette = Palette;
const { passCheck, data } = this._runCheck();
this.passes = passCheck;
Expand All @@ -53,10 +51,6 @@ export class ColorLint<CheckData, ParamType> {
return "";
}

suggestVisualAnnotation(): Annotation[] {
return [];
}

increaseParam() {
if (!this.hasParam) {
throw new Error("Cannot increase param on lint without param");
Expand Down
3 changes: 1 addition & 2 deletions src/lib/lints/background-differentiability.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ColorLint } from "./ColorLint";
import type { TaskType } from "./ColorLint";
import chroma from "chroma-js";
import { Color, toColorSpace } from "../Color";

const hexJoin = (colors: Color[]) => colors.map((x) => x.toHex()).join(", ");
Expand All @@ -14,7 +13,7 @@ export default class BackgroundDifferentiability extends ColorLint<
_runCheck() {
const { colors, background } = this.palette;
const colorsCloseToBackground = colors.reduce((acc, x, idx) => {
const pass = chroma.deltaE(x.toChroma(), background.toChroma()) < 10;
const pass = x.symmetricDeltaE(background) < 15;
return pass ? [...acc, idx] : acc;
}, [] as number[]);

Expand Down

0 comments on commit 1a5d406

Please sign in to comment.