3

I am trying to change image to red color using css. This is what I have so far

img {
 -webkit-filter: invert(90%); filter: invert(90%);
 }

https://jsfiddle.net/md1/

Vega
  • 27,856
  • 27
  • 95
  • 103
Jouba Ayedi
  • 51
  • 1
  • 1
  • 4
  • Have a look at https://stackoverflow.com/questions/42966641/how-to-transform-black-into-any-given-color-using-only-css-filters/43959853 from their, you can get for target `rgb(255,0,0)`: `brightness(0%) saturate(100%) invert(16%) sepia(93%) saturate(6447%) hue-rotate(1deg) brightness(96%) contrast(118%)` where I just added `brightness(0%) saturate(100%) ` to convert to black first. (https://jsfiddle.net/mhu4uczj/10/) – Kaiido May 30 '17 at 02:03
  • And since I feel the original image may not be available in future https://jsfiddle.net/mhu4uczj/15/ – Kaiido May 30 '17 at 02:18
  • 1
    Possible duplicate of [How to transform black into any given color using only CSS filters](https://stackoverflow.com/questions/42966641/how-to-transform-black-into-any-given-color-using-only-css-filters) – Rob May 30 '17 at 02:23

4 Answers4

3

you need to combine two filters saturate and hue, below is a possible combination to get a "red" (if you use a color picker it will generate the default web "red" - #FF0000 or just #F00, because you can choose your red, stronger or lighter.

img {
  webkit-filter: saturate(100) hue-rotate(-140deg);
  filter: saturate(100) hue-rotate(-140deg)
}
<img class="screencap" src="https://i.stack.imgur.com/speoA.png">

UPDATE

In the first snippet had, allegedly, a bug (after a chat with @kaiido), that was just making red in FF using Windows in tag img - while keeping pink in other browsers in other OSs, (therefore I'm going to file the bug in bugzilla)

So there is a way to do this working cross browser, using mixed filters

img {
  -webkit-filter: brightness(0.6)contrast(3.4)hue-rotate(217deg)saturate(9.9);
  filter: brightness(0.6)contrast(3.4)hue-rotate(217deg)saturate(9.9);
}
<img class="screencap" src="https://i.stack.imgur.com/speoA.png">
dippas
  • 58,591
  • 15
  • 114
  • 126
0

changing the color of a PNG image with some filter styles

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      body {
        background-color: #03030a;
        min-width: 800px;
        min-height: 400px
      }
      img {
        width: 20%;
        float: left;
        margin: 0;
      }
      /*Filter styles*/
      .saturate {
        filter: saturate(3);
        -webkit-filter: saturate(3);
      }
      .grayscale {
        filter: grayscale(100%);
        -webkit-filter: grayscale(100%);
      }
      .contrast {
        filter: contrast(160%);
        -webkit-filter: contrast(160%);
      }
      .brightness {
        filter: brightness(0.25);
        -webkit-filter: brightness(0.25);
      }
      .blur {
        filter: blur(3px);
        -webkit-filter: blur(3px);
      }
      .invert {
        filter: invert(100%);
        -webkit-filter: invert(100%);
      }
      .sepia {
        filter: sepia(100%);
        -webkit-filter: sepia(100%);
      }
      .huerotate {
        filter: hue-rotate(180deg);
        -webkit-filter: hue-rotate(180deg);
      }
      .opacity {
        filter: opacity(50%);
        -webkit-filter: opacity(50%);
      }
    </style>
  </head>
  <body>
    <h2>Change PNG image color</h2>
    <img alt="Mona Lisa" src="https://i.stack.imgur.com/OyP0g.jpg" title="original">
    <img alt="Mona Lisa" src="https://i.stack.imgur.com/OyP0g.jpg" title="saturate" class="saturate">
    <img alt="Mona Lisa" src="https://i.stack.imgur.com/OyP0g.jpg" title="grayscale" class="grayscale">
    <img alt="Mona Lisa" src="https://i.stack.imgur.com/OyP0g.jpg" title="contrast" class="contrast">
    <img alt="Mona Lisa" src="https://i.stack.imgur.com/OyP0g.jpg" title="brightness" class="brightness">
    <img alt="Mona Lisa" src="https://i.stack.imgur.com/OyP0g.jpg" title="blur" class="blur">
    <img alt="Mona Lisa" src="https://i.stack.imgur.com/OyP0g.jpg" title="invert" class="invert">
    <img alt="Mona Lisa" src="https://i.stack.imgur.com/OyP0g.jpg" title="sepia" class="sepia">
    <img alt="Mona Lisa" src="https://i.stack.imgur.com/OyP0g.jpg" title="huerotate" class="huerotate">
    <img alt="Mona Lisa" src="https://i.stack.imgur.com/OyP0g.jpg" title="opacity" class="opacity">
  </body>
</html>
MD SHAYON
  • 7,001
  • 45
  • 38
0

You have to use filter function to give color to image. for example convert white to black and vice versa, use like it filter .invert(100%).

Hamid khan
  • 37
  • 2
-1
img {
 filter:   saturate(100) hue-rotate(241.5deg);
}