Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Make color matrix accessible #94

Merged
merged 14 commits into from
Jun 27, 2016
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ source 'https://rubygems.org'

gem 'jekyll', '3.1.6'
gem 'redcarpet', '3.3.3'
gem 'wcag_color_contrast', '0.0.1'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ GEM
rouge (1.10.1)
safe_yaml (1.0.4)
sass (3.4.22)
wcag_color_contrast (0.0.1)

PLATFORMS
ruby

DEPENDENCIES
jekyll (= 3.1.6)
redcarpet (= 3.3.3)
wcag_color_contrast (= 0.0.1)

BUNDLED WITH
1.12.4
17 changes: 17 additions & 0 deletions _data/palette.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
- name: white
hex: 'FFFFFF'

- name: light
hex: 'B3EFFF'

- name: bright
hex: '00CFFF'

- name: medium
hex: '046B99'

- name: dark
hex: '1C304A'

- name: black
hex: '000000'
85 changes: 85 additions & 0 deletions _includes/color-matrix.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<svg class="usa-matrix-symbol-definitions">
<symbol id="usa-matrix-bad-contrast-ratio" viewBox="0 0 100 100">
<rect width="100" height="100" fill="#f0f0f0"/>
<line x1="0" y1="100" x2="100" y2="0" stroke="white" stroke-width="4"/>
</symbol>
</svg>

<div class="usa-matrix-legend">
<svg><use xlink:href="#usa-matrix-bad-contrast-ratio"/></svg>

<p class="usa-sr-invisible" aria-hidden="true">
Please don't use these color combinations; they do not meet a color
contrast ratio of 4.5:1, so they do not conform with the standards of
Section 508 for body text. This means that some people would have
difficulty reading the text. Employing accessibility best practices
improves the user experience for all users.
</p>
</div>

<table class="usa-table-borderless usa-matrix">
<thead>
<tr>
<td scope="col"></td>
{% for foreground in site.data.palette %}
<td scope="col">
<div class="usa-matrix-desc">
{{ foreground.name | capitalize }} text<br>
<small>#{{ foreground.hex }}</small><br>
</div>
<strong class="usa-matrix-foreground-{{ foreground.name }} usa-sr-invisible" aria-hidden="true">
Aa
</strong>
</td>
{% endfor %}
</tr>
</thead>

<tbody>
{% assign reversed_palette = site.data.palette | reverse %}
{% for background in reversed_palette %}
<tr>
<td scope="row">
<div>
<div class="usa-matrix-square usa-color-{{ background.name }}"></div>
<div class="usa-matrix-desc">
{{ background.name | capitalize }} background<br>
<small>#{{ background.hex }}</small>
</div>
</div>
</td>
{% for foreground in site.data.palette %}
{% assign ratio = foreground.hex | contrast_ratio with: background.hex %}
{% if ratio >= 4.5 %}
<td class="usa-matrix-valid-color-combo">
<div class="usa-matrix-square usa-color-{{ background.name }}"
title="The contrast ratio of {{ foreground.name }} on {{ background.name }} is {{ ratio | human_readable_contrast_ratio }}."
role="presentation">
<strong class="usa-matrix-foreground-{{ foreground.name }} usa-sr-invisible" aria-hidden="true">
Aa
</strong>
</div>
<div class="usa-matrix-color-combo-description">
<strong>{{ foreground.name | capitalize }}</strong> text on
<strong>{{ background.name }}</strong> background
<span class="usa-sr-only">is 508-compliant, with a contrast ratio of {{ ratio | human_readable_contrast_ratio }}.</span>
</div>
</td>
{% else %}
<td class="usa-matrix-invalid-color-combo">
<div title="The contrast ratio of {{ foreground.name }} on {{ background.name }} is {{ ratio | human_readable_contrast_ratio }}, which does not conform with the standards of Section 508 for body text." role="presentation">
<svg class="usa-matrix-square">
<use xlink:href="#usa-matrix-bad-contrast-ratio"/>
</svg>
</div>
<div class="usa-sr-only">
Do not use {{ foreground.name }} text on {{ background.name }} background; it is not 508-compliant, with a contrast ratio of {{ ratio | human_readable_contrast_ratio }}.
</div>
</td>
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
22 changes: 22 additions & 0 deletions _plugins/contrast_ratio.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'liquid'
require 'wcag_color_contrast'

