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

color contrast check prototype #349

Merged
merged 12 commits into from Oct 7, 2022
Merged

color contrast check prototype #349

merged 12 commits into from Oct 7, 2022

Conversation

lukasoppermann
Copy link
Contributor

@lukasoppermann lukasoppermann commented Sep 8, 2022

This is just the start. Once merged it will fix https://github.com/github/primer/issues/1277 Many questions are popping up:

  1. where do we store the contrast requirement (directly in token.json? In a separate spec file?)
  2. Should a build fail if contrasts fail? (Currently impossible, so we would need to add an ignore flag that will report the fail but ignore it)
  3. Should we ignore component tokens and run it on primitives only (I think yes)
  4. We need to import the json when we have one for the new build process and use that to test

Todo:

  • replace lib with own code to reduce dependency (if possible)
  • clean up code to make it easier to read
  • add better table (left aligned, not ' around strings)
  • add all pairs
  • make it run on all modes (rules may differ depending on mode, e.g. HC)

@changeset-bot
Copy link

changeset-bot bot commented Sep 8, 2022

⚠️ No Changeset found

Latest commit: fe60528

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Sep 8, 2022

Variables changed
No variables changed

@github-actions
Copy link
Contributor

github-actions bot commented Sep 8, 2022

🟢 No design token changes found

@lukasoppermann lukasoppermann temporarily deployed to github-pages September 8, 2022 15:06 Inactive
@lukasoppermann lukasoppermann temporarily deployed to github-pages September 9, 2022 15:50 Inactive
@lukasoppermann lukasoppermann temporarily deployed to github-pages September 10, 2022 21:18 Inactive
@lukasoppermann
Copy link
Contributor Author

I added all color pairs now. However the lib does not work with transparent colors.

This is something we need to address. The "problem" here is that we may transparent colors on multiple bgs.

E.g. a accent.muted could be place on canvas.default or canvas.inset. So we would probably need to add to some pairs the possibilities of background colors.

This should work but adds more effort.

@rezrah do you have any other idea?

@lukasoppermann lukasoppermann temporarily deployed to github-pages September 10, 2022 21:26 Inactive
@lukasoppermann lukasoppermann temporarily deployed to github-pages September 11, 2022 21:35 Inactive
@lukasoppermann
Copy link
Contributor Author

Okay, I added the possibility to define multiple bgs for transparent colors.

The way it works now:

  • for each pair you can define an options object with a bgs: string[] property, the bg strings are references to colors, e.g. canvas default.
  • the script creates a check for each specified bg
  • when testing you have colorA, colorB, bg
    • first colorB is mixed with bg to create an opaque version of colorB on bg
    • colorA is mixed with colorB to create an opaque color
    • both colors are tested against each other

@lukasoppermann lukasoppermann temporarily deployed to github-pages September 12, 2022 07:49 Inactive
@lukasoppermann lukasoppermann temporarily deployed to github-pages September 12, 2022 08:33 Inactive
@lukasoppermann lukasoppermann temporarily deployed to github-pages September 12, 2022 08:56 Inactive
@lukasoppermann lukasoppermann temporarily deployed to github-pages September 14, 2022 08:48 Inactive
@lukasoppermann lukasoppermann marked this pull request as ready for review September 14, 2022 08:53
@lukasoppermann lukasoppermann requested a review from a team as a code owner September 14, 2022 08:53
@lukasoppermann lukasoppermann requested a review from a team September 14, 2022 08:53
@lukasoppermann lukasoppermann temporarily deployed to github-pages September 14, 2022 08:55 Inactive
@lukasoppermann
Copy link
Contributor Author

Hey @rezrah I think this is ready now.
If you have an idea how to replace any of the dependencies feel free to let me know.

Also this script outputs color-contrast-check.json. This can be used for the workflow part, but I think the workflow is step 2, correct? So we merge this first and do the workflow afterwards.

script/color-contrast.ts Outdated Show resolved Hide resolved
* testContrast
* @description test the contrast of two colors against each other
* @param minimumContrast
* @param colorA
Copy link
Contributor

Choose a reason for hiding this comment

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

can we refer to the params as foreground and background for clarity?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could, but this would not be truthful for testing e.g. default text color vs. link color. I had fg vs. bg first.

Happy to adjust it again, what do you think?

*/
const renderConsoleTable = (theme: string, results: contrastTestResult[]): void => {
// config table
const contrastTable = new Table({
Copy link
Contributor

Choose a reason for hiding this comment

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

Any chance we can do this without the dependency? Appreciate it's the quicker option, but it leaves us open to security and maintenance burden over time that becomes difficult to justify for otherwise straightfoward tasks. Happy to help rewrite this in a part 2 PR if that helps too!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, lets book this for a 2nd PR please.

Reasons for the dep:

  • Table looks better
  • center aligned columns with console.table are hard to read

Copy link
Contributor

Choose a reason for hiding this comment

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

Table looks better

FYI.. worth bearing in mind that comments in the Pull Request will strip out all extra styling

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But not when using it locally.

script/lib/flattenObject.ts Outdated Show resolved Hide resolved
script/color-contrast.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@rezrah rezrah left a comment

Choose a reason for hiding this comment

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

This is great @lukasoppermann 🙌

  • To answer your question in description, I can definitely work with this in CI workflows
  • Left some comments on things we should fix before this merges. Typing errors, deps etc.
    • Happy to help fix those with you or in future PR if easier too. LMK.

Should a build fail if contrasts fail?

IMO no, because this is a side-effect and additional abstraction on top of build jobs. It would be inaccurate to suggest the build failed when it was actually a governance layer that did. We can work around it by making the contrast check fail instead for transparency.

Instead, let's focus on failing package releases:

  • Canary should always pass and ship, to avoid blocking testing
  • GA on @latest channel should fail

Thoughts?

@lukasoppermann lukasoppermann requested review from rezrah and removed request for langermank October 6, 2022 13:42
@lukasoppermann lukasoppermann temporarily deployed to github-pages October 6, 2022 13:43 Inactive
Copy link
Contributor

@rezrah rezrah left a comment

Choose a reason for hiding this comment

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

LGTM!

@lukasoppermann lukasoppermann temporarily deployed to github-pages October 7, 2022 08:38 Inactive
@lukasoppermann lukasoppermann merged commit 8d1136b into main Oct 7, 2022
@lukasoppermann lukasoppermann deleted the color-contrast branch October 7, 2022 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants