Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (591)
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  
  RGB Values  (Read 835 times)
0 Members and 1 Guest are viewing this topic.
Offline ghostsoldier23

Junior Devvie


Medals: 1



« Posted 2011-12-25 04:02:07 »

I understand RGB when R, G and B are separate 0-264 values, but how does the scale/coloring work when it's expressed as one number?

Like when you use setRGB on a BufferedImage.  It only takes one RGB value... how does that work?
Offline ReBirth
« Reply #1 - Posted 2011-12-25 04:10:37 »

RGB ranges 0-255. The setRGB method takes int[] as parameter, each one representing color of one pixel. To make it easy, write it in hexa like 0xff567c. That's single value concating value of R, G and B together.

Online ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-12-25 04:13:34 »

The values go from 0 to 255 (not 264) because it uses 1 byte for each color channel. Since setRGB takes an integer and an integer has 4 bytes, each byte represents each color channel (the 4th byte is for alpha and it is the leftmost byte, however it may or may not be used depending on the type of the BufferedImage). An example:
1  
2  
3  
4  
5  
6  
7  
ARGB = 255 255 255 255
Hex = 0xffffffff
Binary = 11111111 11111111 11111111 11111111

ARGB = 50 50 50 50
Hex = 0x32323232
Binary = 00110010 00110010 00110010 00110010


EDIT: @Rebirth, the OP is talking about the 3-argument setRGB(int x, int y, int rgb)

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

Junior Devvie


Medals: 1



« Reply #3 - Posted 2011-12-25 05:23:31 »

Oops I meant 255 (256 colors/value DUH).  Dumb mistake..

Thanks for the explanation!
Offline Z-Man
« Reply #4 - Posted 2011-12-25 05:46:07 »

I know the OP didn't request it, but I thought I'd share since I've been tinkering with this as well. This is what I use to (I think) convert between an ARGB int and separate ARGB values. I've tested the color returned by toARGB() and it works, but I haven't tested the color returned by toAWT() except by printing it and seeing that I get the same ARGB values.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public static int toARGB(int r, int g, int b, int a)
{
   int argb = a;
   argb = (argb << 8) + r;
   argb = (argb << 8) + g;
   argb = (argb << 8) + b;
   return argb;
}

public static Color toAWT(int rgbInt)
{
   int r, g, b, a;
   
   b = rgbInt & 255;
   g = (rgbInt >> 8) & 255;
   r = (rgbInt >> 16) & 255;
   a = (rgbInt >> 24) & 255;
   
   return new Color(r, g, b, a);
}
Online ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-12-25 06:18:59 »

Ewww lets change that toARGB into a one liner shall we please?
1  
2  
3  
public static int toARGB(int a, int r, int g, int b) {
    return (a << 24) + (r << 16) + (g << 8) + b;
}


And that toAWT method is buggy because using >> will keep the negative sign for all of them if alpha value is greater than 127:
1  
2  
3  
4  
5  
6  
7  
8  
public static Color toAWT(int rgb) {
    int a = (rgb >> 24) & 0xff;
    int r = (rgb >> 16) & 0xff;
    int g = (rgb >> 8) & 0xff;
    int b = rgb & 0xff;
   
    return new Color(r,g,b,a);
}

Offline Z-Man
« Reply #6 - Posted 2011-12-25 06:27:26 »

Oops, thanks for the fix and the optimization Grin
Offline counterp

Senior Devvie


Medals: 11



« Reply #7 - Posted 2011-12-25 08:17:57 »

Why don't you just do

1  
new Color(rgba, hasAlpha);


The color's value is stored as an integer in the Color class anyways... it doesn't make sense to strip out the color channels just so they can be packed together into an integer again
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.

trollwarrior1 (34 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!