Skip to content

Revise color functions and add Sass testing #3966

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

Merged
merged 61 commits into from
Feb 4, 2021

Conversation

thisisdano
Copy link
Contributor

@thisisdano thisisdano commented Feb 2, 2021

  • Adds sass-true for Mocha-based Sass unit testing (npm run test:sass)
  • Add standalone test script for selected Sass functions
  • Add options $context data to color-picker functions for clearer error messaging
  • Tried to address many edge cases, and test for them
  • Improves performance of color calculations
  • Improves error messaging
  • Demotes most color errors to warnings, with predictable fallbacks
  • Continues to throw errors for transparent color calculations
  • Adds special error handling to output some error messages as strings for testing purposes
  • Adds new helper functions for repeatable, single-purpose calculations:
    • is-accessible-magic-number(): Determines if two grades achieve a specified WCAG level
    • color-token-family(): Outputs the family of a color token or split
    • color-token-grade(): Outputs the grade (system or theme grade) of a color token or split. Note that this is different from calculate-grade() which will output the absolute system grade for any color.
    • color-token-type(): Outputs the type ("system" or "theme") of a color token
    • color-token-variant(): Outputs the variant ("vivid") of a color token or split
    • magic-number(): Outputs the magic number between two grades
    • next-token(): Gets the next color token "darker" or "lighter" from a specified color

New settings

$theme-body-background-color: "white"
$theme-text-color: "ink"
$theme-text-reverse-color: "white"
$theme-alert-text-color: default
$theme-alert-text-reverse-color: default
$theme-alert-link-color: default
$theme-alert-link-reverse-color: default

Updated settings

$theme-breadcrumb-background-color: "default"
$theme-identifier-primary-link-color: default

NOTE 1: default means to use the value of the applicable text or link default.

NOTE 2: The updated settings in breadcrumb mean that it will use "dark mode" if the value of $theme-body-background-color changes to a dark shade.

Alert includes both standard and reverse color options since different alerts may need to use one or the other. For most components, there's only a single color, which is the preferred color for all instances, which are assumed to be the same.

…eliable output

- Use a simpler technique to find "next grade"
- Output a custom grade in the midpoint of USWDS grades for any grade that falls between (ie 15 (between 10 and 20) or 2.5 (between 0 and 5))
- Use alpha instead of opacity because of function collision
- Throw an error if not a valid token
- remove get-custom-color-split(), redundant with decompose-color-token()
- rename "custom-color" functions to "color-token"
- Add tests for token checking functions
Copy link
Contributor

@mejiaj mejiaj left a comment

Choose a reason for hiding this comment

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

Overall looks good. I think users will be happy this takes the preferred link color.

Testing:

$theme-color-base-lightest: #f2f2f2;
$theme-color-base-lighter: #dcdcdc;
$theme-color-base: #707070;
$theme-color-base-dark: #535353;
$theme-color-base-darkest: #141414;
$theme-color-base-ink: #141414;
WARNING: [Alert (emergency)] Neither the specified preferred color token (`ink`) nor the fallback color token (`base-lighter`) have AA contrast on a `emergency` background. Using `ink`. Please check your source code and project settings.
    src/stylesheets/core/_functions.scss 1895:5                 get-color-token-from-bg()
    src/stylesheets/core/_functions.scss 1960:27                get-link-tokens-from-bg()
    src/stylesheets/core/mixins/_set-link-from-bg.scss 8:17     set-link-from-bg()
    src/stylesheets/core/mixins/_alert-status-styles.scss 30:7  alert-status-styles()
    src/stylesheets/components/_alerts.scss 83:5                @import
    src/stylesheets/packages/_uswds-components.scss 25:9        @import
    src/stylesheets/uswds.scss 13:9                             root stylesheet

image

Which is expected since we have ink as the preferred color.


$theme-color-accent-warm: #ff6839;
$theme-color-base-ink: #292a2b;
WARNING: [Button] Neither the specified preferred color token (`white`) nor the fallback color token (`ink`) have AA contrast on a `accent-cool-dark` background. Using `white`. Please check your source code and project settings.
    src/stylesheets/core/_functions.scss 1895:5              get-color-token-from-bg()
    src/stylesheets/core/mixins/_set-text-from-bg.scss 8:28  set-text-from-bg()
    src/stylesheets/core/mixins/_set-text-from-bg.scss 25:3  set-text-and-bg()
    src/stylesheets/elements/_buttons.scss 63:5              @import
    src/stylesheets/packages/_uswds-components.scss 10:9     @import
    src/stylesheets/uswds.scss 13:9                          root stylesheet

WARNING: [Button] Neither the specified preferred color token (`white`) nor the fallback color token (`ink`) have AA contrast on a `accent-warm` background. Using `white`. Please check your source code and project settings.
    src/stylesheets/core/_functions.scss 1895:5              get-color-token-from-bg()
    src/stylesheets/core/mixins/_set-text-from-bg.scss 8:28  set-text-from-bg()
    src/stylesheets/core/mixins/_set-text-from-bg.scss 25:3  set-text-and-bg()
    src/stylesheets/elements/_buttons.scss 73:3              @import
    src/stylesheets/packages/_uswds-components.scss 10:9     @import
    src/stylesheets/uswds.scss 13:9                          root stylesheet

WARNING: [Button] Neither the specified preferred color token (`white`) nor the fallback color token (`ink`) have AA contrast on a `accent-warm` background. Using `white`. Please check your source code and project settings.
    src/stylesheets/core/_functions.scss 1895:5              get-color-token-from-bg()
    src/stylesheets/core/mixins/_set-text-from-bg.scss 8:28  set-text-from-bg()
    src/stylesheets/core/mixins/_set-text-from-bg.scss 25:3  set-text-and-bg()
    src/stylesheets/elements/_buttons.scss 76:5              @import
    src/stylesheets/packages/_uswds-components.scss 10:9     @import
    src/stylesheets/uswds.scss 13:9                          root stylesheet

image

image

Again, doesn't pass AA but goes with preferred color.


What I liked about the previous function is it fell back to the most accessible color if it didn't pass.

@thisisdano
Copy link
Contributor Author

I updated the function to pick the better of the two options if neither of them pass. So the order is

  1. Preferred
  2. Fallback
  3. Highest magic number

@thisisdano thisisdano merged commit b18b43b into jm-color-contrast-3906 Feb 4, 2021
@thisisdano thisisdano deleted the dw-color-functions branch February 4, 2021 00:27
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.

2 participants