color negative

Hi all,
I'm trying to define a simple method that takes an image and returns its negative. It appears I have some trouble with the method that returns the negative of the pixel color (only black becomes white, but every other color is returned as yellow).
Here's the code:

private GImage makeColorNegative(GImage image) {
int[][]array = image.getPixelArray();
for(int row = 0; row < array.length; row++) {
for(int col = 0; col < array[0].length; col++) {
int pixelColor = array[row][col];
array[row][col] = getNegativeColor(pixelColor);
return new GImage(array);

private int getNegativeColor(int color) {
int alpha = ((color >> 24) & 0xFF);
int negRed = ~((color >> 24) & 0xFF);
int negGreen = ~((color >> 16) & 0xFF);
int negBlue = ~((color >> 8) & 0xFF);
return (alpha << 24) | (negRed << 16) | (negGreen << 8) | negBlue;

PS Please bear with me - as you can see, I am an absolute beginner :)


  • You've probably got the shifts wrong.
    I'd guess that colours are argb. So alpha is

    (x >> 24) &0xFF;
    (x >> 16) & 0xFF;
    (x >> 8) & 0xFF;
    x & 0xFF;

    It's not clear what colour negative means. If you want black to become white and drak grey to go to light grey, take 255 - channel. However that might not give you the results you want on the coloured pixels.

    Colour space is a known problem. Often you need to convert to HSV space, do your manipulation, and convert back, to get what want. That's a bit beyond the scope of this reply.

    Read my books
    [link=] MiniBasic how to write a script interpreter [/link]
    [link=] Basic Algorithms (in C) [/link]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion