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

2012/5/27

How to remove a color of an image for another one?

USBest USBest

2012/5/27

#
Hey guys! I've got an image that has some red parts. How can I paint all red parts blue? The Color-code of the red part is 178,34,34 And the Color-code of the blue color is 30,144,255
danpost danpost

2012/5/27

#
Use the GreenfootImage methods 'getColorAt(int, int)' and 'setColorAt(int, int, Color)'. On each pixel in the image, if getColorAt equals red color, setColorAt blue color.
USBest USBest

2012/5/27

#
Is there no faster method, than checking all pixel?
danpost danpost

2012/5/27

#
Not programmatically. You could use a graphics editor to fill the red with the blue and save the image under a different name. Is that what you are asking about?
USBest USBest

2012/5/27

#
No, I meant programmaticall... I just thought, there might be a better solution with the Graphics2D class or with another 2DGraphic class...
danpost danpost

2012/5/27

#
It would not run significantly faster, if there was a method in one of those classes; and there would be little to gain by going that route. Just use a simple double for loop.
Color fromColor = new Color(178, 34, 34);
Color toColor = new Color(30, 144, 255);
GreenfootImage image = new GreenfootImage("ImageFilename");
for (int i = 0; i < image.getWidth(); i++)
{
    for (int j = 0; j < image.getHeight(); j++)
    {
        if (fromColor.equals(image.getColorAt(i, j))) image.setColorAt(i, j, toColor);
    }
}
USBest USBest

2012/5/27

#
well...ok! Thank you danpost! :)
You need to login to post a reply.