Sign in to follow this  
Followers 0
cppman

Image Blending

2 posts in this topic

#1 ·  Posted (edited)

Okay, it took me all (and i mean) all day to get the formula right for alpha blending.. but i finally got it :P i was so happy.

So I wrote a UDF for people to use, and an example of what it is/does.

the udf is in: trans.au3

it is the following:

_CreateAlpha($color1, $color2, $alpha)

$color1 & $color2 need to be an array of RGB values. As so in this program.

The alpha value needs to be a number between 0.0 and 1.0 (1 = completely visible, 0 = completly covered by the color you chose)

here is the program example: *NOTE: the program is really slow considering the way i did it :)*

in order to run it u need this file: trans.au3

#include "trans.au3"
#include <misc.au3>
AutoItSetOption("PixelCoordMode", 0)
Global $IMAGE_LOCATION = ""
Global $SHADE_COLOR = 0x000000
$Form1 = GUICreate("Image Blending", 861, 258, 131, 139)
$Group1 = GUICtrlCreateGroup("Before", 3, 4, 314, 249)
$BeforePic = GUICtrlCreatePic("", 8, 18, 303, 230, BitOR($SS_NOTIFY,$WS_GROUP))
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("After", 318, 4, 328, 249)
$AfterGraphic = GUICtrlCreateGraphic(324, 20, 317, 228)
GUICtrlSetColor(-1, 0xECE9D8)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("Load Image", 649, 11, 174, 23, 0)
$Button2 = GUICtrlCreateButton("Shade Color", 648, 36, 174, 23, 0)
$Button3 = GUICtrlCreateButton("Start Shader", 648, 60, 174, 23, 0)
GUISetState(@SW_SHOW)
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $Button1
        $IMAGE_LOCATION = FileOpenDialog("Choose an Image", "", "Image Files(*.bmp;*.jpg;*.gif)")
        GUICtrlSetImage($BeforePic, $IMAGE_LOCATION)
    case $msg = $Button2
        $SHADE_COLOR = _ChooseColor()
    case $msg = $Button3
        StartShader()
    EndSelect
WEnd
Exit

Func StartShader()
    $al_value = InputBox("Alpha Value", "Enter the alpha value for your selected color: (0.0 - 1.0)")
    GUICtrlSetImage($BeforePic, $IMAGE_LOCATION)
    Local $pixel_x
    Local $pixel_y
    Local $pixel_color
    Local $graphic_x
    Local $graphic_y
    Local $progress
    $pos = WinGetPos("Image Blending")
    $cpos = ControlGetPos("", "", $BeforePic)
    $image_width = $cpos[2]
    $image_height = $cpos[3]
    $image_x = $cpos[0]+3
    $image_y = $cpos[1]+30
    for $x = $image_x to $image_width
        for $y = $image_y to $image_height
            $graphic_y += 1
            $color = PixelGetColor($x, $y)
            $PixelColor = _HexToRGB($color)
            $ShaderColor = _HexToRGB($SHADE_COLOR)
            $alpha = _CreateAlpha($PixelColor, $ShaderColor, $al_value)
            GUICtrlSetGraphic($AfterGraphic, $GUI_GR_COLOR, $alpha)
            GUICtrlSetGraphic($AfterGraphic, $GUI_GR_PIXEL, $x-11, $y-(18+30))
        Next
        GUICtrlSetGraphic($AfterGraphic, $GUI_GR_REFRESH)
    Next
EndFunc
Edited by CHRIS95219

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

That's funny.. because when i googled "alpha blending" this was the first thing that popped up: Value = Value0(1.0 - Alpha) + Value1(Alpha)

:)

Anyway, nice function. May come in handy one day. :P

Edited by Manadar

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0