Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# HOWTO: Mix two colors with ALPHA?

Posted on 2003-11-05
Medium Priority
629 Views
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
Question by:HATCHET
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 14

Accepted Solution

aelatik earned 1000 total points
ID: 9692434
0

LVL 5

Expert Comment

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

~ fantasy ~
0

LVL 11

Expert Comment

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.
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
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
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

LVL 11

Expert Comment

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

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

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

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

## Featured Post

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month4 days, 19 hours left to enroll