News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

RGB Macro In assembly question

Started by travism, December 17, 2008, 07:08:27 AM

Previous topic - Next topic

travism

In the Win32Api Manual RGB is defined as:

#define RGB(r, g ,b)  ((DWORD) (((BYTE) (r) | \ 
    ((WORD) (g) << 8)) | \
    (((DWORD) (BYTE) (b)) << 16)))


Im trying to figure out how to implement that in masm? Or is it already somewhere?

Siekmanski

You can use this:

RGB MACRO r,g,b
    EXITM %((b shl 16) or (g shl 8) or (r))
ENDM

And if you use Aplha, the macros from DirectX:

note the reverse order.....

D3DCOLOR_ARGB MACRO a:REQ,r:REQ,g:REQ,b:REQ
   EXITM %(((a and 0ffh) shl 24) or ((r and 0ffh) shl 16) or ((g and 0ffh) shl 8) or (b and 0ffh))
ENDM

RGBA MACRO r,g,b,a
   EXITM D3DCOLOR_ARGB(a,r,g,b)
ENDM

RGB MACRO r,g,b
   EXITM D3DCOLOR_ARGB(0ffh,r,g,b)
ENDM


Neil

The RGB macro is part of Masm32, you'll find it in macros.asm. This is what it looks like :-


    ; -----------------------------------
    ; INPUT red, green & blue BYTE values
    ; OUTPUT DWORD COLORREF value in eax
    ; -----------------------------------
      RGB MACRO red, green, blue
        xor eax, eax
        mov ah, blue    ; blue
        mov al, green   ; green
        rol eax, 8
        mov al, red     ; red
      ENDM


travism


Rainstorm

myabe it depends what you're doing but you can just supply the color value directly in the api call too
i do that most of the time while trying out stuff.
invoke SetDCBrushColor, wParam, 3349691    ; makes brush color red

i have an app called Color Cop that will output the chosen color in a variety of formats, along with the RGB values (the above value is, while using the Powerbuilder mode format)

kromag

Where is this app 'Color Cop'

It would be pretty cool if there were an app similar to http://pinvoke.net
that could output masm32 code for dealing with winAPIs!

When I get more time I might make one to freshen up on my asm.
---
Will.

travism

Quote from: kromag on December 17, 2008, 01:36:08 PM
Where is this app 'Color Cop'

It would be pretty cool if there were an app similar to http://pinvoke.net
that could output masm32 code for dealing with winAPIs!

When I get more time I might make one to freshen up on my asm.
---
Will.

That's actually pretty cool. I didn't even know it was an application.

jj2007

Quote from: Siekmanski on December 17, 2008, 09:23:11 AM
...
And if you use Aplha, the macros from DirectX:

note the reverse order.....

D3DCOLOR_ARGB MACRO a:REQ,r:REQ,g:REQ,b:REQ
   EXITM %(((a and 0ffh) shl 24) or ((r and 0ffh) shl 16) or ((g and 0ffh) shl 8) or (b and 0ffh))
ENDM

RGBA MACRO r,g,b,a
   EXITM D3DCOLOR_ARGB(a,r,g,b)
ENDM


I am bit confused about the order. The enhanced RGB macro below accepts an optional 4th parameter, example:
RGB(40h, 60h, 80h, 99h)=99806040

In contrast, the DirectX macro yields this:
RGBA(40h, 60h, 80h, 99h)=99406080

Where is my error?? Which order is correct? The Wiki seems to opt for the first version

With immediate hex values:     RGB(40h, 60h, 80h)=      00806040
Dwords: eax=40h, ecx=60h, edx=80h: RGB(eax, ecx, edx)=  00806040
Words:  ax=40h,  cx=60h,  dx=80h: RGB(ax,  cx,  dx)=    00806040
Bytes:  al=40h,  cl=60h,  dl=80h: RGB(al,  cl,  dl)=    00806040
With mixed size variables:  RGB(MyLong, MyWord, edx)=   00806040
With alpha channel:     RGB(40h, 60h, 80h, 99h)=        99806040
DirectX alpha channel:  RGBA(40h, 60h, 80h, 99h)=       99406080


include \masm32\include\masm32rt.inc

PushDW MACRO arg ;; pushes args of any size and type as dwords
LOCAL ArgType, ArgSize
 ArgType = (opattr(arg)) AND 127
 if ArgType eq 48 ;; register
ArgSize SIZESTR <arg>
 elseif ArgType eq 36 ;; immediate
ArgSize = 4
 else
ArgSize = SIZEOF arg
 endif
 if ArgSize lt 3
movzx eax, arg ;; word or byte
push eax
 else
push arg
 endif
ENDM