module ContrastRatio
def contrast_ratio(color1, color2)
WCAGColorContrast.ratio(color1, color2)
end

def human_readable_contrast_ratio(ratio)
if ratio < 4
ndigits = 1
elsif ratio < 5
ndigits = 2
else
ndigits = 0
end

"#{ratio.round(ndigits)}:1"
end
end

Liquid::Template.register_filter(ContrastRatio)
170 changes: 170 additions & 0 deletions _sass/_color-matrix.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// "Marge" is a cross between USWDS's "Medium" and "Large" breakpoints.
// It's the equivalent of Bootstrap's "Medium" size.
$marge-screen: 992px;

// The WAI-ARIA specification states the following about aria-hidden:
//
// Authors MAY, with caution, use aria-hidden to hide visibly rendered
// content from assistive technologies only if the act of hiding this
// content is intended to improve the experience for users of assistive
// technologies by removing redundant or extraneous content. Authors
// using aria-hidden to hide visible content from screen readers MUST
// ensure that identical or equivalent meaning and functionality is
// exposed to assistive technologies.
//
// Unfortunately, however, the U.S. Web Design Standards forcibly set
// anything with aria-hidden to display: none, making it difficult to
// address the above use case. The following rule allows us to override
// this default and create content that is visible to sighted users but
// irrelevant to non-sighted users.
.usa-sr-invisible[aria-hidden="true"] {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this .usa-sr-invisible class is a valid use case for accessible content, and we should push it upstream to USWDS if possible. I'm curious what our other accessibility folks think about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I just discovered that this was actually a bug filed at uswds/uswds#1120, and it was fixed just 2 days ago. So another option here is to just use the very latest uswds... which, come to think of it, we're already doing via a git submodule, so this mostly just involves me git pull'ing my web-design-standards directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack, I was wrong--our usdws checkout is actually from commit 16e8619 (October 2015). I'm a bit hesitant about updating it to the very latest version that fixes this issue, however, because I'm not sure if there will be other breakages with the style guide... Could use any advice anyone has here!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I gave this a bit of thought and my inclination is to commit this as-is, but add a new issue to upgrade to the new uswds and remove the .usa-sr-invisible class. That way we can deal with any possible regressions in a separate PR and keep this one nicely scoped to just the color matrix.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know a whole bunch about how the two work together, but I'm inclined to agree with you @toolness — if upgrading is likely to introduce a lot of other conflicts, let's keep this issue focused on the color matrix, and file another for upgrading to the latest web design standards package.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I think it's best to roll with this for now and upgrade in a new issue.

display: block !important;
}

.usa-matrix-foreground-white {
color: $color-white;

// https://css-tricks.com/adding-stroke-to-web-text/
text-shadow:
-1px -1px 0 #000,
1px -1px 0 #000,
-1px 1px 0 #000,
1px 1px 0 #000;
}

.usa-matrix-foreground-light {
color: $color-light;
}

.usa-matrix-foreground-bright {
color: $color-bright;
}

.usa-matrix-foreground-medium {
color: $color-medium;
}

.usa-matrix-foreground-dark {
color: $color-dark;
}

.usa-matrix-foreground-black {
color: $color-black;
}

.usa-matrix td {
vertical-align: top;
}

.usa-matrix-square {
width: 4em;
height: 4em;
display: flex;
justify-content: center;
align-items: center;

strong {
text-shadow: none;
}
}

.usa-matrix-symbol-definitions {
display: none;
}

.usa-matrix-legend {
position: relative;

svg {
position: absolute;
top: 0;
left: 0;
width: 35px;
height: 35px;
}

p {
margin-left: 45px;
font-size: 0.75em;
}
}

