egret给对象涂颜色

转载自:https://blog.csdn.net/liyaxin2010/article/details/84983980

Egret-修改Image颜色

Egret没有直接给image提供修改颜色属性,但是我们可以使用滤镜来修改image的颜色。

下面是我使用的核心代码,粘贴出来,可以直接使用

public setImageColor(image: eui.Image, color: number) {
    // 将16进制颜色分割成rgb值
    let spliceColor = (color) => {
        let result = {r: -1, g: -1, b: -1};
        result.b = color % 256;
        result.g = Math.floor((color / 256)) % 256;
        result.r = Math.floor((color / 256) / 256);
        return result;
    }
    let result = spliceColor(color);
    let colorMatrix = [
        1, 0, 0, 0, 0,
        0, 1, 0, 0, 0,
        0, 0, 1, 0, 0,
        0, 0, 0, 1, 0
    ];
    colorMatrix[0] = result.r / 255;
    colorMatrix[6] = result.g / 255;
    colorMatrix[12] = result.b / 255;
    let colorFilter = new egret.ColorMatrixFilter(colorMatrix);

    image.filters = [colorFilter];
}

关于滤镜的使用可以查看官网  http://developer.egret.com/cn/github/egret-docs/Engine2D/filter/filter/index.html

猜你喜欢

转载自blog.csdn.net/wulong710/article/details/87085829