java图像颜色公式

标签 java colors

我知道灰度的 java 代码是这样的( 0.2126 * red + 0.7152 * green + 0.0722 * blue( 我想知道是否有人知道我如何找到更多种类的着色公式,比如如果我想让图片以老式方式制作,更橙色,使其更亮或更暗......更锐利等等

 int pixel = image.getRGB(j, i);
 int red = (pixel) & 0xff;
 int green = (pixel >> 8) & 0xff;
 int blue = (pixel >> 16) & 0xff;
 int newPixel = (int) (0.2126 * red  + 0.7152 * green + 0.0722 * blue);
                    image1.setRGB(j, i, newPixel);

最佳答案

您提到的旧时尚方式称为“棕褐色”效果。看看this question特别是this answer它指向以下代码片段(请注意,我没有编写此代码,只是帮助找到您问题的答案)

/**
*
* @param img Image to modify
* @param sepiaIntensity From 0-255, 30 produces nice results
* @throws Exception
*/
public static void applySepiaFilter(BufferedImage img, int
sepiaIntensity) throws Exception
{
// Play around with this. 20 works well and was recommended
// by another developer. 0 produces a grey image
int sepiaDepth = 20;

int w = img.getWidth();
int h = img.getHeight();

WritableRaster raster = img.getRaster();

// We need 3 integers (for R,G,B color values) per pixel.
int[] pixels = new int[w*h*3];
raster.getPixels(0, 0, w, h, pixels);

// Process 3 ints at a time for each pixel. Each pixel has 3 RGB
colors in array
for (int i=0;i<pixels.length; i+=3)
{
int r = pixels[i];
int g = pixels[i+1];
int b = pixels[i+2];

int gry = (r + g + b) / 3;
r = g = b = gry;
r = r + (sepiaDepth * 2);
g = g + sepiaDepth;

if (r>255) r=255;
if (g>255) g=255;
if (b>255) b=255;

// Darken blue color to increase sepia effect
b-= sepiaIntensity;

// normalize if out of bounds
if (b<0) b=0;
if (b>255) b=255;

pixels[i] = r;
pixels[i+1]= g;
pixels[i+2] = b;
}
raster.setPixels(0, 0, w, h, pixels);
}

关于java图像颜色公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11555533/

相关文章:

JavaFX - ErrorStream 到标签中

jquery - 虹膜颜色选择器

ios - 如何在 XCode 的 Interface Builder 中定义颜色?

java - Eclipse 无法找到 XMLOutputFactory 的最新 Java 1.6 方法

java - 错位的构造

java - 构建类对象并定义变量

java - 将鼠标保持在 Ellipse2D 内

jquery - CSS3 - 修复透明导航 : Is there a way to color an element depending on the actually visible background of the element?

python - 如何在 python 2.7.3 中更改文本颜色

java - 如何在AndEngine Rectangle类中实现glColorPointer