0

Hi I would like to write a program that can tell me the color of a pixel base on the RGB values. Currently I am using the following code to read pixels from a buffered image. However, this program can only identify 7 colors and is very inaccurate. If possible, I want the program to be able to identify whether the color is light or dark. ie. "light red" "dark red"

        Public static void main(String args[]){
            boolean isRed=false;
            boolean isGreen=false;
            boolean isBlue=false;
            String color="";

            //read color from a color array

            blue = color[i][j].getBlue();
            red = color[i][j].getRed();
            green = color[i][j].getGreen();

            if(blue>(255-blue)) {
                isBlue=true;
            }
            if(red>(255-red)) {
                isRed=true;
            }
            if(green>(255-green)) {
                isGreen=true;
            }

            if(isRed==false&&isGreen==false&&isBlue==false) {
                color="black";
            }else if(isRed==true&&isGreen==true&&isBlue==true) {
                color="white";
            }else if(isRed==true&&isGreen==false&&isBlue==false) {
                color="red";
            }else if(isRed==false&&isGreen==true&&isBlue==false) {
                color="green";
            }else if(isRed==false&&isGreen==false&&isBlue==true) {
                color="blue";
            }else if(isRed==true&&isGreen==true&&isBlue==false) {
                color="yellow";
            }else if(isRed==false&&isGreen==true&&isBlue==true) {
                color="cyan";
            }else if(isRed==true&&isGreen==false&&isBlue==true) {
                color="magenta";
            }
            System.out.println(color);
         }
Icarus
  • 501
  • 2
  • 16
  • Are you aware that your method doesn't compile, since it's missing a loop? – Peter O. Sep 26 '19 at 21:58
  • Names of colors and their characteristics are very `subjective` and don't really have anything to do with the actual color. My recommendation would be to define you own names based on a range of individual RGB values and then use that. Remember that there can be up to `2^24` colors based on 8 bit RGB values. – WJS Sep 26 '19 at 21:58
  • sum the values of red, green and blue values then divide them by 3, this gives you the intensity of the color if it is more than 127 then it's a light color, and if it's less than 127 then it's a darker color. – Eboubaker Sep 26 '19 at 22:03
  • you must change/add more conditions,... and why `blue>(255-blue)` and not `blue > 127` ? actually why not just `boolean isBlue = blue > 127;`, no `if` needed [back to question: `isLightBlue = blue > 170`, `isDarkBlue = !isLightBlue && blue > 85;` (values must be *tweaked*) or using ZOLDIK solution together with `isBlue = blue > 85;...`] – user85421 Sep 26 '19 at 22:05
  • [this class](https://stackoverflow.com/a/20670056/10387008) should do the job, use the method getColorNameFromRgb(). – Eboubaker Sep 26 '19 at 22:11
  • 1
    If your source image has pixels generated using web colors you can use the reverse lookup. However, if you have an image (say photo jpeg) that was compressed or altered by filters then the pixel values will be off . You could use a treshold to find the nearest named color – Dr Phil Sep 26 '19 at 22:27
  • @ZOLDIK That class finds the nearest average color (but doesn't have a threshold to give up – Dr Phil Sep 26 '19 at 22:31

0 Answers0