This site is no longer active and is available for archival purposes only. Registration and login is disabled.

unsigned short color from RGB


unsigned short color from RGB

Postby BadBazza » Jan 4, 2002 @ 5:43am

Lost....... Assumed Coding!
User avatar
BadBazza
pm Insider
 
Posts: 81
Joined: Sep 21, 2001 @ 6:26am
Location: Blackpool, UK


Re: unsigned short color from RGB

Postby MirekCz » Jan 4, 2002 @ 6:45am

With best regards,
Mirek Czerwinski
User avatar
MirekCz
pm Member
 
Posts: 269
Joined: Sep 18, 2001 @ 6:42pm
Location: Poland,city Poznań


Re: unsigned short color from RGB

Postby BadBazza » Jan 4, 2002 @ 7:20am

Thanks Mirek,<br><br>For anyone else reading this post the topic is "Easyce stuff" in "Phantoms forum" and is well worth reading.<br><br>How would I impletement a macro to do this? I would like to call vline() as follows:-<br>vline(x,y,len,RGB(255,0,0));<br><br>is the following the correct syntax?<br><br>#define RGB(r,g,b) ((unsigned short)((r<<11)+(g<<5)+(b))<br><br>or the faster version<br>#define RGB(r,g,b) ((unsigned short)((r>>8 )&0xF800)+((g>>5)&0x07E0)+((b>>3)&0x001F)) <br><br>and is this the best way to code a solution. <br><br>Thanks in advance <br>Bad<br><br>PS I've never used macros before so please feel free to correct my syntax or tell me not to use them ;)<br><br>PPS I'm not sure about the 2nd #define at all.<br>Last modification: BadBazza - 01/04/02 at 04:20:01
Lost....... Assumed Coding!
User avatar
BadBazza
pm Insider
 
Posts: 81
Joined: Sep 21, 2001 @ 6:26am
Location: Blackpool, UK


Re: unsigned short color from RGB

Postby MirekCz » Jan 4, 2002 @ 9:24am

nah you didn't really get it<br>it should be like<br>#define RGB(r,g,b) ((unsigned short)(((r<<8)&0xf800)+((g<<3)&0x07e0)+(g>>3));<br><br>ps. I rarely define macros so not sure if it's correct, but i think so.<br>hope it works correct (didn't test it)
With best regards,
Mirek Czerwinski
User avatar
MirekCz
pm Member
 
Posts: 269
Joined: Sep 18, 2001 @ 6:42pm
Location: Poland,city Poznań


Re: unsigned short color from RGB

Postby MirekCz » Jan 4, 2002 @ 9:25am

the darn face should be "8 )" , sorry...
With best regards,
Mirek Czerwinski
User avatar
MirekCz
pm Member
 
Posts: 269
Joined: Sep 18, 2001 @ 6:42pm
Location: Poland,city Poznań


Re: unsigned short color from RGB

Postby BadBazza » Jan 4, 2002 @ 9:31am

Thanks again Mirek,<br><br>I think I was going the wrong way. Just to check<br><br>#define RGB(r,g,b) ((unsigned short)(((r<<8 )&0xf800)+((g<<3)&0x07e0)+(g>>3)); <br><br>is ((g<<3)&0x07e0) and (g>>3) part of the macro correct?<br><br>Thanks in advance<br>Bad
Lost....... Assumed Coding!
User avatar
BadBazza
pm Insider
 
Posts: 81
Joined: Sep 21, 2001 @ 6:26am
Location: Blackpool, UK


Re: unsigned short color from RGB

Postby Dan East » Jan 4, 2002 @ 9:58am

Some general define tips:<br><br>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.<br><br>For example:<br>[fixed]<br>#define sqr( a ) ( a * a )<br>int i=sqr( 5 + j );<br>[/fixed]<br>The preprocessor would convert that to:<br>[fixed]int i=( 5 + j * 5 + j );[/fixed]<br><br>So operator precedence would not be correct. <br><br>[fixed]<br>#define sqr( a ) ( ( a ) * ( a ) )<br>int i=sqr( 5 + j );<br>[/fixed]<br>The preprocessor would convert that to:<br>[fixed]int i=( ( 5 + j ) * ( 5 + j ) );[/fixed]<br><br>Which is the desired result.<br><br>Dan East
User avatar
Dan East
Site Admin
 
Posts: 5264
Joined: Jan 25, 2001 @ 5:19pm
Location: Virginia, USA


Re: unsigned short color from RGB

Postby BadBazza » Jan 4, 2002 @ 10:04am

Dan,<br><br>Thanks for those tips, do you agree with Mirek's macro?<br><br>#define RGB(r,g,b) ((unsigned short)((((r)<<8 )&0xf800)+(((g)<<3)&0x07e0)+((g)>>3)) <br><br>Thanks in advance<br>Bad<br><br>PS You have done a good job with DeXplor, I will be registering it when I upgrade to PPC2002
Lost....... Assumed Coding!
User avatar
BadBazza
pm Insider
 
Posts: 81
Joined: Sep 21, 2001 @ 6:26am
Location: Blackpool, UK


Re: unsigned short color from RGB

Postby Dan East » Jan 4, 2002 @ 10:47am

Looks fine to me!<br><br>Dan East
User avatar
Dan East
Site Admin
 
Posts: 5264
Joined: Jan 25, 2001 @ 5:19pm
Location: Virginia, USA


Re: unsigned short color from RGB

Postby Digby » Jan 4, 2002 @ 1:54pm

The macro is wrong.  The last g should be a b.<br><br>
Digby
pm Insider
 
Posts: 1011
Joined: Apr 29, 2001 @ 1:53pm


Re: unsigned short color from RGB

Postby BadBazza » Jan 4, 2002 @ 4:13pm

Mirek, Digby and Dan - Thank you<br><br>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 <br><br>#define rgb(r,g,b) ((unsigned short)((((r)<<8 ) &0xf800) +(((g)<<3)&0x07e0)+((b)>>3)))<br><br>Note 1: remove the space between the 8 and the )<br>Note 2: I had to use rgb instead of RGB as it looks like RGB is defined in wingdi.h<br><br>Thanks again for your help<br>Bad<br>
Lost....... Assumed Coding!
User avatar
BadBazza
pm Insider
 
Posts: 81
Joined: Sep 21, 2001 @ 6:26am
Location: Blackpool, UK


Re: unsigned short color from RGB

Postby MirekCz » Jan 5, 2002 @ 6:39am

hehe,damn, yeah, stupid mistake with g/b, sorry about that:) I was kinda sleepy at the time of writing it;P <br>Thnx Digby for correcting me.
With best regards,
Mirek Czerwinski
User avatar
MirekCz
pm Member
 
Posts: 269
Joined: Sep 18, 2001 @ 6:42pm
Location: Poland,city Poznań


Return to Phantom's Forum


Sort


Forum Description

Discuss any of Phantom's projects here (Operation Nutcracker, etc.)

Moderators:

sponge, RICoder, Phantom

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum