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

feat: contrast util with yiq calculator #3652

Merged
merged 7 commits into from
Nov 7, 2022
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
2 changes: 2 additions & 0 deletions framework/core/js/src/common/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import formatNumber from './utils/formatNumber';
import mapRoutes from './utils/mapRoutes';
import withAttr from './utils/withAttr';
import * as FocusTrap from './utils/focusTrap';
import isDark from './utils/isDark';
import Notification from './models/Notification';
import User from './models/User';
import Post from './models/Post';
Expand Down Expand Up @@ -120,6 +121,7 @@ export default {
'utils/throttleDebounce': ThrottleDebounce,
'utils/isObject': isObject,
'utils/focusTrap': FocusTrap,
'utils/isDark': isDark,
'models/Notification': Notification,
'models/User': User,
'models/Post': Post,
Expand Down
21 changes: 21 additions & 0 deletions framework/core/js/src/common/utils/isDark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* The `isDark` utility converts a hex color to rgb, and then calcul a YIQ
* value in order to get the appropriate brightness value (is it dark or is it
* light?) See https://www.w3.org/TR/AERT/#color-contrast for references. A YIQ
* value >= 128 is a light color.
*/

export default function isDark(hexcolor: String) {
let hexnumbers = hexcolor.replace("#", "");

if (hexnumbers.length == 3) {
hexnumbers += hexnumbers;
}

const r = parseInt(hexnumbers.substr(0,2),16);
const g = parseInt(hexnumbers.substr(2,2),16);
const b = parseInt(hexnumbers.substr(4,2),16);
const yiq = ((r*299)+(g*587)+(b*114))/1000;

return (yiq >= 128) ? false : true;
}
7 changes: 7 additions & 0 deletions framework/core/less/common/variables.less
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
@code-color: #fff;
}

// Beyond dark or light mode, we need stable colors depending on the luminosity
// of the parents element's background. This allow not to change the color
// variable depending on the dark/light mode to get the same final color, and
// thus to simplify the logic.
@text-on-dark: #ddd;
@text-on-light: #111;

@hero-bg: @control-bg;
@hero-color: @control-color;
@hero-muted-color: @control-color;
Expand Down