public class BlendablePicture extends Picture {
public BlendablePicture(String filename) {
super(filename);
}
public void blendRectWithWhite(int xMin, int yMin, int xMax, int yMax,
double a) {
int x;
x = xMin;
while (x <= xMax) {
int y;
y = yMin;
while (y <= yMax) {
Pixel refPix = this.getPixel(x, y);
refPix.setRed((int) Math.round(refPix.getRed() * (1.0 + a)));
refPix.setGreen((int) Math.round(refPix.getGreen() * (1.0 + a)));
refPix.setBlue((int) Math.round(refPix.getBlue() * (1.0 + a)));
y = y + 1;
}
}
}
}
我需要将白色与像素混合,但这段代码只是让一切变得更亮!它需要看起来像这样:
任何有关此代码的帮助将不胜感激!
最佳答案
而不是
refPix.setRed((int)Math.round(refPix.getRed()*(1.0+a)));
尝试类似的事情
refPix.setRed((int)Math.round(refPix.getRed()*(1.0-a)+255*a));
当a = 1.0时,您得到R*0.0+255*1.0 = 255
当a = 0.0时,得到R*1.0+255*0.0 = R
当a = 0.5时,你得到R*0.5+255*0.5(一半一半)
这适用于任何颜色,而不仅仅是白色,您只需将红色、绿色和蓝色的 255 替换为您想要与之混合的颜色,即可获得 RGB 平均混合。
关于java - 将图片与白色混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15373300/