gpt4 book ai didi

Change image color without CSS filter(在不使用CSS滤镜的情况下更改图像颜色)

转载 作者:bug小助手 更新时间:2023-10-24 22:08:10 26 4
gpt4 key购买 nike



I would like to change the color of an image with CSS.

我想用css改变图像的颜色。


The problem is that it cannot be with filters, I usually use this code:

问题是它不能与过滤器一起使用,我通常使用以下代码:


filter: hue-rotate(339deg) brightness(1);

It works, but if the image is black for example it does not work, and if it has colors it only shows variations of X color. I would like to be able to change all the colors.

它可以工作,但如果图像是黑色的,它就不能工作,如果它有颜色,它只显示X颜色的变化。我希望能够改变所有的颜色。


更多回答

is the image an SVG?

这张图片是SVG吗?

The image is PNG. Why?

图片为PNG。为什么?

@PrincipeMestizo What are you trying to do?

@PrincpeMstizo你想做什么?

I wanna this: webdevtrick.com/demos/css-filter-editor But... that instead of having so many filters it has only one to change the color.

我想要这个:webdevtrick.com/demos/css-Filter-EDITOR,但是...而不是有这么多滤镜,它只有一个改变颜色。

The only thing I want is to be able to change all the colors. Example: if the image is black, I want to put it red, blue, yellow, or any other color.

我唯一想要的就是能够改变所有的颜色。例如:如果图像是黑色,我希望将其设置为红色、蓝色、黄色或任何其他颜色。

优秀答案推荐

You can apply a color filter on an image by:

您可以通过以下方式在图像上应用滤色器:



  • positioning a layer with a partially transparent background (with the use of rgba())


Demo:

演示:




const layer = document.querySelector('.layer');
input.addEventListener('input', function() {
const r = parseInt(input.value.substr(1, 2), 16)
const g = parseInt(input.value.substr(3, 2), 16)
const b = parseInt(input.value.substr(5, 2), 16)
layer.style.backgroundColor = `rgb(${r}, ${g}, ${b}, 0.9)`;
})

.container {
position: relative;
width:fit-content;
}

.layer {
position: absolute;
top: 0;
left: 0;
height:100%;
width:100%;
}

.img {
height: 100px;
width: 100px;
background: url("https://i.imgur.com/0etSSYl.png");
background-size: cover;
}

<div class="container">
<div class="layer"></div>
<div class="img"></div>
</div>

<hr>
<input type="color" id="input">
<-- apply color filter




更多回答

I have an <img src="... it's possible with that?

我有一个

@PrincipeMestizo You would have to make it black & white, which is not possible without using CSS filter.

@PrincpeMestizo您必须将其设置为黑白,如果不使用css滤镜,这是不可能的。

@PrincipeMestizo It looks slightly confusing, but really it's just simple string manipulation that's specific to this scenario. Since input type="color" returns the value in hex, we have to extract the RGB values to make the background partially transparent since hex doesn't support partially transparent colors.

@PrincpeMestizo它看起来有点令人困惑,但实际上它只是特定于此场景的简单字符串操作。因为INPUT TYPE=“COLOR”返回十六进制的值,所以我们必须提取RGB值来使背景部分透明,因为十六进制不支持部分透明的颜色。

This method does not work for me, because the entire image is filled, even the transparent part.

这种方法对我不起作用,因为整个图像都是填充的,即使是透明的部分。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com