Results 1 to 15 of 15

Thread: [RESOLVED] Color

  1. #1

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    678

    Resolved [RESOLVED] Color

    I get a "COLOR" form GetPixel api. This a long data type. How do I convert this to RPG?

    Thanks

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    8,090

    Re: Color

    Quote Originally Posted by AccessShell View Post
    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. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,687

    Re: Color

    About 20 years ago I worked on a mainframe reporting application called RPG. Report Program Generator.
    Please remember next time...elections matter!

  4. #4

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    678

    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!

  5. #5

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    678

    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.

  6. #6

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    678

    Re: Color

    I got it. I got the conversion and all works fine now.

    Thanks

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    8,090

    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. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  8. #8

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    678

    Re: [RESOLVED] Color

    Actually, I made the change to the right API call. I wrote the wrong API call in the post.

  9. #9
    Banned
    Join Date
    Nov 2011
    Posts
    7,803

    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.

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    8,090

    Re: [RESOLVED] Color

    Quote Originally Posted by Niya View Post
    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. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    23,660

    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

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    8,090

    Re: [RESOLVED] Color

    Quote Originally Posted by dilettante View Post
    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. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  13. #13
    PowerPoster
    Join Date
    Feb 2006
    Posts
    23,660

    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.

  14. #14
    Banned
    Join Date
    Nov 2011
    Posts
    7,803

    Re: [RESOLVED] Color

    Quote Originally Posted by dilettante View Post
    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

  15. #15
    Frenzied Member
    Join Date
    Dec 2014
    Posts
    1,946

    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
  •  



Click Here to Expand Forum to Full Width