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

[Amsterdam] Saturating colors and providing contrast mixin for text. #2873

Merged
merged 9 commits into from
Feb 25, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}

.guideRule__caption {
color: $euiColorSuccess;
color: $euiColorSuccessText;
}
}

Expand Down
75 changes: 64 additions & 11 deletions src-docs/src/views/guidelines/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
EuiFormRow,
EuiCode,
EuiSwitch,
EuiCallOut,
} from '../../../../src/components';

const allowedColors = [
Expand All @@ -41,6 +42,21 @@ const allowedColors = [
'euiColorAccent',
];

const textVariants = [
'euiColorEmptyShade',
'euiColorLightestShade',
'euiColorLightShade',
'euiColorMediumShade',
'euiColorDarkShade',
'euiColorDarkestShade',
'euiColorFullShade',
'euiColorPrimaryText',
'euiColorSecondaryText',
'euiColorWarningText',
'euiColorDangerText',
'euiColorAccentText',
];

const ratingAAA = <EuiBadge color="#000">AAA</EuiBadge>;

const ratingAA = <EuiBadge color="#333">AA</EuiBadge>;
Expand Down Expand Up @@ -136,6 +152,7 @@ export default class extends Component {
this.state = {
value: '3',
behindTextVariant: false,
showTextVariants: true,
};
}

Expand All @@ -151,24 +168,39 @@ export default class extends Component {
});
};

onTextVariantChange = e => {
this.setState({
showTextVariants: e.target.checked,
});
};

