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

Reuse of the color-yiq function #23114

Closed
envolute opened this issue Jul 17, 2017 · 3 comments
Closed

Reuse of the color-yiq function #23114

envolute opened this issue Jul 17, 2017 · 3 comments
Labels

Comments

@envolute
Copy link
Contributor

envolute commented Jul 17, 2017

With a simple change it is possible to use the color-yiq function with other properties, not just "color"

// Color contrast
@mixin color-yiq($color, $prop: 'color') {
  $r: red($color);
  $g: green($color);
  $b: blue($color);

  $yiq: (($r * 299) + ($g * 587) + ($b * 114)) / 1000;

  @if ($yiq >= 150) {
    #{$prop}: #111;
  } @else {
    #{$prop}: #fff;
  }
}
.element-inside-primary {
  @include color-yiq(theme-color("primary"), 'background-color');
}

Just add the parameter "$prop: 'color'" that we can use other properties
This can be interesting when you want to assign a background that contrasts with a color.

envolute pushed a commit to envolute/bootstrap that referenced this issue Aug 1, 2017
This change refers to the issue twbs#23114
@klasjersevi
Copy link

It would be much better to change it to a function, so we can use it for whatever we like. Then the colors #111 and #fff should naturally use some variables so you can customize it.
Additionally it would be nice to provide one or more mixins that utilize the function.

Function

// Color contrast
@function color-yiq($color) {
  $r: red($color);
  $g: green($color);
  $b: blue($color);

  $yiq: (($r * 299) + ($g * 587) + ($b * 114)) / 1000;

  @if ($yiq >= 150) {
    @return $text-black;
  } @else {
    @return $text-white;
  }
}

Mixins

@mixin text-color($color: $body-bg) {
  color: color-yiq($color);
}

or

@mixin color-yiq($color: $body-bg, $prop: 'color') {
  #{$prop}: color-yiq($color);
}

@XhmikosR
Copy link
Member

A PR is welcome.

@mdo
Copy link
Member

mdo commented Oct 23, 2017

Mixin was renamed to be a function in #24426.

@mdo mdo closed this as completed Oct 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants