Skip to content

Commit

Permalink
Merge pull request #247 from oddbird/hotfix-ratio
Browse files Browse the repository at this point in the history
Fix ratio not updating when sliders change
  • Loading branch information
jgerigmeyer authored Mar 3, 2025
2 parents 4d10b48 + 4337ce3 commit 5f9c4b6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
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();
});
});
});

0 comments on commit 5f9c4b6

Please sign in to comment.