It is necessary to determine whether the image is color or black and white. Standard tools (ColorModel, Color) give only a color mask.

I came to this decision, check the color difference and if it is more than 5 units, then I consider the image to be colored.

private boolean isColored(int pixel){ int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel ) & 0xff; return Math.abs(red - green) > 5 || Math.abs(red - blue) > 5 || Math.abs(blue - green) > 5; } 

Maybe there are better options?

  • What's the problem? Check the format; if it allows colors, go over the pixels. Double cycle, nothing extraordinary. If you use any standard tools, show the code. - VladD
  • Running through the pixels is also not a problem, but with the definition of color, I don’t know how to be, in theory, a shade of gray when r == g == b, but this may not always be the case. On the contrary, I would like to find a library that would check. - Alexander Nakoryakov
  • Well, if you have pixels in RGB, then the condition that the pixel is not colored is exactly r == g == b. If in RGBA, then r == g == b or a == 0. Or do you have a counterexample? (Well, do you really think that the library will do something radically different?) - VladD
  • That is, if you have something exotic, such as HSV, then you should check v == 0 || s == 0. - VladD
  • My task is to calculate the cost of printing, I receive pdf at the entrance, select images from it, and since the images are raster, it does not always turn out that gray is exactly r == g == b, although it looks like gray, no exotics. I thought that the library would be able to determine the deviation from the norm of gray (r == g == b), I hadn’t worked with color before, so I may not know any particularities. - Alexander Nakoryakov

0