Skip to main content
gamedev.net
🔒 Locked

RGB Decoding

Started by g_fire_1983 Apr 14, 2002 at 9:38 AM 3 replies 150+ views
Original Post
g_fire_1983
g_fire_1983
I'm using Delphi 5 (just moved over from VB6 after a month of learning to program badly). Does anyone know how to easily break down the color values returned in a COLORREF type into the RGB components? Mathematically I understand the process: RGBval [IntegerDiv] 256^(RGBComp - 1) and 255 where RGBComp is the component you're looking for (i.e. 1 is Red, 2 is Green, 3 is blue etc...) I also know that in Delphi ^ is used as a pointer function so I'm using the Power function in the math unit. However the compiler keeps whining about my first [If] line and on the second line it won't do the bitwise operator...[] Here's the function: function DecodePixel(RGBval : COLORREF; RGBComp : Integer) : Integer; begin If RGBComp > 0 And RGBComp < 4 And RGBval >= 0 And RGBval < 16777216 Then DecodePixel := RGBval div Power(256, (RGBComp - 1)) and 255 Else DecodePixel := -1; end; end. [edited by - g_fire_1983 on April 14, 2002 11:16:06 AM]
Jason Zelos
Jason Zelos
If your talking about returning a single component from a 32 bit RGB value (int/long etc), you need to bitwise AND your Colour with 255 (to power r=1,g=2 etc). It looks like you have got your operations mixed up.

I would guess (I don''t use delphi):

DecodePixel := RGBval and (255 to power RGBComp)

A quick search on google confirms ''and'' as a bitwise AND unless enclosed in parenthis in which case it acts as a logical AND. This would probably be your ''If'' problem.

,Jay
g_fire_1983
g_fire_1983
it appears there was an objecion on the part of the compiler to me doing it all in one line of code (which''ll teach me to be a smart ass.

It seems to like it in the following format:

a := RGBVal [Integer Div] 256 [Power] (RGBNum -1) ;
a := a and 255

so if I split the lines it works well enough.

cheers m8

~~tim
Michalson
Michalson
Red:=RGB and 255;
Green:=(RGB shr 8) and 255;
Blue:=(RGB shr 16) and 255;

Generalized function:
Value:=(RGB shr ((RGBComp-1)*8)) and 255;

Of course that mul (*) is slow, so you could use either a precomputed array[1..3] or a case(not if) statement.
a person
a person
but the mul should be replaced by a (shl 3) if the compiler optimier is worth anything. or you could change it yourself (since all the calculations should be integer math anyway. using a case/if/array is slower then a single multiple.

also the RGBVal does not need to be checked since it will always be valid even if the 4th byte contains some garbage value. this helps quiken the conditional statment. though i think you should remove the conditional statement completly since this is a pixel function that will be called numerous times (in fact probably so much that you may want to not use a function for this) that you will greatly reduce your speed.
in cases like this you should take care that your calls dont use the wrong value, instead of placing gaurds that will probably never be used at all (first checking to see if the values are within bounds, then checking the function call return to make sure that the value was good is overkill at the perpixel level and will greatly reduce the speed of the code).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.