首页 > 解决方案 > 使用一组 2D 点进行颜色插值

问题描述

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

标签: colorsinterpolation

解决方案


我用javascript写了这个:https ://jsfiddle.net/87nw05kz/

重要的部分是计算颜色。它找到被着色的像素到每种颜色的距离的平方反比,并使用它来确定每种颜色应影响该像素的程度。

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};
}

推荐阅读