-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Adjust ratio for opacity #222
Changes from 7 commits
9c239d7
51feefe
2aee05d
50b00f9
bdf6fc1
d4dccb8
bf7d3eb
2df32d4
4e9be03
55e5b88
a2defbe
f40cfb0
52f355d
d42dcde
0576ac3
7d12b22
607ea55
cf92e08
72a30d0
e129e61
439f291
1044fb1
15b5251
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,30 @@ | ||
<script lang="ts"> | ||
import { contrast } from 'colorjs.io/fn'; | ||
import { contrast, mix } from 'colorjs.io/fn'; | ||
|
||
import ColorIssues from '$lib/components/ratio/ColorIssues.svelte'; | ||
import Result from '$lib/components/ratio/Result.svelte'; | ||
import ExternalLink from '$lib/components/util/ExternalLink.svelte'; | ||
import Icon from '$lib/components/util/Icon.svelte'; | ||
import { RATIOS } from '$lib/constants'; | ||
import { bg, fg } from '$lib/stores'; | ||
|
||
let ratio = $derived(contrast($bg, $fg, 'WCAG21')); | ||
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(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(() => { | ||
if ($bg.alpha !== 1) | ||
return 'Alpha is not considered when the background is not opaque.'; | ||
if ($fg.alpha !== 1) | ||
return 'This ratio is our best estimate with transparency.'; | ||
jamesnw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return null; | ||
}); | ||
</script> | ||
|
||
<aside data-layout="results"> | ||
|
@@ -20,6 +35,13 @@ | |
<span class="sr-only">The contrast ratio is</span> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know it's not new here, but I don't think this is useful screen reader context. The heading directly before this says 'current ratio' already. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Removed in 1044fb1. On the topic, I wonder if (in another story) we should edit the click-to-copy buttons to say what exactly they are copying? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I don't know the best solution on those - but no harm oping an issue to explore it. |
||
<span class="result-ratio-number">{displayRatio}:1</span> | ||
</h3> | ||
{#if alphaWarning} | ||
<div class="result-warning-info"> | ||
<p data-color-info="warning" class="result-warning"> | ||
<Icon name="warning" />{alphaWarning} | ||
</p> | ||
</div> | ||
{/if} | ||
|
||
<p class="result-intro"> | ||
In WCAG 2, contrast is a measure of the difference in perceived brightness | ||
|
@@ -73,7 +95,9 @@ | |
@use 'config'; | ||
|
||
[data-layout='results'] { | ||
background-color: var(--bgcolor); | ||
background: linear-gradient(90deg, var(--bgcolor), var(--bgcolor)), | ||
url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 60"><rect fill="%23e8e8e8" width="30" height="30"/><rect x="30" y="30" width="30" height="30" fill="%23e8e8e8"/></svg>'); | ||
background-size: 30px 30px; | ||
color: var(--fgcolor); | ||
display: grid; | ||
gap: var(--result-layout-gap, var(--shim)); | ||
|
@@ -96,26 +120,34 @@ | |
} | ||
|
||
.contrast-info { | ||
column-gap: var(--shim); | ||
display: grid; | ||
grid-area: contrastinfo; | ||
// fixed row height to prevent layout shift when warning appears | ||
grid-template: | ||
'heading' min-content | ||
'number' min-content / 100%; | ||
'heading heading' min-content | ||
'number warning' var(--ratio-warning-height) / auto 1fr; | ||
margin-block-end: var(--contrast-info-block-end, var(--gutter)); | ||
|
||
@include config.between('sm-column-break', 'lg-page-break') { | ||
--contrast-info-block-end: 0; | ||
gap: var(--shim) var(--gutter); | ||
// fixed width column to prevent layout shift as the ratio number changes | ||
// fixed row height to prevent layout shift when warning appears | ||
grid-template: | ||
'heading number' min-content | ||
'intro intro' 1fr / auto var(--ratio-width); | ||
'intro warning' var(--ratio-warning-height) / auto minmax( | ||
var(--ratio-width), | ||
1fr | ||
); | ||
} | ||
|
||
@include config.above('lg-page-break') { | ||
gap: var(--shim); | ||
grid-template: | ||
'heading' auto | ||
'intro' auto | ||
'number' auto / 100%; | ||
'heading heading' auto | ||
'intro intro' auto | ||
'number warning' var(--ratio-warning-height) / var(--ratio-width) auto; | ||
} | ||
} | ||
|
||
|
@@ -138,15 +170,30 @@ | |
} | ||
|
||
.result-ratio { | ||
align-items: start; | ||
align-items: var(--result-ratio-align, center); | ||
display: inline-flex; | ||
grid-area: number; | ||
line-height: 0.7; // weird number alignment | ||
|
||
@include config.between('sm-column-break', 'lg-page-break') { | ||
--result-ratio-align: start; | ||
justify-content: flex-end; | ||
} | ||
} | ||
.result-warning-info { | ||
jgerigmeyer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
align-items: center; | ||
display: flex; | ||
grid-area: warning; | ||
} | ||
.result-warning { | ||
jgerigmeyer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--warning-bg: var(--bg); | ||
--warning-padding-block: var(--shim); | ||
--warning-padding-inline: var(--shim); | ||
--warning-size: border: var(--border-width) solid var(--border); | ||
border-radius: var(--border-radius); | ||
display: flex; | ||
gap: var(--half-shim); | ||
} | ||
|
||
.result-ratio-number { | ||
background-color: var(--status-result-bg, var(--bgcolor)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we link these two notes directly to the fuller description (expanding if necessary)?
If not that, we could consider adding the fuller explanation in a tooltip/dialog here instead of in the expandable "issues" section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think linking will be much easier once we're done with #223. I'm fine with moving it to a tooltip/dialog, but note we'd need to develop that pattern. I'll defer to @SondraE on what we should do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, would it make more sense for me to focus on cleaning up #223 in the meantime @jamesnw? That PR just needs style cleanup, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct- that would be great!