Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved] Colors and single RGB integers... getting some strange results.  (Read 2440 times)
0 Members and 1 Guest are viewing this topic.
Offline Screem
« Posted 2012-03-15 14:29:19 »

I'm trying to replace certain colors in an image with different colors inside an int[] array, but something isn't going right. I have a sprite sheet with letters, numbers, and symbols in shades of gray and I'm trying to change the colors to get different colored fonts. Everything is going well, just something isn't working when the red, green, and blue values are compressed into one integer value. When I specify a certain pixel to be red for example, I get some strange color with an RGB value of (R: 181, G: 100, B: 58) instead of (R: 255, G: 0, B: 0). The same thing happens for every other color I specify. Here is the code I use to change three separate integers, red, green, and blue, into one integer:

1  
2  
3  
4  
5  
6  
public static int getColor(int r, int g, int b) {
      if(r > 255 || r < 0) r = 255;
      if(g > 255 || g < 0) g = 255;
      if(b > 255 || b < 0) b = 255;
      return ((255 & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
   }


Here is a picture showing the results I got when I tried to change colors into red, green, and blue:
The RGB values are shown next to the color, in the order Red, Green, Blue.


Everything else works fine, the correct shades of gray are being replaced, no errors, etc, I just can't figure out where I'm going wrong with combining the red, green, and blue values into one int.

I even tried to use this line of code to get a red color, which uses the official Color class in the Java JDK, but I got the same results:
1  
int red = new Color(255, 0, 0).getRGB();


Just in case anyone needs to see, here is a picture with what it looks like:


Does anyone know what the problem is? Any help would be gladly appreciated! Smiley

Offline Damocles
« Reply #1 - Posted 2012-03-15 14:41:05 »

looks like your alpha transparecy is like at 50%,

switch areound the RGB and Alpha values.

Offline Screem
« Reply #2 - Posted 2012-03-15 15:06:11 »

I tried changing
1  
return ((255 & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);


To all of the following:
1  
2  
3  
return ((255 & 0xff) << 24) | (b & 0xff) | ((g & 0xff) << 8) | ((r & 0xff) << 24); // ABGR instead of ARGB
return ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff) | ((255 & 0xff) << 24); // RGBA instead of ARGB
return (b & 0xff) | ((g & 0xff) << 8) | ((r & 0xff) << 16) | ((255 & 0xff) << 24); // BGRA instead of ARGB


but got the same results Undecided
Is this what you meant by switch around the RGB and Alpha values?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DrZoidberg

Senior Member


Medals: 10



« Reply #3 - Posted 2012-03-15 15:21:08 »

Your getColor method works correctly. Although I would shorten it to
1  
2  
3  
public static int getColor(int r, int g, int b) {
    return (0xff << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
}


The problem must be somewhere else in your code.
Are you extracting the int array from a BufferedImage and write into it?
Is your BufferedImage of type INT_RGB?
Offline Screem
« Reply #4 - Posted 2012-03-15 23:04:20 »

Quote
Are you extracting the int array from a BufferedImage and write into it?
Is your BufferedImage of type INT_RGB?

I'm creating a blank array of size 64 (for an 8x8 tile image) and writing into that.

As for the type, that's a good point. I just checked it, and turns out the sprite sheet is of type BYTE_INDEXED. I couldn't find a setType() method (only found a getType() method) so I created a new BufferedImage the size of the sprite sheet and set the RGB of the new BufferedImage to that of the old one, and it works!

Is this the only way to convert the type of a BufferedImage or is there a much faster way? It seems like there would be a better method.

Anyways, thanks a lot for the help! Cheesy

Offline DrZoidberg

Senior Member


Medals: 10



« Reply #5 - Posted 2012-03-16 00:37:12 »

The only other way I know of is to use the Graphics.drawImage method.

1  
2  
3  
Graphics g = newImage.getGraphics();
g.drawImage(oldImage, 0, 0, null);
g.dispose();

Offline Screem
« Reply #6 - Posted 2012-03-16 01:43:54 »

Hm... doesn't look like it'll fit in with the way I have this thing coded. Thanks anyways!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (18 views)
2014-04-15 18:08:23

BurntPizza (15 views)
2014-04-15 03:46:01

UprightPath (28 views)
2014-04-14 17:39:50

UprightPath (13 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (54 views)
2014-04-10 04:04:31

BurntPizza (111 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!