render() {
const { value, behindTextVariant } = this.state;
const { value, behindTextVariant, showTextVariants } = this.state;
const { selectedTheme } = this.props;

let palette;
if (selectedTheme === 'amsterdam-dark') {
palette = darkAmsterdamColors;
} else if (selectedTheme === 'amsterdam-light') {
palette = { ...lightColors, ...lightAmsterdamColors };
} else if (selectedTheme === 'dark') {
palette = darkColors;
} else {
palette = lightColors;
switch (selectedTheme) {
case 'amsterdam-dark':
palette = darkAmsterdamColors;
break;
case 'amsterdam-light':
palette = { ...lightColors, ...lightAmsterdamColors };
break;
case 'dark':
palette = darkColors;
break;
default:
palette = lightColors;
break;
}

// Vis colors are the same for all palettes
const visColors = lightColors.euiPaletteColorBlind;
const visColorKeys = Object.keys(lightColors.euiPaletteColorBlind);
const colorsForContrast =
showTextVariants && selectedTheme === 'amsterdam-light'
? textVariants
: allowedColors;

function getContrastRatings(color1, color2) {
if (color1.indexOf('Shade') === -1 && color2.indexOf('Shade') === -1) {
Expand Down Expand Up @@ -238,6 +270,27 @@ export default class extends Component {
combination that is <EuiBadge color="#333">AA</EuiBadge> or above
with the exception of using large text.
</p>
{selectedTheme === 'amsterdam-light' && (
<EuiCallOut
color="warning"
iconType="accessibility"
title="Amsterdam changes in contrast levels">
<p>
The Amsterdam theme introduces a more vibrant core color
palette. In order to maintain a WCAG contrast of at least 4.5
you should use the text variants of the core color variables
such as&nbsp;
<EuiCode>$euiColorSecondaryText</EuiCode>. Components that
render text such as <EuiCode>EuiText</EuiCode> will do so on
their own without any extra configuration.
</p>
<EuiSwitch
label="Show text variant"
checked={showTextVariants}
onChange={this.onTextVariantChange}
/>
</EuiCallOut>
)}
<h3>Rating definitions</h3>
<ul>
<li>
Expand All @@ -259,7 +312,7 @@ export default class extends Component {
<EuiSpacer size="xxl" />

<EuiFlexGroup className="eui-textCenter" justifyContent="center">
<EuiFlexItem grow={false}>
<EuiFlexItem style={{ maxWidth: 400 }}>
<EuiFormRow
id="ratingsRange"
label="Minimum color contrast combinations to show">
Expand Down Expand Up @@ -287,7 +340,7 @@ export default class extends Component {
<EuiFlexItem key={index}>
<EuiText size="xs">
<h3>{color}</h3>
{allowedColors.map(function(color2, index) {
{colorsForContrast.map(function(color2, index) {
const contrastRatings = getContrastRatings(color, color2);

if (!contrastRatings || contrastRatings.contrast < value) {
Expand Down
11 changes: 7 additions & 4 deletions src/components/badge/notification_badge/_notification_badge.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
flex-shrink: 0; // Ensures it never scales down below its intended size
display: inline-block;
border-radius: $euiBorderRadius;
background: $euiColorAccent;
color: $euiColorEmptyShade;
font-size: $euiFontSizeXS;
font-weight: $euiFontWeightMedium;
line-height: $euiSize;
Expand All @@ -15,6 +13,10 @@
text-align: center;
transition: all $euiAnimSpeedFast ease-in;
cursor: default;

$backgroundColor: $euiColorAccent;
background: $backgroundColor;
color: chooseLightOrDarkText($backgroundColor, $euiColorGhost, $euiColorInk);
}

.euiNotificationBadge--medium {
Expand All @@ -26,6 +28,7 @@
}

.euiNotificationBadge--subdued {
background-color: tint($euiColorLightShade, 30%);
color: $euiColorFullShade;
$backgroundColor: tint($euiColorLightShade, 30%);
background: $backgroundColor;
color: chooseLightOrDarkText($backgroundColor, $euiColorGhost, $euiColorInk);
}
12 changes: 6 additions & 6 deletions src/components/expression/_variables.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Modifier naming and colors.
$euiExpressionColors: (
subdued: $euiColorMediumShade,
primary: $euiColorPrimary,
secondary: $euiColorSecondary,
warning: $euiColorWarning,
danger: $euiColorDanger,
accent: $euiColorAccent,
subdued: $euiColorDarkShade,
primary: $euiColorPrimaryText,
secondary: $euiColorSecondaryText,
warning: $euiColorWarningText,
danger: $euiColorDangerText,
accent: $euiColorAccentText,
);
2 changes: 1 addition & 1 deletion src/components/icon/_icon.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

// This provides the default secondary color
.euiIcon__fillSecondary {
fill: $euiColorSecondary;
fill: makeGraphicColor($euiColorSecondary);
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/components/icon/_variables.scss
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
$euiIconLoadingOpacity: .05;

// Modifier naming and colors.

$euiIconColors: (
text: $euiTextColor,
primary: $euiColorPrimary,
subdued: $euiColorMediumShade,
secondary: $euiColorSecondary,
success: $euiColorSuccess,
accent: $euiColorAccent,
warning: $euiColorWarning,
danger: $euiColorDanger,
subdued: makeGraphicColor($euiColorMediumShade),
primary: makeGraphicColor($euiColorPrimary),
secondary: makeGraphicColor($euiColorSecondary),
success: makeGraphicColor($euiColorSuccess),
accent: makeGraphicColor($euiColorAccent),
warning: makeGraphicColor($euiColorWarning),
danger: makeGraphicColor($euiColorDanger),
ghost: $euiColorGhost,
);

Expand Down
12 changes: 6 additions & 6 deletions src/components/link/_link.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
$textColors: (
subdued: $euiColorDarkShade,
primary: $euiColorPrimary,
secondary: $euiColorSecondary,
accent: $euiColorAccent,
warning: $euiColorWarning,
danger: $euiColorDanger,
primary: $euiColorPrimaryText,
secondary: $euiColorSecondaryText,
accent: $euiColorAccentText,
warning: $euiColorWarningText,
danger: $euiColorDangerText,
text: $euiTextColor,
ghost: $euiColorGhost,
);
Expand Down Expand Up @@ -41,4 +41,4 @@ $textColors: (
// Make button EuiLink's text selectable
button.euiLink { // sass-lint:disable-line no-qualifying-elements
user-select: text;
}
}
10 changes: 5 additions & 5 deletions src/components/stat/_stat.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Modifier naming and colors.
$titleColors: (
subdued: $euiColorDarkShade,
primary: $euiColorPrimary,
secondary: $euiColorSecondary,
danger: $euiColorDanger,
accent: $euiColorAccent,
primary: $euiColorPrimaryText,
secondary: $euiColorSecondaryText,
danger: $euiColorDangerText,
accent: $euiColorAccentText,
);

.euiStat__title {
Expand Down Expand Up @@ -47,4 +47,4 @@
0% { opacity: 1; }
50% { opacity: .25; }
100% { opacity: 1; }
}
}
12 changes: 6 additions & 6 deletions src/components/toast/_toast.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@
}

// Modifier naming and colors.
$toastTypes: (
primary: $euiColorPrimary,
success: $euiColorSecondary,
warning: $euiColorWarning,
danger: $euiColorDanger,
$euiToastTypes: (
primary: makeGraphicColor($euiColorPrimary, $euiColorEmptyShade),
success: makeGraphicColor($euiColorSecondary, $euiColorEmptyShade),
warning: makeGraphicColor($euiColorWarning, $euiColorEmptyShade),
danger: makeGraphicColor($euiColorDanger, $euiColorEmptyShade),
);

// Create button modifiders based upon the map.
@each $name, $color in $toastTypes {
@each $name, $color in $euiToastTypes {
.euiToast--#{$name} {
border-top: 2px solid $color;
}
Expand Down
11 changes: 9 additions & 2 deletions src/global_styling/functions/_colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@

// Given a $foreground and a $background, make the $foreground AA accessibility by slightly
// adjusting it till the contrast is high enough
// By default it will compare against the page background color

// ex: makeContrastColor($lightPink, #FFF) would continually shade the pink until
// it had higher than 4.5 contrast on a white background.
@function makeHighContrastColor($foreground, $background) {
@function makeHighContrastColor($foreground, $background: $euiPageBackgroundColor, $ratio: 4.5) {
$contrast: contrastRatio($foreground, $background);

// Determine the lightness factor of the background color first to
Expand All @@ -97,7 +98,7 @@

$highContrastTextColor: $foreground;

@while ($contrast < 4.5) {
@while ($contrast < $ratio) {
@if ($brightness > 50) {
$highContrastTextColor: shade($highContrastTextColor, 5%);
} @else {
Expand All @@ -119,3 +120,9 @@

@return $highContrastTextColor;
}

// Non-textual elements only need a contrast ration of 3:1
Copy link
Contributor

Choose a reason for hiding this comment

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

Even if we aren't adding this to the guidelines, I'd add some more explanation to why this is used. Assumption is someone will see this in use in the existing code, grep for the function and see that it calculates lower, but when is it a good time to use this over makeHighContrastColor. I know it sounds silly, but just say something like... "This is useful for icons so they don't get desaturated colors, but are still readable"...etc.

I'd also consider renaming it to makeGraphicContrastColor to better signal this is a contrast calculating function similar to makrHighContrastColor.

@function makeGraphicColor($color, $background: $euiPageBackgroundColor) {
@return makeHighContrastColor($color, $background, 3);
}

12 changes: 11 additions & 1 deletion src/global_styling/variables/_colors.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This extra import allows any variables that are created via functions to work when loaded into JS
@import '../functions/index';

// Core
$euiColorPrimary: #006BB4 !default;
$euiColorSecondary: #017D73 !default;
Expand All @@ -23,13 +26,20 @@ $euiColorDarkestShade: #343741 !default;
$euiColorFullShade: #000 !default;

// Every color below must be based mathmatically on the set above.

$euiTextColor: $euiColorDarkestShade !default;
$euiTitleColor: shadeOrTint($euiTextColor, 50%, 0%) !default;
$euiLinkColor: $euiColorPrimary !default;
$euiFocusBackgroundColor: tintOrShade($euiColorPrimary, 90%, 50%) !default;
$euiPageBackgroundColor: tintOrShade($euiColorLightestShade, 50%, 30%) !default;

// Constrasty text variants
$euiColorPrimaryText: makeHighContrastColor($euiColorPrimary) !default;
$euiColorSecondaryText: makeHighContrastColor($euiColorSecondary) !default;
$euiColorAccentText: makeHighContrastColor($euiColorAccent) !default;
$euiColorWarningText: makeHighContrastColor($euiColorWarning) !default;
$euiColorDangerText: makeHighContrastColor($euiColorDanger) !default;
$euiColorSuccessText: $euiColorSecondaryText !default;

// Visualization colors

// Maps allow for easier JSON usage
Expand Down
12 changes: 11 additions & 1 deletion src/themes/eui-amsterdam/eui_amsterdam_colors_light.scss
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
$euiColorPrimary: #006DE4;
// This extra import allows any variables that are created via functions to work when loaded into JS
@import '../../global_styling/variables/colors';

$euiColorPrimary: #006DE4;
$euiColorSecondary: #00BFB3;
$euiColorAccent: #FC358E;

// Constrasty text variants
$euiColorPrimaryText: makeHighContrastColor($euiColorPrimary);
$euiColorSecondaryText: makeHighContrastColor($euiColorSecondary);
$euiColorAccentText: makeHighContrastColor($euiColorAccent);