gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > macro within gba_types.h

#25304 - black - Fri Aug 20, 2004 8:17 am

howdy~

i can not grasp how RGB(r,g,b) works inside gba_types.h, anyone could explain it in details ? and if possible i'd like to see any example to see how to use it, thanx~
Code:

// gba_types.h
// Variable types

#ifndef GBA_TYPES_H

#define GBA_TYPES_H

...

#define RGB(r,g,b)   ((u16)(r | (g<<5) | (b<<10)))

#endif

_________________
Never end on learning~

#25308 - cocole - Fri Aug 20, 2004 9:31 am

Well, you should be able to see the answer anywhere in any GBA tutorial or even a C tut.

It adds (by the | operator)
the value r
the value g 5 time bitshifted on the left
and b 10 time bitshifted

let me explain in plain maths :
if r = 1
and g = 1
and b = 1
so

g << 5 is 10000
b << 10 is 1000000000

so r | (g<<5) | (b<<10)) = 1 | 10000 | 1000000000 = 1000100001

#25309 - black - Fri Aug 20, 2004 10:08 am

woh thanx, i'm not well in bits shifting but i know how it works now, i'll scratch some docs for bits and back here later. :) thanx again dude~
_________________
Never end on learning~

#25510 - black - Tue Aug 24, 2004 7:44 am

i read an article about bits shifting and now i know what the code really did, but what havnt be solved is how to calc rgb color like the code below ?
Code:

define RGB(r,g,b)   ((u16)(r | (g<<5) | (b<<10)))

what color should RGB(1, 1, 1) will be ? and how to make arbitrary color(such as red) with it ?

i'd appreciate any relative tuto to be suggested, thanx in advance~
_________________
Never end on learning~

#25511 - tepples - Tue Aug 24, 2004 7:54 am

RED is RGB(31, 8, 8)
ORANGE is RGB(31,23, 8)
YELLOW is RGB(31,31, 8)
GREEN is RGB( 8,31, 8)
TURQUOISE is RGB( 8, 31, 31)
BLUE is RGB( 8, 8,31)
MAGENTA aka MAGIC PINK is RGB(31, 8,31)
BROWN is RGB(23,17, 8)

Components range from 0 to 31, but ddon't use component values below 8 because they don't show up bright enough on the LCD. Castlevania broke this rule, using a lot of the darker colors, and reviewers found the game too dark.

Play around with a paint program's color picker to learn more colors.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#25629 - black - Thu Aug 26, 2004 8:43 am

tepples wrote:

Play around with a paint program's color picker to learn more colors.


usually i work with something like Flash which color palette which will show some hex number like 0xffff00 instead of (8, 12, 31), i want to know how to convert those hex numbers into GBA format ?
_________________
Never end on learning~

#25637 - gb_feedback - Thu Aug 26, 2004 1:12 pm

0xffff00 is three hexadecimal RGB values ff, ff, 00

Converting that to decimal you would get 255, 255, 0

(Or to binary 11111111, 11111111, 00000000)

Most system using RGB use values from 0 to 255 for each component of colour, instead of the 0 to 31 on the GBA.

So to get the gba equivalent you drop the last 3 binary digits...

(11111, 11111, 00000)

Which is 31, 31, 0 on the GBA
_________________
http://www.bookreader.co.uk/

#25646 - tepples - Thu Aug 26, 2004 3:56 pm

Sure, just lopping off the lower bits works, but not everybody can do binary in his head, and it still doesn't correct for the dark screen. The best balance I've found between correctness and simplicity is the following: To go from PC CRT component values 0-255 to GBA LCD component values 8-31, divide by 11 and add 8.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#25647 - gb_feedback - Thu Aug 26, 2004 4:17 pm

I'm sure you're right, tepples, but I was just explaining what 0xffff00 meant for the man, so sticking to the basics.

By the way, I would have arranged for black to be a special case, if I were going to write this macro. It seems a pity to throw away any possible contrast when for example displaying black text. I suppose you'll say you can't see the difference, but why risk it?
_________________
http://www.bookreader.co.uk/

#25681 - tepples - Thu Aug 26, 2004 10:59 pm

Because GIMP 2 uses the darkest color as color 0 when converting an image from RGB to indexed color, some tutorials may recommend using Real Black to represent transparency.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.