-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add $contrast as third parameter of contrast-color() function. #4
Conversation
Add $contrast as third parameter of contrast-color() function. By default contrast set to 21, to not brake legacy behavior of function.
This looks like a good idea. Still, please give me some days to think about it. There are many ways in which the API could be extended and I want to be sure that this is the best way to go. Apart of that, there are some small changes I would like to suggest:
But maybe it is best to wait with these changes until I have made up my mind. Thanks anyway for this awesome pull request! |
@xi Thanks you for awesome library. |
I thought about this for a bit. Sorry if this is a bit longer. My primary concern is that you could expect I think you can categorize the functions in the contrast module along 3 dimensions: How many input colors do they take? Which one do they select? What do they do if none of the colors provides sufficient contrast? The existing functions can then be described like this:
I thought of 2 more functions that might be a nice addition (see 272657f for an implementation):
We could then think of @function contrast-color($base, $color1, $color2, $threshold) {
$c: contrast-first($base, ($color1, $color2), $threshold);
@if $c == null {
$c: contrast-best($base, ($color1, $color2));
$c: contrast-stretch($base, $c, $threshold);
$_: contrast-check($base, $c, $threshold);
}
@return $c;
} I now have two questions:
|
Here how is see it for now (I need some time to think about it): /// Pick contrast option for a given base color
///
/// @param {color} $base the base color to compare to
/// @param {list} $colors [($planifolia-contrast-dark-default, $planifolia-contrast-light-default)] list of colors
/// @param {number} $ratio [21] satisfactory contrast ratio between 1 and 21
/// @return {color | false} color from list `$colors` or false
@function contrast-pick(
$base,
$colors: (
$planifolia-contrast-dark-default,
$planifolia-contrast-light-default,
),
$ratio: 21
) {
$ratio: _pf-threshold($ratio);
$length: length($colors);
$best: false;
@if $length < 1 {
@error "#{$colors} need minimum one item"
}
@for $i from 1 through $length {
$color: nth($colors, $i);
@if contrast($color, $base) >= $ratio {
@return $color;
}
@if $i < $length {
$nextColor: nth($colors, $i+1);
@if contrast($color, $base) >= contrast($nextColor, $base) {
$best: $color;
} @else {
$best: $nextColor;
}
}
}
@return $best;
}
.elem {
$bg: black;
$fg: contrast-pick($bg, (blue, #008A00, red), 4.5);
background: $bg;
color: $fg;
padding: 8px;
&:before {
content: "#{$fg}";
}
} |
I have thought about this again and decided that I will not merge it. The important logic of this library is in the Thanks for the idea though. I really appreciate it! |
Add $contrast as third parameter of contrast-color() function. By default contrast set to 21, to not brake legacy behavior of function. #3