简体   繁体   English

如何为具有透明背景的本地图标图像着色?

[英]How to tint local icon image with transparent background?

I have looked into multiple solutions, but I could not find one that is suitable for what I need.我研究了多种解决方案,但找不到适合我需要的解决方案。

I have this local image我有这个本地图片

在此处输入图片说明 When I hover the image I need to change only the black background to my theme color.当我悬停图像时,我只需要将黑色背景更改为我的主题颜色。 A hover image is not a good solution because the theme color changes and it's difficult to get from the designer an image each time.悬停图像不是一个好的解决方案,因为主题颜色会发生变化,并且每次都很难从设计师那里获得图像。

I have tried drawing the local image from both local source and from hidden div.我尝试从本地源和隐藏 div 中绘制本地图像。 And after that reading bit by bit and changing only the black bits to my theme color.然后一点一点地阅读并只将黑色部分更改为我的主题颜色。

<div style='display: block'>
    <img id='img' src='back-icon-64.png' width='32' height='32' />
</div>
<canvas id="canvas" width=32 height=32>

The JavaScript JavaScript

function drawImage() {

    var hiddenImg = document.getElementById('img')
    var canvas = document.getElementById("canvas")
    var ctx = canvas.getContext("2d")

    var image = new Image(32, 32)
    image.src = hiddenImg.src
    image.onload = function () {
        ctx.drawImage(image, 0, 0)
        var data = ctx.getImageData(0, 0, 32, 32)
    }
}

function recolor(ctx) {

    var imgData = ctx.getImageData(0, 0, 32, 32);
    var data = imgData.data;

    for (var i = 0; i < data.length; i += 4) {
        red = data[i + 0];
        green = data[i + 1];
        blue = data[i + 2];
        alpha = data[i + 3];
        //console.log("rgba" + red + green, blue, alpha)
        console.log("rgba")
    }
}
drawImage()

Although I tried to set headers I am getting this error in the console:虽然我尝试设置标题,但我在控制台中收到此错误:

Uncaught DOMException: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.未捕获的 DOMException:无法在“CanvasRenderingContext2D”上执行“getImageData”:画布已被跨域数据污染。

An working fiddle will be really helpful.一个工作小提琴将非常有帮助。

Thank you!谢谢!

You can consider mix-blend-mode (you won't have transparency as you will need a white backgroud under the image)你可以考虑mix-blend-mode (你不会有透明度,因为你需要图像下的白色背景)

 .icon { width:64px; height:64px; display:inline-block; background:url(https://i.stack.imgur.com/Vi3wa.png)center/cover, #fff; position:relative; } .icon:hover::before { content:""; position:absolute; top:0; left:0; right:0; bottom:0; background:var(--c,red); mix-blend-mode:lighten; }
 <div class="icon"> </div> <div class="icon" style="--c:blue;"> </div> <div class="icon" style="--c:green;"> </div>

Selects the lighter of the backdrop and source colors.选择背景色和源色中较亮的颜色。

The backdrop is replaced with the source where the source is lighter;背景替换为光源较亮的光源; otherwise, it is left unchanged.否则,它保持不变。

https://drafts.fxtf.org/compositing-1/#valdef-blend-mode-lighten https://drafts.fxtf.org/compositing-1/#valdef-blend-mode-lighten

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM