This site requires JavaScript, please enable it in your browser!
Greenfoot back
tallkid10
tallkid10 wrote ...

2012/5/13

Changing specific colors in an image

tallkid10 tallkid10

2012/5/13

#
Hi, I am trying to make a program that would allow a user to load an image, then click a button on the side that would change the colors within the image. My friend wanted something like this so he could test different color variations quickly for some items he is drawing for a game. He made a Color palette table with 5 rows and 7 columns. Each row consists of the 7 color tied with that color palette. Row one is the "template" colors. He would load a template image and then my program would go through comparing the colors and changing them to another row's color. Hopefully that makes sense. I'm trying to use Greenfoot's getColor() and setColor() to accomplish this but when I compare the colors nothing happens. This is my color changing code,
 public void change()
    {
        int width = pic.getWidth();
        int height = pic.getHeight();
        
        temp = getPic().getTemp();  //template color
        newCol = getPic().getCol();  //color to change to
        
      //scan through pixels in image
        for(int x = 15; x < width; x++)
            for(int y = 15; y < height; y++)
            {
                here = pic.getColorAt(x,y);  //current color
                
//begin changing colors 
                if(here.equals(temp[0]))                     
                    pic.setColorAt(x,y, newCol[0]);
                else if(here.equals(temp[1]))
                    pic.setColorAt(x,y, newCol[1]);
                else if(here.equals(temp[2]))
                    pic.setColorAt(x,y, newCol[2]);
                else if(here.equals(temp[3]))
                    pic.setColorAt(x,y, newCol[3]);
                else if(here.equals(temp[4]))
                    pic.setColorAt(x,y, newCol[4]);
                else if(here.equals(temp[5]))
                    pic.setColorAt(x,y, newCol[5]);
                else if(here.equals(temp[6]))
                    pic.setColorAt(x,y, newCol[6]);
            }
        
    }
as this is howeve, nothing changes. I've seen people on google mention using BufferedImage to do something like this but this seemed a lot easier, being I have no experience with BufferedImage class or Graphics2d. Is what I'm trying to do even possible using greenfoots getColor() and setColor() ?
danpost danpost

2012/5/13

#
First, make sure the right pallete is being used for the pic that you have (if the colors do not match, no change will occur). If that is not the problem, then I would insert the following code at line 8:
for (int i = 0; i < 7; i++)
{
    System.out.println("temp[" + i + "]: Red = " + temp[i].getRed());
    System.out.println("         Blue = " + temp[i].getBlue());
    System.out.println("         Green = " + temp[i].getGreen());
    System.out.println("         Alpha = " + temp[i].getAlpha());
    System.out.println("");
    System.out.println("newCol[" + i + "]: Red = " + newCol[i].getRed());
    System.out.println("           Blue = " + newCol[i].getBlue());
    System.out.println("           Green = " + newCol[i].getGreen());
    System.out.println("           Alpha = " + newCol[i].getAlpha());
    System.out.println("");
}
to make sure the colors are being returned properly. I was wondering -- is there a reason you skip the first 15 pixels on the top and left side of 'pic'?
tallkid10 tallkid10

2012/5/13

#
Wow thanks for such a quick response. I had the x and y starting at 15 from inspecting the arrays earlier, forgot to change'm when I pasted the code here. I didn't know about the getRed/blue/green() methods, I was just trying to inspect using greenfoot (which didn't tell me much). I'm guessing they are part of the Color class? Never thought to check that. I found out that the template color wasn't entered in correctly. I changed one number and it all worked! Thanks a bunch.
You need to login to post a reply.