• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

negative color

 
Sophie Bell
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}

Thanks
 
Tim Moores
Bartender
Posts: 2502
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be other problems, but the bit-shifts used for negRed, negGreen and negBlue are incorrect. The shifts should be 16, 8, and 0, respectively, instead of 24, 16, and 8.
 
Sophie Bell
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't believe I made such a silly mistake...
Thanks, Tim
Although, the program still doesn't work
In fact, it seems this particular mistake had nothing to do with the result
Here's the latest version:

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 >> 16) & 0xFF);
int negGreen = ~((color >> 8) & 0xFF);
int negBlue = ~(color & 0xFF);
return (alpha << 24) | (negRed << 16) | (negGreen << 8) | negBlue;
}
 
Tim Moores
Bartender
Posts: 2502
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, right: the ~ operator will switch *all* 32 bits, and not just the least-significant 8 bits that you're looking at for a particular color. So instead of "~ XYZ", try "255 - XYZ".
 
Sophie Bell
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Tim! It worked
 
Rob Spoor
Sheriff
Pie
Posts: 20399
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or move the ~ within the parentheses:
 
Sophie Bell
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks, Rob
I love this forum! You are great
 
Rob Spoor
Sheriff
Pie
Posts: 20399
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We most definitely are!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic