使用一组 2D 点进行颜色插值
Color interpolation with a set of 2D points
我正在尝试对表面光弹性的物理问题进行建模。我成功地获得了一个 X,Y 数组——表面的坐标,并且对于每个点,我都有一个 RGB 格式的相应颜色。我使用 Python 散点图进行绘图,结果已经很好了,但是由于缺乏我无法改进的分辨率,仍然存在一些不连续性 :( 我只是想问一下如何在连续方式(使用新的 "in-between" 点,它们的颜色已根据邻域点进行插值)。我不一定要在 python 中寻找编码,欢迎使用每个软件。谢谢!
我在 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};
}
我正在尝试对表面光弹性的物理问题进行建模。我成功地获得了一个 X,Y 数组——表面的坐标,并且对于每个点,我都有一个 RGB 格式的相应颜色。我使用 Python 散点图进行绘图,结果已经很好了,但是由于缺乏我无法改进的分辨率,仍然存在一些不连续性 :( 我只是想问一下如何在连续方式(使用新的 "in-between" 点,它们的颜色已根据邻域点进行插值)。我不一定要在 python 中寻找编码,欢迎使用每个软件。谢谢!
我在 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};
}