Announcement

Collapse
No announcement yet.

Converting RGB value to BGR for PB 7.04

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting RGB value to BGR for PB 7.04

    I know PB 8 has the BGR conversion function but the set of programs I need this in are 7.04 and code I want to use from the forum is for PB 8 and 9. Anyway the sample code uses the BGR function and from what I can glean this might be a replacement function. Looking for verification and/or correction as always

    Thanks,

    Bob Mechler

    Code:
    FUNCTION ToBGR(OldRGB AS LONG) AS LONG
      LOCAL NewBGR&,cRed&,cGreen&,cBlue&
    
      cRed&   = OldRGB& MOD 256
      cGreen& = (OldRGB& \ 256) MOD 256
      cBlue&  = ((OldRGB& \ 256) \ 256) MOD 256
      NewBGR& = cBlue& * 65536 + cGreen& * 256 + cRed&
      
      FUNCTION = NewBGR&
    END FUNCTION
    Last edited by BOB MECHLER; 7 Jan 2009, 03:38 PM. Reason: update

  • #2
    Hey Bob, since I'm always looking for a hard way to do a simple thing, this is a way I used to keep things sort of straight in my twisted mind.

    Regards, Ian
    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "win32api.inc"
    
    TYPE KolorBitTYPE ' equivalent to a long/dword
      rVal AS BYTE
      gVal AS BYTE
      bVal AS BYTE
      dum1 AS BYTE
    END TYPE
    
    UNION KolorAccessU ' make data type transfers simple
      Kolor AS LONG
      KolorBits AS KolorBitTYPE
    END UNION
    '-------------------------------
    '###############################
    '-------------------------------
    FUNCTION PBMAIN () AS LONG
    
     DIM RGBKolor AS LONG, _
         GBRKolor AS LONG, _
         rVal AS LONG, _
         gVal AS LONG, _
         bVal AS LONG
    
    ' Just make some values to play with
      rVal = 15
      gVal = 20
      bVal = 30
    ' make our original colour
      CALL CreateKolor(rVal, gVal, bVal, RGBKolor)
    ' split it into RGB values
      CALL RGBExtract(rVal, gVal, bVal, RGBKolor)
    ' Did we get it right?
      MSGBOX "Constituent Colors: R, G, B:" + $CR + FORMAT$(rVal) + ", " + FORMAT$(gVal) + ", " + FORMAT$(bVal)
    ' Reverse it
      CALL RBG_GBR_Swap(RGBKolor, GBRKolor)
    ' Split it again
      CALL RGBExtract(rVal, gVal, bVal, GBRKolor)
    ' Check that it is reversed.
      MSGBOX "Reversed Colors: R, G, B:" + $CR + FORMAT$(rVal) + ", " + FORMAT$(gVal) + ", " + FORMAT$(bVal)
    ' done
    
    END FUNCTION
    '-------------------------------
    '###############################
    '-------------------------------
    SUB CreateKolor(rVal AS LONG, gVal AS LONG, bVal AS LONG, RGBKolor AS LONG)
      DIM KolorBits AS LOCAL KolorBitTYPE
      DIM KolorAccess AS LOCAL KolorAccessU
    
    ' place the individual colors into the TYPE
      KolorBits.rVal = rVal
      KolorBits.gVal = gVal
      KolorBits.bVal = bVal
    ' put the TYPE into a union
      KolorAccess.Kolorbits = KolorBits
    ' extract our colour value
      RGBKolor = KolorAccess.Kolor
    END SUB
    '-------------------------------
    '###############################
    '-------------------------------
    SUB RGBExtract(rVal AS LONG, gVal AS LONG, bVal AS LONG, RGBKolor AS LONG)
      DIM KolorBits AS LOCAL KolorBitTYPE
      DIM KolorAccess AS LOCAL KolorAccessU
    
    ' place the colour value into our union
      KolorAccess.Kolor = RGBKolor
    ' extract the Type, which contains individual colour values
      KolorBits = KolorAccess.KolorBits
    ' Extract individual colour values from our TYPE
      rVal = KolorBits.rVal
      gVal = KolorBits.gVal
      bVal = KolorBits.bVal
    END SUB
    '-------------------------------
    '###############################
    '-------------------------------
    SUB RBG_GBR_Swap(RGBKolor AS LONG, GBRKolor AS LONG)
      DIM KolorBits AS LOCAL KolorBitTYPE
      DIM KolorAccess AS LOCAL KolorAccessU
    
    ' Put our original colour value into the union
      KolorAccess.Kolor = RGBKolor
    ' Extract the TYPE for access to individual colours
      KolorBits = KolorAccess.KolorBits
    ' SWAP the Red/Blue values (Green remains in place)
      SWAP KolorBits.rVal, KolorBits.bVal
    ' Put the reversed TYPE into the union
      KolorAccess.KolorBits = KolorBits
    ' Extract our Reversed Colour value
      GBRKolor = KolorAccess.Kolor
    END SUB
    '-------------------------------
    '###############################
    '-------------------------------
    :) IRC :)

    Comment


    • #3
      Useful example.

      Thanks,

      Bob Mechler

      Comment


      • #4
        Another way
        Code:
        FUNCTION PBMAIN() AS LONG
            LOCAL value AS LONG
            LOCAL red   AS INTEGER
            LOCAL green AS INTEGER
            LOCAL blue  AS INTEGER
            LOCAL pr    AS BYTE POINTER
            LOCAL pb    AS BYTE POINTER
            LOCAL vl    AS STRING
            
            red   = 58%     '&H3A
            green = 110%    '&H6E
            blue  = 165%    '&HA5
            
            value = RGB(red, green, blue)
            vl = HEX$(value,8) + $CRLF
            pr = VARPTR(value)
            pb = VARPTR(value)+2
            SWAP @pr, @pb   ' now value is BGR
            vl = vl + HEX$(value,8)
            
            MSGBOX vl
        
        END FUNCTION
        "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

        Comment


        • #5
          Code:
          MACRO swapcolour(colour)  'colour is assumed to be a LONG or DWORD
          !mov eax,colour    'get colour into a register
          !bswap eax         'swap the bytes so 1234 -> 4321
          !ror eax,8         'rotate by 1 byte so 4321 -> 1432 as required
          !mov colour,eax    'write the result back
          END MACRO
                 
          FUNCTION PBMAIN () AS LONG
          
            LOCAL x AS LONG
          
            x=&h112233     '11,22,33 are the R,G and B values
          
            MSGBOX HEX$(x) 'display values
            swapcolour(x)  'swap RGB to BGR
            MSGBOX HEX$(x) 'display again to show it's worked
                       
          END FUNCTION

          Comment

          Working...
          X