Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

color negative

SophieMSophieM Posts: 1Member
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 :)


Comments

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

    (x >> 24) &0xFF;
    red
    (x >> 16) & 0xFF;
    green
    (x >> 8) & 0xFF;
    blue
    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=http://www.lulu.com/bgy1mm] MiniBasic how to write a script interpreter [/link]
    [link=http://www.lulu.com/bgy1mm] Basic Algorithms (in C) [/link]
Sign In or Register to comment.