-
Aug 3rd, 2022, 12:05 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Color
I get a "COLOR" form GetPixel api. This a long data type. How do I convert this to RPG?
Thanks
-
Aug 3rd, 2022, 12:35 PM
#2
Re: Color
Originally Posted by AccessShell
I get a "COLOR" form GetPixel api. This a long data type. How do I convert this to RPG?
Thanks
RPG? Rocket Propelled Grenade? Role Play Game? Not sure "COLOR" can be converted to those.
It can be converted to RGB (Red, Green, Blue) though. The low-order byte (of the long) is Red, the next-to-low byte is Green, the third is Blue, and the high byte depends on the context (possibly Alpha or possibly an OLE Color flag).
You just need to know how to take apart a Long into four (or maybe just three) bytes.
EDIT: I just looked and GetPixel doesn't use the high-order byte at all, other than indicating an error if one occurred.
EDIT2: You could have it return the following UDT, and the work would be done (as opposed to returning a Long). Just change the API declaration. And check nada for &hFF to make sure there wasn't an error.
Code:
Public Type MyRGB
red As Byte
green As Byte
blue As Byte
nada As Byte
End Type
Last edited by Elroy; Aug 3rd, 2022 at 12:41 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Aug 3rd, 2022, 01:20 PM
#3
Re: Color
About 20 years ago I worked on a mainframe reporting application called RPG. Report Program Generator.
Please remember next time...elections matter!
-
Aug 3rd, 2022, 01:22 PM
#4
Thread Starter
Fanatic Member
Re: Color
Thank you Elroy, I am not into RPGs. Although about 50 yrs ago I wrote in RPG I.
How do I change the GetPixel function as you suggest? I tried to
Code:
Public Declare Function GetPixelFormat Lib "gdi32.dll" (ByVal hDC As Long) As MyRGB
The function does not like it!
-
Aug 3rd, 2022, 01:30 PM
#5
Thread Starter
Fanatic Member
Re: Color
I modified the program by placing RGB(0,0,255) into a picture box. I ran the program and got 16711680 as the long value. I cannot see how this becomes the RGB value.
-
Aug 3rd, 2022, 02:51 PM
#6
Thread Starter
Fanatic Member
Re: Color
I got it. I got the conversion and all works fine now.
Thanks
-
Aug 3rd, 2022, 06:03 PM
#7
Re: [RESOLVED] Color
Yeah, just getting back. It looks like you made the change to the wrong API call. I'm assuming you figured that out.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Aug 3rd, 2022, 07:09 PM
#8
Thread Starter
Fanatic Member
Re: [RESOLVED] Color
Actually, I made the change to the right API call. I wrote the wrong API call in the post.
-
Aug 3rd, 2022, 11:00 PM
#9
Re: [RESOLVED] Color
Code:
Private Sub GetRGB(ByVal c As Long, ByRef R As Byte, ByRef G As Byte, ByRef B As Byte)
R = c And &HFF&
G = (c And &HFF00&) / 256
B = (c And &HFF0000) / 65536
End Sub
You can extract individual colour channels from a Long using the above code in VB6.
-
Aug 4th, 2022, 09:59 AM
#10
Re: [RESOLVED] Color
Originally Posted by Niya
Code:
Private Sub GetRGB(ByVal c As Long, ByRef R As Byte, ByRef G As Byte, ByRef B As Byte)
R = c And &HFF&
G = (c And &HFF00&) / 256
B = (c And &HFF0000) / 65536
End Sub
You can extract individual colour channels from a Long using the above code in VB6.
Boo, I was trying to let him figure that out on his own.
Also, we should probably use integer division, so it's not doing a lot of type casting in the expression. Also, that 256 should probably have an "&" on it so that's not typecast as well.
Last edited by Elroy; Aug 4th, 2022 at 10:02 AM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Aug 4th, 2022, 10:23 AM
#11
Re: [RESOLVED] Color
Watch out though, VB colors are not that simple.
Code:
Option Explicit
Private Const WIN32_NULL As Long = 0
Private Enum HRESULT
S_OK = 0
E_INVALIDARG = &H80000003
E_UNEXPECTED = &H8000FFFF
End Enum
Private Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Declare Function OleTranslateColor Lib "oleaut32" ( _
ByVal clr As OLE_COLOR, _
ByVal hPal As Long, _
ByRef RGBQUAD As RGBQUAD) As HRESULT
Private Sub Form_Load()
Dim RGBQUAD As RGBQUAD
With RGBQUAD
OleTranslateColor BackColor, WIN32_NULL, RGBQUAD
Debug.Print .rgbRed; .rgbGreen; .rgbBlue
OleTranslateColor ForeColor, WIN32_NULL, RGBQUAD
Debug.Print .rgbRed; .rgbGreen; .rgbBlue
OleTranslateColor vbCyan, WIN32_NULL, RGBQUAD
Debug.Print .rgbRed; .rgbGreen; .rgbBlue
End With
End Sub
-
Aug 4th, 2022, 10:30 AM
#12
Re: [RESOLVED] Color
Originally Posted by dilettante
Watch out though, VB colors are not that simple.
Yeah, but Dil, his question was specifically about the return of GetPixel. They are pretty simple in that context.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Aug 4th, 2022, 10:45 AM
#13
Re: [RESOLVED] Color
True enough, but we have seen this story play out several times here.
Somebody is given the simplistic "solution" and then comes back crying when it fails used elsewhere. Asking such a fundamental, trivial, and obvious question in the first place is wearing a big sign saying "I do not know what I am doing!"
That's fine, but then they need to be given a more complete answer. We could even go into what OLE_COLOR means and where a palette comes into the picture.
-
Aug 4th, 2022, 12:19 PM
#14
Re: [RESOLVED] Color
Originally Posted by dilettante
True enough, but we have seen this story play out several times here.
Somebody is given the simplistic "solution" and then comes back crying when it fails used elsewhere. Asking such a fundamental, trivial, and obvious question in the first place is wearing a big sign saying "I do not know what I am doing!"
That's fine, but then they need to be given a more complete answer. We could even go into what OLE_COLOR means and where a palette comes into the picture.
Interestingly, it probably would have played out with me. I always assumed OLE_COLOR was just a simple alias/typedef that tells the programmer to "treat this 32 bit Integer as an RGB structure". I never imagined that there was more to it than that so I never looked it up. I just looked it up and I'm amazed that I wrote a lot and I mean a lot of code back in the day dealing with colours in VB6 and never ran into a problem that forced me to look up what an OLE_COLOR really is. I really got lucky.
Anyways, if I wanted to protect myself from non-RGB OLE_COLORs without the OleTranslateColor API, could something like this work? :-
Code:
Private Sub Form_Load()
Dim r As Byte, g As Byte, b As Byte
GetRGB RGB(23, 56, 255), r, g, b
Debug.Print r, g, b
'Throws an error because this is OLE_COLOR is actually
'an entry into a palette and not RGB
GetRGB Me.BackColor, r, g, b
End Sub
Private Sub GetRGB(ByVal c As Long, ByRef r As Byte, ByRef g As Byte, ByRef b As Byte)
If (c And &HFF000000) = 0& Then
r = c And &HFF&
g = (c And &HFF00&) / 256&
b = (c And &HFF0000) / 65536
'This is untested. I don't know where to find an OLE_COLOR
'where OleColorType = RgbColor.
ElseIf c And &HFF000000 = &H2000000 Then
b = c And &HFF&
g = (c And &HFF00&) / 256&
r = (c And &HFF0000) / 65536
Else
Err.Raise vbObjectError + 50000, , "Invalid operation. Cannot translate paletted color"
End If
End Sub
-
Aug 4th, 2022, 01:07 PM
#15
Re: [RESOLVED] Color
Code:
Private Type MyRGB
red As Byte
green As Byte
blue As Byte
End Type
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cbCopy As Long)
Private Sub Form_Load()
Dim Col As MyRGB
CopyMemory Col, vbBlue, 3
MsgBox vbBlue & vbNewLine & Col.red & ":" & Col.green & ":" & Col.blue
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|