td[scope="row"] > div {
display: flex;

.usa-matrix-desc {
flex: 1;
}
}

@media (min-width: $marge-screen) and (max-width: "#{$large-screen - 1}") {
table th, table td {
padding: 0.5em;
}

.usa-matrix-desc {
font-size: 0.75em;
}

td[scope="row"] > div {
flex-direction: column;

.usa-matrix-desc {
padding-top: 0.5em;
}
}
}

@media (min-width: $large-screen) {
td[scope="row"] > div {
.usa-matrix-desc {
padding-left: 0.5em;
}
}
}

@media (min-width: $marge-screen) {
.usa-matrix-color-combo-description {
@include sr-only();
}
}

@media (max-width: "#{$marge-screen - 1}") {
.usa-matrix-legend {
display: none;
}

table.usa-matrix {
display: block;

thead {
display: none;
}

tbody {
display: block;

tr {
display: block;

td.usa-matrix-valid-color-combo {
display: flex;
align-items: center;

.usa-matrix-color-combo-description {
flex: 1;
padding-left: 1em;
}
}

td.usa-matrix-invalid-color-combo {
display: none;
}

td[scope="row"] {
display: none;
}
}
}
}
}
1 change: 1 addition & 0 deletions css/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
// Custom styles ---------- //

@import 'styleguide';
@import 'color-matrix';
42 changes: 6 additions & 36 deletions visual-style.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,21 @@ <h2 class="styleguide-header" id="colors">Color palette</h2>
<p>Color palettes to use with Illustrator, Sketch, and Keynote</p>

<div class="usa-grid-full usa-color-row usa-primary-color-section">
<div class="usa-color-square usa-color-white">
{% for color in site.data.palette %}
<div class="usa-color-square usa-color-{{ color.name }} {% cycle '', 'usa-mobile-end-row' %}">
<div class="usa-color-inner-content">
<p class="usa-color-name">white</p>
<p class="usa-color-hex">#FFFFFF</p>
</div>
</div>
<div class="usa-color-square usa-color-light usa-mobile-end-row">
<div class="usa-color-inner-content">
<p class="usa-color-name">light</p>
<p class="usa-color-hex">#B3EFFF</p>
</div>
</div>
<div class="usa-color-square usa-color-bright">
<div class="usa-color-inner-content">
<p class="usa-color-name">bright</p>
<p class="usa-color-hex">#00CFFF</p>
</div>
</div>
<div class="usa-color-square usa-color-medium usa-mobile-end-row">
<div class="usa-color-inner-content">
<p class="usa-color-name">medium</p>
<p class="usa-color-hex">#046B99</p>
</div>
</div>
<div class="usa-color-square usa-color-dark">
<div class="usa-color-inner-content">
<p class="usa-color-name">dark</p>
<p class="usa-color-hex">#1C304A</p>
</div>
</div>
<div class="usa-color-square usa-color-black usa-mobile-end-row">
<div class="usa-color-inner-content">
<p class="usa-color-name">black</p>
<p class="usa-color-hex">#000000</p>
<p class="usa-color-name">{{ color.name }}</p>
<p class="usa-color-hex">#{{ color.hex }}</p>
</div>
</div>
{% endfor %}
</div>

<br>
<h3>Color combinations for text and backgrounds</h3>
<p>This matrix shows how to use the 18F brand colors for text and backgrounds in combinations that conform with the standards of Section 508 of the Rehabilitation Act. For more on color contrast, read <a href="https://pages.18f.gov/accessibility/color/" target='_blank' title='Opens in new window'>18F's Accessibility Guide</a>. Each of the combinations shown below meets or exceeds a contrast ratio of 4.5:1, and is approved for body text and headline text.</p>

<br>
<img src="{{ site.baseurl }}/assets/img/18F-color-palette.png" alt="18F color palette">
<br>
{% include color-matrix.html %}

<a class="usa-button usa-button-gray" href="{{ site.baseurl }}/assets/dist/18F_Color_Palette.zip">Download color palettes</a>

Expand Down