Announcement

Collapse
No announcement yet.

Mixing two colors

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

  • Mixing two colors

    I am doing something simple but i have never done it. I would like to have a
    function like this:

    Code:
    FUNCTION MixColors(Color1 AS DWORD, Color2 AS DWORD) AS DWORD
    
    FUNCTION = ??????
    
    END FUNCTION
    WHat i want to do is to, for example mix blue with red and have purple.
    But since i have never done this, i dont know how.

    Thanx in advance!

  • #2
    Code:
    #COMPILE EXE
    #DIM ALL
    FUNCTION PBMAIN () AS LONG
    ' compare the values of the function and the correct mix value
    MSGBOX STR$(MixRGB(RGB(64,32,48),RGB(255,128,64)))+" "+STR$(RGB(160,80,56))
        
    END FUNCTION
    FUNCTION MixRGB(BYVAL RGB1&, BYVAL RGB2&) AS LONG
         LOCAL B1 AS BYTE PTR, B2 AS BYTE PTR
         B1=VARPTR(RGB1&)
         B2=VARPTR(RGB2&)
         @B1=(@[email protected])/2
         INCR B1
         INCR B2
         @B1=(@[email protected])/2
         INCR B1
         INCR B2
         @B1=(@[email protected])/2
         FUNCTION=RGB1&
    END FUNCTION
    Chris Boss
    Computer Workshop
    Developer of "EZGUI"
    http://cwsof.com
    http://twitter.com/EZGUIProGuy

    Comment


    • #3
      Elias,
      there are faster ways but this one is may be easier to follow:
      Code:
      'PBCC5.01/PBWin9.01 function
      %RedMask   = &h000000ff
      %GreenMask = &h0000ff00
      %BlueMask  = &h00ff0000  
      
      FUNCTION MixColors(Color1 AS DWORD, Color2 AS DWORD) AS DWORD
      LOCAL red1,red2,red3,green1,green2,green3,blue1,blue2,blue3 AS LONG
      
      'extract the red, green and blue components of each colour
      red1  = color1 AND %RedMask
      green1= color1 AND %GreenMask
      blue1 = color1 AND %BlueMask
      red2  = color2 AND %RedMask
      green2= color2 AND %GreenMask
      blue2 = color2 AND %BlueMask
      
      'add the individual components together but don't allow it to exceed 1 byte.
      'if the sum is greater than the mask then return the mask instead as it represents the fullest value of that colour
      red3   =  MIN (red1   + red2,   %RedMask)      
      green3 =  MIN (green1 + green2, %GreenMask)
      blue3  =  MIN (blue1  + blue2,  %BlueMask)
      
      'combine the 3 colours together
      FUNCTION = red3 + green3 + blue3
      
      END FUNCTION

      Comment


      • #4
        Thank you Chris and Paul. Ill try it.

        Comment


        • #5
          I see Chris is averaging the colours where my code adds them. I suppose either could be taken as "mixing".

          Paul.

          Comment


          • #6
            To mix colors requires adding each byte value (red, green or blue) and then diving by 2 (average).

            To mix means to "blend" the colors.

            For example if one mixes white and black, you should get a gray in between.

            0,0,0

            mixed with 255,255,255

            should be 128,128,128

            Gray.

            Alphablending is similiar, except one defines a percentage for each color.
            Chris Boss
            Computer Workshop
            Developer of "EZGUI"
            http://cwsof.com
            http://twitter.com/EZGUIProGuy

            Comment


            • #7
              Chris,
              I looked at "mixing" the way I'd look at combining light, not the way I'd look at combining paint.
              With paint, there is a maximum brightness for any colour and when you add anythng else you reduce the overall brightness.
              With light, when you add another colour then you add to the overall brightness until it saturates.

              If you add red paint and blue paint you get a darker colour, that's the way your mixing would do it.
              If you add red light and blue light you get a lighter colour, that's the way my mixing would do it.

              I don't know what Elias wants when he mixes coulours but he now has a choice.

              Paul.

              Comment


              • #8
                I modified the routine to alphablend the colors. A percentage paramater is added. If the percentage is say 75 (for 75%) then color 1 imakes up 75% of the mixture while color makes up 25% of the mixture.

                Code:
                #COMPILE EXE
                #DIM ALL
                FUNCTION PBMAIN () AS LONG
                ' compare the values of the function and the correct mix value
                MSGBOX STR$(MixRGB(RGB(64,32,48),RGB(255,128,64),50))+" "+STR$(RGB(160,80,56))
                MSGBOX STR$(MixRGB(RGB(0,0,0),RGB(255,255,255),25))+" "+STR$(RGB(191,191,191))
                
                END FUNCTION
                ' alpha& paramater is from 0 to 100 and is the percentage of color 1 to be used
                ' in blending the colors. If color 1 is 25% then color 2 is 75%
                FUNCTION MixRGB(BYVAL RGB1&, BYVAL RGB2&, BYVAL Alpha&) AS LONG
                     LOCAL B1 AS BYTE PTR, B2 AS BYTE PTR
                     LOCAL Alpha1!, Alpha2!
                     B1=VARPTR(RGB1&)
                     B2=VARPTR(RGB2&)
                     IF Alpha&<0 THEN Alpha&=0
                     IF Alpha&>100 THEN Alpha&=100
                     Alpha1!=Alpha&/100
                     Alpha2!=(100-Alpha&)/100
                     @B1=(@B1*Alpha1!)+(@B2*Alpha2!)
                     INCR B1
                     INCR B2
                     @B1=(@B1*Alpha1!)+(@B2*Alpha2!)
                     INCR B1
                     INCR B2
                     @B1=(@B1*Alpha1!)+(@B2*Alpha2!)
                     FUNCTION=RGB1&
                END FUNCTION
                Chris Boss
                Computer Workshop
                Developer of "EZGUI"
                http://cwsof.com
                http://twitter.com/EZGUIProGuy

                Comment


                • #9
                  Paul,

                  Most of the time mixing of two colors means mixing of pigment, not light.

                  If you look at my alphablending example, something like this would be used in alphablending bitmaps (albiet a faster version of course).
                  Chris Boss
                  Computer Workshop
                  Developer of "EZGUI"
                  http://cwsof.com
                  http://twitter.com/EZGUIProGuy

                  Comment


                  • #10
                    Color tool

                    Here is a nice little program by Werner Henning from Berlin to help you choose a nice group of colors.
                    It is called Pick_RGB_Color.
                    Search in Werner's post for the source.
                    You can also simply use it as a quick reminder of all those color names.
                    Very handy if you have 2 monitors.


                    Attached Files
                    Last edited by Guy Dombrowski; 3 Jun 2009, 08:22 AM. Reason: Added info
                    Old QB45 Programmer

                    Comment


                    • #11
                      Originally posted by Guy Dombrowski View Post
                      Here is a nice little program by Werner Henning from Berlin to help you choose a nice group of colors.
                      It is called Pick_RGB_Color.
                      Search in Werner's post for the source.
                      Do you have a link to this code? It would be appreciated. I tried searching several different ways. Thx.

                      =======================================
                      "Looking foolish does the spirit good."
                      John Updike
                      =======================================
                      It's a pretty day. I hope you enjoy it.

                      Gösta

                      JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                      LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                      Comment


                      • #12
                        Here it is Gosta

                        http://www.powerbasic.com/support/pb...ad.php?t=40144
                        Old QB45 Programmer

                        Comment


                        • #13
                          Pick_RGB_Color.bas

                          And here is the version I improved a bit


                          You had to search in all Werner Henning's posts
                          Attached Files
                          Last edited by Guy Dombrowski; 3 Jun 2009, 10:45 PM.
                          Old QB45 Programmer

                          Comment


                          • #14
                            Originally posted by Guy Dombrowski View Post
                            And here is the version I improved a bit
                            This doesn't put anything in the Clipboard (running in PNWin9, not CC).
                            You had to search in all Werner Henning's posts
                            A search for "Werner Henning" only shows this thread.

                            {Later} I have been looking at the code and I am puzzled, Guy. I see a lot of equates (ie "%RGB_INDIANRED, %RGB_LIGHTCORAL, %RGB_SALMON,") but can't find where they are assigned.
                            '
                            Code:
                            'PBWIN 9.01 - WinApi 05/2008 - XP Pro SP3
                            #Compile Exe                                
                            #Dim All 
                            #Optimize SPEED  'Fly baby Fly!!!
                            #Debug Display On '<<<<<<<<<<<<<<< Remember to turn off for production code
                             
                            Function PBMain         
                             ? Str$(%RGB_INDIANRED)
                            End Function 
                            '
                            Obviously they are assigned by PB but I've never seen them before.

                            =====================================
                            Diligence is the mother of good luck.
                            Ben Franklin
                            =====================================


                            ============================================
                            A little sincerity is a dangerous thing,
                            and a great deal of it is absolutely fatal.
                            Oscar Wilde
                            ============================================
                            Last edited by Gösta H. Lovgren-2; 4 Jun 2009, 12:15 AM. Reason: Added "Later"
                            It's a pretty day. I hope you enjoy it.

                            Gösta

                            JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                            LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                            Comment


                            • #15
                              Gösta
                              Look in PBWIN 9.0 help as follows:
                              Contents-Programming Reference-Data Types-Constants-Built in RGB Color Equates
                              Or page 75 of the printed PBWin manual.

                              What I find interesting is that this says it was originally written in PBCC 5.0 and works once there then the process stayed running on my machine, and the next time I tried to run it, it gives me the undefined equate error on the first ARRAY ASSIGN state with the colour equates.

                              I didn't check the copy to clipboard feature but the rest of it runs fine in PBWIN 9.0.

                              Also a curiosity, albeit extremely minor at this point, is the fact that these Built in Colour Equates are not available in PBCC 5.0.
                              Rod
                              I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                              Comment


                              • #16
                                Built In RGB Color Equates

                                There is a little bug in the Help file of PB/CC 5.01
                                If you try to access that item the link will not work but if you go to the Built In String Equate and then use the link at the bottom, you will get there.
                                Old QB45 Programmer

                                Comment


                                • #17
                                  This board is not an official communications venue.

                                  Please report bugs directly to the official bug-reporting address, [email protected].
                                  Michael Mattias
                                  Tal Systems (retired)
                                  Port Washington WI USA
                                  [email protected]
                                  http://www.talsystems.com

                                  Comment


                                  • #18
                                    Originally posted by Rodney Hicks View Post
                                    Gösta
                                    Look in PBWIN 9.0 help as follows:
                                    Or page 75 of the printed PBWin manual.
                                    RTFM, huh? Who'da thot? Thanks Rod.


                                    What I find interesting is that this says it was originally written in PBCC 5.0 and works once there then the process stayed running on my machine, and the next time I tried to run it, it gives me the undefined equate error on the first ARRAY ASSIGN state with the colour equates.

                                    I didn't check the copy to clipboard feature but the rest of it runs fine in PBWIN 9.0.
                                    A problem I have is it doesn't fit on my screen right, getting the colors all to display. Another is that it doesn't End cleanly. I have to ACD. I just figured it needed a Dalog End somewhere. (Haven't looked it closer yet.)

                                    Great to find those equates though. I wonder how many other goodies PB is hiding?

                                    ================================
                                    "Glory is fleeting,
                                    but obscurity is forever."
                                    Napoleon Bonaparte (1769-1821)
                                    ================================
                                    It's a pretty day. I hope you enjoy it.

                                    Gösta

                                    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                                    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                                    Comment


                                    • #19
                                      It turns out that I was looking in PBCC 5.0. Downloaded but did not install PBCC 5.01, probably because of time constraints.
                                      I thought I was using PBCC 5.01.
                                      Rod
                                      I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                                      Comment


                                      • #20
                                        A problem I have is it doesn't fit on my screen right, getting the colors all to display
                                        Try changing the font eg. $fontname1 = "Terminal" ?

                                        BTW There are more named color equates here..
                                        http://www.powerbasic.com/support/pb...d.php?p=316485
                                        Rgds, Dave

                                        Comment

                                        Working...
                                        X