我正在尝试理解图形。现在我陷入了色彩混合。我尝试使用一些基本算法来合并 rgb 中的颜色。现在我想像在现实生活中一样合并它们,即黄色 + 蓝色 = 绿色。我有一个这样的方法。
package com.boxonix.light.utils;
public class Utils {
public static int rgbToHex(int r, int g, int b) {
return (1048576 * r) + (255 * g) + b;
}
public static int blendPixels(int r, int g, int b, double alpha, int bgPixel){
return 0;
}
public static int getRed(int color) {
int red = Math.floorDiv(color, 1048576);
return red;
}
public static int getGreen(int color) {
int green = Math.floorDiv(color % 65536, 256);
return green;
}
public static int getBlue(int color) {
int blue = Math.floorDiv(color % 1048576, 256);
return blue;
}
}
r、g、b 是颜色,代表来自背景像素 (bgPix) 的像素,alpha 是透明度 (0.0 - 1.0)。我可以将 bgPix 转换为 r1、g1、b1。现在我需要混合它们,帮助! :D
最佳答案
听起来您正试图在某些背景色之上对前景色进行 alpha 混合。令人高兴的是,它很简单:
alpha * foregroundColor + (1 - alpha) * backgroundColor
想法是只有部分颜色来自前景色,其余的 (1 - alpha
) 来自背景。想一下就可以直观地看出这一点:如果 alpha 为 0,则所有颜色都来自背景;如果 alpha 为 1,则全部来自前台。
使用上述方法定义的示例代码:
public static int blendPixels(int r, int g, int b, double alpha, int bgPixel) {
int blendedRed = (int)Math.round(alpha * r + (1.0 - alpha) * getRed(bgPixel));
int blendedGreen = (int)Math.round(alpha * g + (1.0 - alpha) * getGreen(bgPixel));
int blendedBlue = (int)Math.round(alpha * b + (1.0 - alpha) * getBlue(bgPixel));
return rgbToHex(blendedRed, blendedGreen, blendedBlue);
}
希望对您有所帮助!
关于java - 每像素颜色混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34318650/