Solved

HOWTO: Mix two colors with ALPHA?

Posted on 2003-11-05
6
607 Views
Last Modified: 2012-05-04
Lets say you've got two colors... a background and a foreground.  The background is BLUE [RGB(0,0,255)], the foreground is RED [RGB(255,0,0)].  Now if you want to overlay the foreground color onto the background color with varying ALPHA blending (opacity)... lets just say 150 (out of a scale of 0...255).  What's the code (in C, C++, Delphi, or VB) to combine the two colors with an ALPHA value of 150? or 100? or 50?

I'm just trying to figure out how ALPHA color combining works.  I know you can take the RGB value, increase, or decrease each of the R, G, and B values (min = 0, max = 255) to brighten or darken an image... but blending two RGB color values... I'm not sure what equation for that is.

Thanks,

Kevin Wilson
TheVBZone.com
TheVBZone.net
VBZ@thevbzone.com
0
Comment
Question by:HATCHET
6 Comments
 
LVL 14

Accepted Solution

by:
aelatik earned 250 total points
ID: 9692434
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9692722
You should have try this:
http://www.fastgraph.com/help/alpha_blending.html

~ fantasy ~
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9693363
Paste this ENTIRE code into a notepad and save it as "Form1.frm". (Or whatever name as long as it has a ".frm" extension)
Start a new project and remove its original form.
Add this form instead of the old one.
Under project properties menu, set Form1 as the startup object.

Run the program and enyoj!

VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   7830
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   11655
   LinkTopic       =   "Form1"
   ScaleHeight     =   7830
   ScaleWidth      =   11655
   StartUpPosition =   3  'Windows Default
   Begin VB.Frame Frame3
      Caption         =   "Opacity"
      Height          =   1095
      Left            =   6960
      TabIndex        =   22
      Top             =   4080
      Width           =   3735
      Begin MSComctlLib.Slider SliderOpacity
         Height          =   255
         Left            =   240
         TabIndex        =   23
         Top             =   480
         Width           =   2775
         _ExtentX        =   4895
         _ExtentY        =   450
         _Version        =   393216
         Max             =   255
         SelStart        =   50
         TickStyle       =   1
         TickFrequency   =   32
         Value           =   255
      End
      Begin VB.Label Opacity
         Caption         =   "255"
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   238
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   255
         Left            =   3120
         TabIndex        =   24
         Top             =   480
         Width           =   495
      End
   End
   Begin VB.Frame Frame2
      Caption         =   "Foreground"
      Height          =   1695
      Left            =   6960
      TabIndex        =   12
      Top             =   2160
      Width           =   3735
      Begin MSComctlLib.Slider SliderFBlue
         Height          =   255
         Left            =   960
         TabIndex        =   13
         Top             =   1080
         Width           =   2055
         _ExtentX        =   3625
         _ExtentY        =   450
         _Version        =   393216
         Max             =   255
         SelStart        =   255
         TickStyle       =   1
         TickFrequency   =   32
         Value           =   255
      End
      Begin MSComctlLib.Slider SliderFGreen
         Height          =   255
         Left            =   960
         TabIndex        =   14
         Top             =   720
         Width           =   2055
         _ExtentX        =   3625
         _ExtentY        =   450
         _Version        =   393216
         Max             =   255
         SelStart        =   255
         TickStyle       =   1
         TickFrequency   =   32
         Value           =   255
      End
      Begin MSComctlLib.Slider SliderFRed
         Height          =   255
         Left            =   960
         TabIndex        =   15
         Top             =   360
         Width           =   2055
         _ExtentX        =   3625
         _ExtentY        =   450
         _Version        =   393216
         Max             =   255
         SelStart        =   255
         TickStyle       =   1
         TickFrequency   =   32
         Value           =   255
      End
      Begin VB.Label Label1
         Alignment       =   1  'Right Justify
         Caption         =   "Red"
         Height          =   255
         Index           =   1
         Left            =   240
         TabIndex        =   21
         Top             =   360
         Width           =   615
      End
      Begin VB.Label Label2
         Alignment       =   1  'Right Justify
         Caption         =   "Green"
         Height          =   255
         Index           =   1
         Left            =   240
         TabIndex        =   20
         Top             =   720
         Width           =   615
      End
      Begin VB.Label Label3
         Alignment       =   1  'Right Justify
         Caption         =   "Blue"
         Height          =   255
         Index           =   1
         Left            =   240
         TabIndex        =   19
         Top             =   1080
         Width           =   615
      End
      Begin VB.Label Fred
         Caption         =   "255"
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   238
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H000000FF&
         Height          =   255
         Left            =   3120
         TabIndex        =   18
         Top             =   360
         Width           =   495
      End
      Begin VB.Label FGreen
         Caption         =   "255"
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   238
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H00008000&
         Height          =   255
         Left            =   3120
         TabIndex        =   17
         Top             =   720
         Width           =   495
      End
      Begin VB.Label FBlue
         Caption         =   "255"
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   238
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H00FF0000&
         Height          =   255
         Left            =   3120
         TabIndex        =   16
         Top             =   1080
         Width           =   495
      End
   End
   Begin VB.Frame Frame1
      Caption         =   "Background"
      Height          =   1695
      Left            =   6960
      TabIndex        =   2
      Top             =   240
      Width           =   3735
      Begin MSComctlLib.Slider SliderBBlue
         Height          =   255
         Left            =   960
         TabIndex        =   3
         Top             =   1080
         Width           =   2055
         _ExtentX        =   3625
         _ExtentY        =   450
         _Version        =   393216
         Max             =   255
         SelStart        =   255
         TickStyle       =   1
         TickFrequency   =   32
         Value           =   255
      End
      Begin MSComctlLib.Slider SliderBGreen
         Height          =   255
         Left            =   960
         TabIndex        =   4
         Top             =   720
         Width           =   2055
         _ExtentX        =   3625
         _ExtentY        =   450
         _Version        =   393216
         Max             =   255
         SelStart        =   255
         TickStyle       =   1
         TickFrequency   =   32
         Value           =   255
      End
      Begin MSComctlLib.Slider SliderBRed
         Height          =   255
         Left            =   960
         TabIndex        =   5
         Top             =   360
         Width           =   2055
         _ExtentX        =   3625
         _ExtentY        =   450
         _Version        =   393216
         Max             =   255
         SelStart        =   255
         TickStyle       =   1
         TickFrequency   =   32
         Value           =   255
      End
      Begin VB.Label Label1
         Alignment       =   1  'Right Justify
         Caption         =   "Red"
         Height          =   255
         Index           =   0
         Left            =   240
         TabIndex        =   11
         Top             =   360
         Width           =   615
      End
      Begin VB.Label Label2
         Alignment       =   1  'Right Justify
         Caption         =   "Green"
         Height          =   255
         Index           =   0
         Left            =   240
         TabIndex        =   10
         Top             =   720
         Width           =   615
      End
      Begin VB.Label Label3
         Alignment       =   1  'Right Justify
         Caption         =   "Blue"
         Height          =   255
         Index           =   0
         Left            =   240
         TabIndex        =   9
         Top             =   1080
         Width           =   615
      End
      Begin VB.Label BRed
         Caption         =   "255"
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   238
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H000000FF&
         Height          =   255
         Left            =   3120
         TabIndex        =   8
         Top             =   360
         Width           =   495
      End
      Begin VB.Label BGreen
         Caption         =   "255"
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   238
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H00008000&
         Height          =   255
         Left            =   3120
         TabIndex        =   7
         Top             =   720
         Width           =   495
      End
      Begin VB.Label BBlue
         Caption         =   "255"
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   238
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H00FF0000&
         Height          =   255
         Left            =   3120
         TabIndex        =   6
         Top             =   1080
         Width           =   495
      End
   End
   Begin VB.PictureBox Picture2
      Appearance      =   0  'Flat
      BackColor       =   &H80000005&
      ForeColor       =   &H80000008&
      Height          =   3735
      Left            =   480
      ScaleHeight     =   3705
      ScaleWidth      =   4785
      TabIndex        =   1
      Top             =   720
      Width           =   4815
      Begin VB.PictureBox Picture3
         Appearance      =   0  'Flat
         BackColor       =   &H80000005&
         ForeColor       =   &H80000008&
         Height          =   2535
         Left            =   1200
         ScaleHeight     =   2505
         ScaleWidth      =   3105
         TabIndex        =   25
         Top             =   840
         Width           =   3135
      End
   End
   Begin VB.PictureBox Picture1
      Appearance      =   0  'Flat
      BackColor       =   &H80000005&
      ForeColor       =   &H80000008&
      Height          =   4815
      Left            =   120
      ScaleHeight     =   4785
      ScaleWidth      =   6585
      TabIndex        =   0
      Top             =   360
      Width           =   6615
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Form_Load()
    Call PaintBack
    Call PaintFore
End Sub

Private Sub Slider1_Click()

End Sub

Private Sub Slider1_Scroll()
    Call PaintOpacity
End Sub

Private Sub SliderBBlue_Scroll()
    BBlue.Caption = Format(SliderBBlue.Value)
    Call PaintBack
End Sub

Private Sub SliderBGreen_Scroll()
    BGreen.Caption = Format(SliderBGreen.Value)
    Call PaintBack
End Sub

Private Sub SliderBRed_Scroll()
    BRed.Caption = Format(SliderBRed.Value)
    Call PaintBack
End Sub

Private Sub SliderFBlue_Scroll()
    FBlue.Caption = Format(SliderFBlue.Value)
    Call PaintFore
End Sub

Private Sub SliderFGreen_Scroll()
    FGreen.Caption = Format(SliderFGreen.Value)
    Call PaintFore
End Sub

