-
Notifications
You must be signed in to change notification settings - Fork 841
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
Set up sass functions folder, add new functions for color contrast #628
Changes from 3 commits
2c8125f
1183ee8
0e91f7d
a94f4f1
c61d43e
a8ccc1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Mixes a provided color with white. | ||
@function tint($color, $percent) { | ||
@return mix(#fff, $color, $percent); | ||
} | ||
|
||
// Mixes a provided color with black. | ||
@function shade($color, $percent) { | ||
@return mix(#000, $color, $percent); | ||
} | ||
|
||
// For theming. Checks the text color and tells us whether it's light or dark. | ||
// Based on that we either tint (add white) or shade (add black). | ||
@function tintOrShade($color, $tint, $shade) { | ||
@if (lightness($euiTextColor) > 50) { | ||
@return shade($color, $shade); | ||
} @else { | ||
@return tint($color, $tint); | ||
} | ||
} | ||
|
||
// The reverse of the above | ||
@function shadeOrTint($color, $shade, $tint) { | ||
@if (lightness($euiTextColor) < 50) { | ||
@return shade($color, $shade); | ||
} @else { | ||
@return tint($color, $tint); | ||
} | ||
} | ||
|
||
// Calculates luminance, which is better than brightness for checking colors | ||
// pow, nth functions come from the _math.scss functions | ||
@function luminance($color) { | ||
// Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef | ||
$rgba: red($color), green($color), blue($color); | ||
$rgba2: (); | ||
|
||
@for $i from 1 through 3 { | ||
$rgb: nth($rgba, $i); | ||
$rgb: $rgb / 255; | ||
|
||
$rgb: if($rgb < .03928, $rgb / 12.92, pow(($rgb + .055) / 1.055, 2.4)); | ||
|
||
$rgba2: append($rgba2, $rgb); | ||
} | ||
|
||
@return .2126 * nth($rgba2, 1) + .7152 * nth($rgba2, 2) + 0.0722 * nth($rgba2, 3); | ||
} | ||
|
||
// Calculate contrast | ||
@function contrastRatio($background, $foreground) { | ||
$backgroundLum: luminance($background) + .05; | ||
$foregroundLum: luminance($foreground) + .05; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, why the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef It's part of the formula for calculating contrast ratios when given relative luminance. To be honest, I barely understand how the math works, but I did check using |
||
|
||
@return max($backgroundLum, $foregroundLum) / min($backgroundLum, $foregroundLum); | ||
} | ||
|
||
// Compare the contrast against both light and white text, then pick the highest. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compare against 'light and dark' or 'white and black'? I'm assuming it's not supposed to be 'light and white'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I think adding example usages in the comments would be really helpful. |
||
@function chooseLightOrDarkText($color, $light, $dark) { | ||
$lightContrast: contrastRatio($color, white); | ||
$darkContrast: contrastRatio($color, black); | ||
|
||
@if ($lightContrast > $darkContrast) { | ||
@return $light; | ||
} | ||
@else { | ||
@return $dark; | ||
} | ||
} | ||
|
||
// Given a color and a background, make that color AA accessibility by slightly | ||
// adjusting it till it works | ||
@function makeHighContrastColor($forground, $background) { | ||
$contrast: contrastRatio($forground, $background); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sp: |
||
|
||
$highContrastTextColor: $forground; | ||
|
||
@while ($contrast < 4.5) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Someone more engineery might have a better way to do this, but it seems to work perfectly fine and outputs decent looking results. Basically keeps adjusting the shadeOrTint by 5% of a passed color until it gets passed 4.5. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems simple enough to me. Not sure what the load is on compilation but at 5% increments I don't think it should run too long. |
||
$highContrastTextColor: shadeOrTint($highContrastTextColor, 5%, 5%); | ||
$contrast: contrastRatio($highContrastTextColor, $euiColorEmptyShade); | ||
} | ||
|
||
@return $highContrastTextColor; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@import 'math'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you just add a comment in here that 'math' needs to come first to be used in all subsequent function files? |
||
@import 'colors'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Greatest common devisor | ||
// From: http://rosettacode.org/wiki/Greatest_common_divisor#JavaScript | ||
@function gcd($a, $b) { | ||
@if ($b != 0) { | ||
@return gcd($b, $a % $b); | ||
} @else { | ||
@return abs($a); | ||
} | ||
} | ||
|
||
// Handles decimal exponents by trying to convert them into a fraction and then | ||
// use a nthRoot-algorithm for parts of the calculation | ||
@function pow($base, $exponent, $prec: 12) { | ||
@if (floor($exponent) != $exponent) { | ||
$prec2 : pow(10, $prec); | ||
$exponent: round($exponent * $prec2); | ||
$denominator: gcd($exponent, $prec2); | ||
@return nthRoot(pow($base, $exponent / $denominator), $prec2 / $denominator, $prec); | ||
} | ||
|
||
$value: $base; | ||
@if $exponent > 1 { | ||
@for $i from 2 through $exponent { | ||
$value: $value * $base; | ||
} | ||
} @else if $exponent < 1 { | ||
@for $i from 0 through -$exponent { | ||
$value: $value / $base; | ||
} | ||
} | ||
|
||
@return $value; | ||
} | ||
|
||
// From: http://rosettacode.org/wiki/Nth_root#JavaScript | ||
@function nthRoot($num, $n: 2, $prec: 12) { | ||
$x: 1; | ||
|
||
@for $i from 0 through $prec { | ||
$x: 1 / $n * (($n - 1) * $x + ($num / pow($x, $n - 1))); | ||
} | ||
|
||
@return $x; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// Core | ||
@import 'functions/index'; | ||
@import 'variables/index'; | ||
@import 'mixins/index'; | ||
@import 'reset/index'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,3 @@ | ||
// Mixes a provided color with white. | ||
@function tint($color, $percent) { | ||
@return mix(#fff, $color, $percent); | ||
} | ||
|
||
// Mixes a provided color with black. | ||
@function shade($color, $percent) { | ||
@return mix(#000, $color, $percent); | ||
} | ||
|
||
// For theming. Checks the text color and tells us whether it's light or dark. | ||
// Based on that we either tint or shade. | ||
@function tintOrShade($color, $tint, $shade) { | ||
@if (lightness($euiTextColor) > 50) { | ||
@return shade($color, $shade); | ||
} @else { | ||
@return tint($color, $tint); | ||
} | ||
} | ||
|
||
@function shadeOrTint($color, $shade, $tint) { | ||
@if (lightness($euiTextColor) < 50) { | ||
@return shade($color, $shade); | ||
} @else { | ||
@return tint($color, $tint); | ||
} | ||
} | ||
|
||
// Core | ||
|
||
$euiColorPrimary: #0079a5 !default; | ||
|
@@ -37,7 +9,7 @@ $euiColorGhost: #FFF !default; | |
// Status | ||
$euiColorSuccess: $euiColorSecondary !default; | ||
$euiColorDanger: #A30000 !default; | ||
$euiColorWarning: #CF3800 !default; | ||
$euiColorWarning: #E5830E !default; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reverts back to our "yellow" color for warnings so everything looks ok again. It becomes darker through use of the functions as we need it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
// Grays | ||
$euiColorEmptyShade: #FFF !default; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to work for the
ghost
(non-fill) text color:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.