How to calculate required hue-rotate to generate specific colour?

后端 未结 3 864
天命终不由人
天命终不由人 2020-11-29 16:58

I have a white image that I am using as a background for a div, and I would like to colour to match the themes main colour. I am aware I can do:

filter: sepi         


        
3条回答
  •  野性不改
    2020-11-29 17:19

    The accepted answer is wrong. Hue-rotate does not conserve saturation or brightness and you have to do crazy math to come up with the correct values. The far easier way - which will result in a correct result - is to do a CSS filter that references an SVG filter. The feColorMatrix primitive in SVG filters allows you to pick a color directly - like so. Take your color #424242 - divide each color's hex value by #FF (.257) and put them in the fifth column, first three rows of your color matrix. Like so:

    div {
      background:url(http://richard.parnaby-king.co.uk/basket.svg) no-repeat scroll 0 0 transparent;
      background-size:5em;
      width:5em;
      height:5em;
      -webkit-filter: url(#colorize);
      filter: url(#colorize);
    }
    /filter>

提交回复
热议问题