Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ratio not updating when sliders change #247

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/lib/components/ratio/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import { RATIOS } from '$lib/constants';
import { bg, fg } from '$lib/stores';

let fgPremultiplied = $derived.by(() => {
if ($fg.alpha === 1 || $bg.alpha !== 1) return $fg;
return mix($bg, $fg, $fg.alpha, {
space: 'srgb',
premultiplied: false,
});
let ratio = $derived.by(() => {
let fgPremultiplied = $fg;
if ($fg.alpha !== 1 && $bg.alpha === 1) {
fgPremultiplied = mix($bg, $fg, $fg.alpha, {
space: 'srgb',
premultiplied: false,
});
}
return contrast($bg, fgPremultiplied, 'WCAG21');
});
let ratio = $derived(contrast($bg, fgPremultiplied, 'WCAG21'));
let displayRatio = $derived(Math.round((ratio + Number.EPSILON) * 100) / 100);
let pass = $derived(ratio >= RATIOS.AA.Normal);
let alphaWarning = $derived.by(() => {
Expand Down
18 changes: 16 additions & 2 deletions test/lib/components/Ratio.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render } from '@testing-library/svelte';
import { render, waitFor } from '@testing-library/svelte';

import Ratio from '$lib/components/ratio/index.svelte';
import { reset } from '$lib/stores';
import { fg, reset } from '$lib/stores';

describe('Ratio', () => {
afterEach(() => {
Expand All @@ -15,4 +15,18 @@ describe('Ratio', () => {
expect(queryAllByText('Pass')).not.toBeNull();
expect(queryByText('Fail')).toBeNull();
});

it('updates ratio when color changes', async () => {
const { getByText, queryByText, queryAllByText } = render(Ratio);
fg.update((val) => {
val.coords = [0.5, 0.5, 0.5];
return val;
});

await waitFor(() => {
expect(getByText('3.22:1')).toBeVisible();
expect(queryByText('Pass')).not.toBeNull();
expect(queryAllByText('Fail')).not.toBeNull();
});
});
});