带有一组2D点的颜色插值

时间:2019-04-15 18:03:28

标签: colors interpolation

我正在尝试对表面上的光弹性物理问题进行建模。我成功获得了X,Y数组-表面坐标,并且对于每个点,我都有RGB格式的相应颜色。我使用Python散点图进行绘制,结果已经不错了,但是由于缺乏分辨率而无法改善:(我只是想问一下如何以连续方式生成相同的表面图(新的“中间”点,它们的颜色已相对于邻点进行插值。)我不一定要用python进行编码,欢迎使用每种软件。谢谢!

1 个答案:

答案 0 :(得分:0)

我是用javascript写的:https://jsfiddle.net/87nw05kz/

重要的部分是calculateColor。它找到从被着色像素到每种颜色的距离的反平方,并以此来决定每种颜色应影响多少像素。

function calculateColor(x, y) {
    let total = 0;
    for (let i = 0; i < colors.length; i++) {
        let c = colors[i];
        let d = distance(c.x, c.y, x, y);
        if (d === 0) {
            return c;
        }
        d = 1 / (d * d);
        c.d = d;
        total += d;
    }
    let r = 0, g = 0, b = 0;
    for (let i = 0; i < colors.length; i++) {
        let c = colors[i];
        let ratio = c.d / total;
        r += ratio * c.r;
        g += ratio * c.g;
        b += ratio * c.b;
    }
    r = Math.floor(r);
    g = Math.floor(g);
    b = Math.floor(b);
    return {r:r,g:g,b:b};
}
相关问题