PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

unsigned short color from RGB

Hi,

This should be a simple one for most of you out there, but I am having a mental block this morning :(

How do I calculate a pixel color using an unsigned short.

eg if I want to pass red to EasyCE function hline() how would i specify it? could a macro be created so i could specify RGB(255,0,0)?

Thanks in advance
Bad
badbazza I was giveing an explanation about this not so long ago, it's either in phantoms forum or in general dev. forum. look into last 25 posts or so in both forums and you should find it.
Thanks Mirek,

For anyone else reading this post the topic is "Easyce stuff" in "Phantoms forum" and is well worth reading.

How would I impletement a macro to do this? I would like to call vline() as follows:-
vline(x,y,len,RGB(255,0,0));

is the following the correct syntax?

#define RGB(r,g,b) ((unsigned short)((r<<11)+(g<<5)+(b))

or the faster version
#define RGB(r,g,b) ((unsigned short)((r>>8 )&0xF800)+((g>>5)&0x07E0)+((b>>3)&0x001F))

and is this the best way to code a solution.

Thanks in advance
Bad

PS I've never used macros before so please feel free to correct my syntax or tell me not to use them ;)

PPS I'm not sure about the 2nd #define at all.
Last modification: BadBazza - 01/04/02 at 04:20:01
nah you didn't really get it
it should be like
#define RGB(r,g,b) ((unsigned short)(((r<<8)&0xf800)+((g<<3)&0x07e0)+(g>>3));

ps. I rarely define macros so not sure if it's correct, but i think so.
hope it works correct (didn't test it)
the darn face should be "8 )" , sorry...
Thanks again Mirek,

I think I was going the wrong way. Just to check

#define RGB(r,g,b) ((unsigned short)(((r<<8 )&0xf800)+((g<<3)&0x07e0)+(g>>3));

is ((g<<3)&0x07e0) and (g>>3) part of the macro correct?

Thanks in advance
Bad
Some general define tips:

Don't put a semicolon at the end - that will cause compilation errors depending on how the macro is used. The way you want to use it as a function parameter would cause errors. Also, you should technically enclose each var in parenthesis like ((a)*(b)), because in a macro what you pass for each var is simply substituted for the var name. You should also enclose the entire expression in parenthesis, which you are already doing.

For example:
[fixed]
#define sqr( a ) ( a * a )
int i=sqr( 5 + j );
[/fixed]
The preprocessor would convert that to:
[fixed]int i=( 5 + j * 5 + j );[/fixed]

So operator precedence would not be correct.

[fixed]
#define sqr( a ) ( ( a ) * ( a ) )
int i=sqr( 5 + j );
[/fixed]
The preprocessor would convert that to:
[fixed]int i=( ( 5 + j ) * ( 5 + j ) );[/fixed]

Which is the desired result.

Dan East
Dan,

Thanks for those tips, do you agree with Mirek's macro?

#define RGB(r,g,b) ((unsigned short)((((r)<<8 )&0xf800)+(((g)<<3)&0x07e0)+((g)>>3))

Thanks in advance
Bad

PS You have done a good job with DeXplor, I will be registering it when I upgrade to PPC2002
Looks fine to me!

Dan East
The macro is wrong.  The last g should be a b.

Mirek, Digby and Dan - Thank you

All is working beautifully now, the (g) should of been a (b) and there was a closing bracket missing off the end. I have not thouroghly tested it but I have tried rgb(255,0,0), rgb(0,255,0) and rgb(0,0,255) and they produce what I would expect. For those interested the final macro is

#define rgb(r,g,b) ((unsigned short)((((r)<<8 ) &0xf800) +(((g)<<3)&0x07e0)+((b)>>3)))

Note 1: remove the space between the 8 and the )
Note 2: I had to use rgb instead of RGB as it looks like RGB is defined in wingdi.h

Thanks again for your help
Bad
hehe,damn, yeah, stupid mistake with g/b, sorry about that:) I was kinda sleepy at the time of writing it;P
Thnx Digby for correcting me.