Change color of PNG image via CSS?

后端 未结 16 1352
自闭症患者
自闭症患者 2020-11-22 07:33

Given a transparent PNG displaying a simple shape in white, is it possible to somehow change the color of this through CSS? Some kind of overlay or what not?

16条回答
  •  有刺的猬
    2020-11-22 07:46

    I've been able to do this using SVG filter. You can write a filter that multiplies the color of source image with the color you want to change to. In the code snippet below, flood-color is the color we want to change image color to (which is Red in this case.) feComposite tells the filter how we're processing the color. The formula for feComposite with arithmetic is (k1*i1*i2 + k2*i1 + k3*i2 + k4) where i1 and i2 are input colors for in/in2 accordingly. So specifying only k1=1 means it will do just i1*i2, which means multiplying both input colors together.

    Note: This only works with HTML5 since this is using inline SVG. But I think you might be able to make this work with older browser by putting SVG in a separate file. I haven't tried that approach yet.

    Here's the snippet:

    
      
        
          
          
        
      
      
    
    
      
        
          
          
        
      
      
    
    
      
        
          
          
        
      
      
    

提交回复
热议问题