Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

html - Applying mix-blend-mode with a grayscale

Is there a way to apply mix-blend-mode: difference with a grayscale?

The background can be any color and difference gives me the opposite text color, that can be ugly.

This snippet shows the problem:

.color {
  width: 100px
  height: 20px;
  padding: 5px;
}

.color > span {
  mix-blend-mode: difference;
  color: white;
}
<div class="color" style="background: #fff">
  <span>good</span>
</div>
<div class="color" style="background: #000">
  <span>good</span>
</div>
<div class="color" style="background: #0f0">
  <span>should be black or dark grey</span>
</div>
<div class="color" style="background: #00f">
  <span>should be white or light grey</span>
</div>
<div class="color" style="background: #f00">
  <span>should be white or light grey</span>
</div>
question from:https://stackoverflow.com/questions/65920291/applying-mix-blend-mode-with-a-grayscale

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is an approximation using filter. Not sure if it will cover all the cases.

.color {
  padding: 5px;
  background: currentColor;
}

.color>span {
  filter: invert(1)  saturate(0);
  font-weight: bold;
  font-size: 23px;
}
<div class="color" style="color: #fff">
  <span>good</span>
</div>
<div class="color" style="color: #000">
  <span>good</span>
</div>
<div class="color" style="color: #0f0">
  <span>should be black or dark grey</span>
</div>
<div class="color" style="color: #00f">
  <span>should be white or light grey</span>
</div>
<div class="color" style="color: #f00">
  <span>should be white or light grey</span>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...