Private Sub SliderFRed_Scroll()
    Fred.Caption = Format(SliderFRed.Value)
    Call PaintFore
End Sub

Private Sub SliderOpacity_Scroll()
    Opacity.Caption = SliderOpacity.Value
    Call PaintOpacity
End Sub

Sub PaintOpacity()
    Dim OpacityPercent As Double
    Dim TransparencyPercent As Double
    Dim ORed As Long
    Dim OGreen As Long
    Dim OBlue As Long
         
    OpacityPercent = SliderOpacity.Value / (255 / 100)
    TransparencyPercent = 100 - OpacityPercent
   
    ORed = Val(BRed.Caption) * TransparencyPercent / 100
    OGreen = Val(BGreen.Caption) * TransparencyPercent / 100
    OBlue = Val(BBlue.Caption) * TransparencyPercent / 100
   
    ORed = Val(Fred.Caption) * OpacityPercent / 100 + ORed
    OGreen = Val(FGreen.Caption) * OpacityPercent / 100 + OGreen
    OBlue = Val(FBlue.Caption) * OpacityPercent / 100 + OBlue
   
    Picture3.BackColor = OBlue * 256 * 256 + OGreen * 256 + ORed
   
End Sub

Sub PaintFore()
    Picture2.BackColor = Val(FBlue.Caption) * 256 * 256 + Val(FGreen.Caption) * 256 + Val(Fred.Caption)
    Call PaintOpacity
End Sub

Sub PaintBack()
    Picture1.BackColor = Val(BBlue.Caption) * 256 * 256 + Val(BGreen.Caption) * 256 + Val(BRed.Caption)
    Call PaintOpacity
End Sub

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:dbrckovi
ID: 9693398
I know that my comment is maybe too big, but it is easier to give you the entire form then to explain where to put each control.

Hope it helped you.

P.S. Some pieces of code can be optimized, but I wanted it to be as easy to understand as possible.
0
 
LVL 3

Author Comment

by:HATCHET
ID: 9695078
aelatik,

I'll look into your vbAccelerator.com example.  That guy is INCREDIBLE with what he can do, but his code is SO COMPLEX and all over the place!  He writes code in a very C / Java style and it takes me a while to weed through it and pick out the useful parts.  I can usually take his code (which requires an external DLL, 3 classes, and a module or two) and condense it into 1 or 2 functions... but it takes me time.
____________________________

fantasy1001,

Your URL points to a reference manual for a software package called "FastGraph 6.0 for Windows" that costs $300 - $500.  I'm looking for some code I can put into Visual Basic (preferably with no dependency files or libraries)
____________________________

dbrckovi,

I'll take a look at your code.  Thanks !
By the way, you forgot to mention the dependency of "Microsoft Windows Common Controls 6.0"   ;)

0
 
LVL 3

Author Comment

by:HATCHET
ID: 9698840
aelatik,

You were the first to answer and the code at the URL you posted worked EXACTLY like I wanted!!  Thank You!

Here's the code (slightly cleaned up... er... modified):
_________________________________

Option Explicit

Private Declare Function OleTranslateColor Lib "OLEPRO32.DLL" (ByVal lngColor As Long, ByVal hPalette As Long, Return_Color As Long) As Long

' Blend the two specified colors
Public Function BlendColor(ByVal lngColor1 As OLE_COLOR, ByVal lngColor2 As OLE_COLOR, Optional ByVal lngOpacityLevel As Byte = 128) As Long
   
   Dim lSrcR As Long
   Dim lSrcG As Long
   Dim lSrcB As Long
   Dim lDstR As Long
   Dim lDstG As Long
   Dim lDstB As Long
   
   lngColor1 = TranslateColor(lngColor1)
   lngColor2 = TranslateColor(lngColor2)
   
   lSrcR = lngColor1 And &HFF
   lSrcG = (lngColor1 And &HFF00&) \ &H100&
   lSrcB = (lngColor1 And &HFF0000) \ &H10000
   lDstR = lngColor2 And &HFF
   lDstG = (lngColor2 And &HFF00&) \ &H100&
   lDstB = (lngColor2 And &HFF0000) \ &H10000
   
   BlendColor = RGB(((lSrcR * lngOpacityLevel) / 255) + ((lDstR * (255 - lngOpacityLevel)) / 255), _
                    ((lSrcG * lngOpacityLevel) / 255) + ((lDstG * (255 - lngOpacityLevel)) / 255), _
                    ((lSrcB * lngOpacityLevel) / 255) + ((lDstB * (255 - lngOpacityLevel)) / 255))

End Function

' Convert Automation color to Windows color
Private Function TranslateColor(ByVal lngColor As OLE_COLOR, Optional ByVal hPal As Long = 0) As Long
   If OleTranslateColor(lngColor, hPal, TranslateColor) Then TranslateColor = -1
End Function

Private Sub Form_Load()
   Me.BackColor = BlendColor(vbRed, vbBlue, 128)
End Sub
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question