I have a piece of C code. I need help to translate it to Delphi code.
/*
* Color is packed into 16-bit word as follows:
*
* 15 8 7 0
* XXggbbbb XXrrrrgg
*
* Note that green bits 12 and 13 are the lower bits of green component
* and bits 0 and 1 are the higher ones.
*
*/
#define CLR_RED(spec) (((spec) >> 2) & 0x0F)
#define CLR_GREEN(spec) ((((spec) & 0x03) << 2) | ((spec & 0x3000) >> 12))
#define CLR_BLUE(spec) (((spec) >> 8) & 0x0F)
#define CDG_GET_SCROLL_COMMAND(scroll) (((scroll) & 0x30) >> 4)
#define CDG_GET_SCROLL_HOFFSET(scroll) ((scroll) & 0x07)
#define CDG_GET_SCROLL_VOFFSET(scroll) ((scroll) & 0x0F)
These are parameterized macros. Since Delphi doesn't support these, you'll need to use functions instead, which is cleaner anyways.
>> is a right-shift, shr in Delphi<< is a left-shift, shl in Delphi& is "bitwise and", and in Delphiand to replace both && and &.| is "bitwise or", or in Delphi0x is the prefix for hex literals, $ in DelphiSo #define CLR_GREEN(spec) ((((spec) & 0x03) << 2) | ((spec & 0x3000) >> 12)) becomes something like:
function CLR_GREEN(spec: word):byte;
begin
result := byte(((spec and $03) shl 2) or ((spec and $3000) shr 12));
end;
(I don't have delphi at hand, so there might be minor bugs)
Convert the other macros in a similar manner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With