RGB MACRO r, g, b, a
push ecx
PushDW r
PushDW g
PushDW b
pop eax
pop ecx
mov ah, al ;; instead of rol eax, 8
mov al, cl
pop ecx
rol eax, 8
add al, cl
ifnb <a> ;; alpha channel used?
PushDW a ;; yes, so move it to highest order byte
pop ecx
rol eax, 8 ;; last byte
mov al, cl
ror eax, 8
endif
pop ecx ;; don't trash ecx
EXITM <eax>
ENDM

D3DCOLOR_ARGB MACRO a:REQ,r:REQ,g:REQ,b:REQ
EXITM %(((a and 0ffh) shl 24) or ((r and 0ffh) shl 16) or ((g and 0ffh) shl 8) or (b and 0ffh))
ENDM

RGBA MACRO r,g,b,a
EXITM D3DCOLOR_ARGB(a,r,g,b)
ENDM

.code
MyLong dd 40h
MyWord dw 60h

start:
print "With immediate hex values:     RGB(40h, 60h, 80h)=    ", 9
if 0
RGB 40h, 60h, 80h
print hex$(eax), 13, 10
else
print hex$(RGB(40h, 60h, 80h)), 13, 10

print "Dwords: eax=40h, ecx=60h, edx=80h: RGB(eax, ecx, edx)=", 9
mov eax, 40h
mov ecx, 60h
mov edx, 80h
print hex$(RGB(eax,ecx,edx)), 13, 10

print "Words:  ax=40h,  cx=60h,  dx=80h: RGB(ax,  cx,  dx)=", 9
mov ax, 40h
mov cx, 60h
mov dx, 80h
print hex$(RGB(ax,cx,dx)), 13, 10

print "Bytes:  al=40h,  cl=60h,  dl=80h: RGB(al,  cl,  dl)=", 9
mov al, 40h
mov cl, 60h
mov dl, 80h
print hex$(RGB(al,cl,dl)), 13, 10

print "With mixed size variables:  RGB(MyLong, MyWord, edx)=", 9
mov edx, 80h
print hex$(RGB(MyLong, MyWord, edx)), 13, 10

print "With alpha channel:     RGB(40h, 60h, 80h, 99h)=    ", 9
print hex$(RGB(40h, 60h, 80h, 99h)), 13, 10

; DirectX RGBA MACRO r,g,b,a
print "DirectX alpha channel:  RGBA(40h, 60h, 80h, 99h)=    ", 9
print hex$(RGBA(40h, 60h, 80h, 99h)), 13, 10, 10

endif

print "Press any key to get outta here"
getkey
exit

end start

Rainstorm

It doesn't output any code, its just a color picker mainly which will output color values in a variety of formats, from anywhere on the screen or you could choose a color of your own, so you can just copy/paste the value (in the desired format) directly into the win API call. very convenient & its Free - i think it has an automatic copy to clipboard too. - here's the link. . .

Color Cop

Quote..that could output masm32 code for dealing with winAPIs
When I get more time I might make one to freshen up on my asm.
take a look at the genesys project (on this forum) or the site..for the tool - dunno whether its the same thing.. but paul had started writing something on those lines.... a code injector i think.

gotta run.. laters.
-

Rockoon

Quote from: jj2007 on December 17, 2008, 01:51:24 PM
Where is my error?? Which order is correct? The Wiki seems to opt for the first version

There is no error I think. For certain, different API's and standards use different ordering for the A, R, G and B channels.

Some API's are even programmable such that you have a configurable mask for which bits are for red, which for green, and which for blue.

You have to remember that there are some competing hardware standards dating all the way back to the original Targa Truecolor video boards (these date back to the same time as the original 8-bit VGA/MCGA that 99.999% of all computers used.) The targa boards were seriously high end ($$$$$) and when SVGA finally adopted 24-bit color there was no attempt to stick to the targa standard.

You are best to just stick with a more generic BytePack() macro that makes no assumptions about which channel is which, and then adapt its usage to the API in question.

Edited to add: Specifically what I am saying is that there is no reason for a macro named RGB, BGR, XRGB, XBGR, ARGB, or ABGR when really what you are doing is packing bytes into a 32-bit value. One macro easily provides the functionality of all those others.
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

kromag

Quote from: travism on December 17, 2008, 01:47:01 PM
Quote from: kromag on December 17, 2008, 01:36:08 PM
Where is this app 'Color Cop'

It would be pretty cool if there were an app similar to http://pinvoke.net
that could output masm32 code for dealing with winAPIs!

When I get more time I might make one to freshen up on my asm.
---
Will.

That's actually pretty cool. I didn't even know it was an application.

You wouldn't consider that a web-app?
---
William