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

Invert foreground color when background is light #15230

Merged
merged 6 commits into from
Sep 29, 2022
Merged

Invert foreground color when background is light #15230

merged 6 commits into from
Sep 29, 2022

Conversation

sangwoo108
Copy link
Contributor

Resolves brave/brave-browser#25392
Resolves brave/brave-browser#25393

For readability, the foreground color is inverted when the background is light. This is based on WCGA2.0 spec.

Submitter Checklist:

  • I confirm that no security/privacy review is needed, or that I have requested one
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally: npm run test -- brave_browser_tests, npm run test -- brave_unit_tests, npm run lint, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

Automated

test-unit ColorUtil.test.ts

Manual

Even when using light solid color background, we should be read stats UI easily.

Storybook

  • Go to "regular"
  • opt out "sponsored image" on right side control panel.
  • try changing background from "defaultImage" on the right side control panel.

@github-actions github-actions bot added the CI/storybook-url Deploy storybook and provide a unique URL for each build label Sep 27, 2022
@sangwoo108 sangwoo108 force-pushed the sko/color branch 5 times, most recently from 746f984 to 4759542 Compare September 27, 2022 07:30
@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

For readability, the foreground color is inverted when the background is light.
This is based on WCGA2.0 spec.
@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

Copy link
Contributor

@fallaciousreasoning fallaciousreasoning left a comment

Choose a reason for hiding this comment

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

Okay, so this looks pretty good to me but I had an idea as I was reading the last bit of the PR: What if instead of adding all of these JS props we optionally added a

--override-readability-color-rgb: 0, 0, 0;
--override-readability-color: rgb(var(--override-readability-color-rgb));

css variable. Then each component that might be overridden could do this:

styled.whatever`
    // Fall back to white
    color: var(--override-readability-color, #ffffff);
`

styled.other`
    // Fall back to --interactive9
   color: var(--override-readability-color, var(--interactive9));
`

styled.withAlpha`
    // Fallback to white, either with 0.8 opacity
    color: rgba(var(--override-readability-color-rgb, 255, 255, 255), 0.8);
`

Then we wouldn't have to pass these JS variables around all over the place!

You could set the variables in components/brave_new_tab_ui/containers/newTab/index.tsx

useEffect(() => {
	if (currentColorsAreReadable) return;

    const style = document.documentElement.style;
	style.setProperty('--override-readability-color-rgb', '0, 0, 0');
	style.setProperty('--override-readability-color', 'rgb(var(--override-readability-color))');
    return () => {
		style.removeProperty('--override-readability-color-rgb')
		style.removeProperty('--override-readability-color')
    }
}, [background, otherThingsThatChangeTheBackground]);

(you don't have to change anything, it's just an idea I came up with)

margin-right: ${p => p.textDirection === 'ltr' && '8px'};
margin-left: ${p => p.textDirection === 'rtl' && '8px'};
border-right: ${p => p.textDirection === 'ltr' && '1px solid rgba(255, 255, 255, 0.6)'};
border-left: ${p => p.textDirection === 'rtl' && '1px solid rgba(255, 255, 255, 0.6)'};
border-right: ${p => p.textDirection === 'ltr' && `1px solid ${p.color + '99' /* == 0.6 */}`};
Copy link
Contributor

Choose a reason for hiding this comment

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

It's annoying that you can destructure a color, right? There's actually a proposal for it but I don't think any browsers implement it yet.

https://www.w3.org/TR/css-color-5/#relative-RGB

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, yeah. It looks awesome. I guess it's still in WD status, so not started implementing it yet?

When we use setProperty(), a color value defined in reset.css wins
over our css variables.

In order to avoid this, use createGlobalStyle and override color value
in resets.css when we need more readable color
Copy link
Contributor Author

@sangwoo108 sangwoo108 left a comment

Choose a reason for hiding this comment

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

Thanks @fallaciousreasoning ! I love the "variable idea" so much. But there was a weird thing I couldn't understand. I made an alternative way to stick to the idea. Could you please take a look again?

margin-right: ${p => p.textDirection === 'ltr' && '8px'};
margin-left: ${p => p.textDirection === 'rtl' && '8px'};
border-right: ${p => p.textDirection === 'ltr' && '1px solid rgba(255, 255, 255, 0.6)'};
border-left: ${p => p.textDirection === 'rtl' && '1px solid rgba(255, 255, 255, 0.6)'};
border-right: ${p => p.textDirection === 'ltr' && `1px solid ${p.color + '99' /* == 0.6 */}`};
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, yeah. It looks awesome. I guess it's still in WD status, so not started implementing it yet?


// override color property in resets.css. Not sure why this happens but,
// the value in the stylesheet wins over variables above.
color: inherit
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't figure out why this happens. This happens when we declare variables with setProperty(). I tried setting property for body, but it didn't work. As a workaround, I override color here.

The previous trial can be found at 27a0d34

Screen Shot 2022-09-28 at 4 59 33 PM

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I like yours better anyway 😄 It's weird we need to set the color: inherit though - I just tested removing it and it seems to work without

css`
      --override-readability-color-rgb: 0, 0, 0;
      --override-readability-color: rgb(0, 0, 0);
`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yeah, I found the culprit. I used wrong variable for some UI.

-  color: var(--override-readability-color-rgb, #FFFFFF);  <--! oops, rgb is 0, 0, 0 -->
+  color: var(--override-readability-color, #FFFFFF);

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

Copy link
Contributor

@fallaciousreasoning fallaciousreasoning left a comment

Choose a reason for hiding this comment

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

Still LGTM @sangwoo108 - I like your solution with createGlobalStyle more than my useEffect anyway - it's much more elegant.


// override color property in resets.css. Not sure why this happens but,
// the value in the stylesheet wins over variables above.
color: inherit
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I like yours better anyway 😄 It's weird we need to set the color: inherit though - I just tested removing it and it seems to work without

css`
      --override-readability-color-rgb: 0, 0, 0;
      --override-readability-color: rgb(0, 0, 0);
`

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@sangwoo108 sangwoo108 merged commit c6023bb into master Sep 29, 2022
@sangwoo108 sangwoo108 deleted the sko/color branch September 29, 2022 10:05
@github-actions github-actions bot added this to the 1.46.x - Nightly milestone Sep 29, 2022
@sangwoo108
Copy link
Contributor Author

Thank you so much @fallaciousreasoning ! I learned a lot from you as always!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/storybook-url Deploy storybook and provide a unique URL for each build
Projects
None yet
3 participants