Sign in to follow this  
Followers 0
adamski

MultiColour Gradients

8 posts in this topic

#1 ·  Posted (edited)

Is there a way to have a gradient box with four colours, one at each corner?

Eg.

Red------Green

|..............|

|..............|

|..............|

|..............|

Blue-------Black

Or:

Black-----Green

|..............|

|..............|

|..............|

|..............|

Black------White

Can a linear gradient have multiple colours?

Eg.

Red-------Green--------Blue------Red

I have searched the forum but only found 2 colour gradients.

Thanks

Edited by adamski

Share this post


Link to post
Share on other sites



I got the multicolour linear part working.

I kinda got something with the 4 corner colour but it is slow and not correct.

; MAIN PROGRAM

#include <Color.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

Global Const $G_VERTICAL = 1
Global Const $G_HORIZONTAL = 2

Const $GUI_Width = 500
Const $GUI_Height = 500
Const $BTN_Width = 80
Const $BTN_Height = 40

Dim $Red     = 0xff0000
Dim $Yellow  = 0xffff00
Dim $Green   = 0x00ff00
Dim $Cyan   = 0x00ffff
Dim $Blue   = 0x0000ff
Dim $Magenta = 0xff00ff

Dim $Black = 0x000000
Dim $White = 0xffffff
Dim $Purple = 0x800080


Dim $ColoursArray[7]
$ColoursArray[0] = $Red
$ColoursArray[1] = $Yellow
$ColoursArray[2] = $Green
$ColoursArray[3] = $Cyan
$ColoursArray[4] = $Blue
$ColoursArray[5] = $Magenta
$ColoursArray[6] = $Red

Dim $ColoursArray2[3]
$ColoursArray2[0] = $Purple
$ColoursArray2[1] = $Yellow
$ColoursArray2[2] = $Purple

Dim $ColoursArray3[4]
$ColoursArray3[0] = $Red
$ColoursArray3[1] = $Green
$ColoursArray3[2] = $White
$ColoursArray3[3] = $Black

Dim $Button_1,$Button_2,$Button_3,$Button_4

$GUI_WIN = CreateWindow()
_GUICtrlCreateGradientMultiColor($ColoursArray2, 0, 0, $GUI_Width,$GUI_Height,$G_HORIZONTAL)
GUISetState()


While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $Button_1
        ExitLoop
    Case $msg = $Button_2
        GUIDelete($GUI_WIN)
        $GUI_WIN = CreateWindow()
        _GUICtrlCreateGradientFourColorBox($ColoursArray3, 0, 0, $GUI_Width,$GUI_Height,$G_HORIZONTAL)
        GUISetState()
    Case $msg = $Button_3
        GUIDelete($GUI_WIN)
        $GUI_WIN = CreateWindow()
        _GUICtrlCreateGradientMultiColor($ColoursArray, 0, 0, $GUI_Width,$GUI_Height,$G_HORIZONTAL)
        GUISetState()
    Case $msg = $Button_4
        GUIDelete($GUI_WIN)
        $GUI_WIN = CreateWindow()
        _GUICtrlCreateGradientMultiColor($ColoursArray, 0, 0, $GUI_Width,$GUI_Height,$G_VERTICAL)
        GUISetState()
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    EndSelect
Wend

Func CreateWindow()
    $hwnd = GuiCreate("Gradient Demo", $GUI_Width, $GUI_Height,(@DesktopWidth-$GUI_Width)/2, (@DesktopHeight-$GUI_Height)/2 , $WS_OVERLAPPEDWINDOW + $WS_CLIPSIBLINGS)

    $Button_1 = GuiCtrlCreateButton("Exit", $GUI_Width - ($BTN_Width + 20), $GUI_Height - ($BTN_Height + 20), $BTN_Width, $BTN_Height)
    $Button_2 = GuiCtrlCreateButton("4 Colour", 10, $GUI_Height - ($BTN_Height + 120), $BTN_Width, $BTN_Height)
    $Button_3 = GuiCtrlCreateButton("Horizontal", 10, $GUI_Height - ($BTN_Height + 70), $BTN_Width, $BTN_Height)
    $Button_4 = GuiCtrlCreateButton("Vertical", 10, $GUI_Height - ($BTN_Height + 20), $BTN_Width, $BTN_Height)
    Return $hwnd
EndFunc


Func _GUICtrlCreateGradientMultiColor($ColoursArray, $nX, $nY, $nWidth, $nHeight, $n_type = $G_VERTICAL)
    
    Local $nColors
    Local $colorR, $colorG, $colorB
    Local $nStepR, $nStepG, $nStepB

    $nColors = UBound($ColoursArray) - 1

    If($n_type == $G_VERTICAL) Then
        
        For $x = 0 To $nColors - 1
        
            GuiCtrlCreateGraphic($nX + ($x)*($nWidth/$nColors), $nY, $nWidth / $nColors, $nHeight)
            $colorR = _ColorGetRed($ColoursArray[$x])
            $colorG = _ColorGetGreen($ColoursArray[$x])
            $colorB = _ColorGetBlue($ColoursArray[$x])
            $nStepR = (_ColorGetRed($ColoursArray[$x+1]) - $colorR) / ($nWidth / $nColors)
            $nStepG = (_ColorGetGreen($ColoursArray[$x+1]) - $colorG) / ($nWidth / $nColors)
            $nStepB = (_ColorGetBlue($ColoursArray[$x+1]) - $colorB) / ($nWidth / $nColors)
            For $i = 0 To $nWidth / $nColors
                $sColor = "0x" & StringFormat("%02X%02X%02X", $colorR+$nStepR*$i, $colorG+$nStepG*$i, $colorB+$nStepB*$i)
                GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $sColor, 0xffffff)
                GUICtrlSetGraphic(-1, $GUI_GR_MOVE, $i, 0)
                GUICtrlSetGraphic(-1, $GUI_GR_LINE, $i, $nHeight)
            Next

        Next
        
    ElseIf($n_type == $G_HORIZONTAL) Then

        For $x = 0 To $nColors - 1
        
            GuiCtrlCreateGraphic($nX, $nY + ($x)*($nHeight / $nColors), $nWidth / $nColors, $nHeight)
            $colorR = _ColorGetRed($ColoursArray[$x])
            $colorG = _ColorGetGreen($ColoursArray[$x])
            $colorB = _ColorGetBlue($ColoursArray[$x])
            $nStepR = (_ColorGetRed($ColoursArray[$x+1]) - $colorR) / ($nWidth / $nColors)
            $nStepG = (_ColorGetGreen($ColoursArray[$x+1]) - $colorG) / ($nWidth / $nColors)
            $nStepB = (_ColorGetBlue($ColoursArray[$x+1]) - $colorB) / ($nWidth / $nColors)
            For $i = 0 To $nHeight / $nColors
                $sColor = "0x" & StringFormat("%02X%02X%02X", $colorR+$nStepR*$i, $colorG+$nStepG*$i, $colorB+$nStepB*$i)
                GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $sColor, 0xffffff)
                GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 0, $i)
                GUICtrlSetGraphic(-1, $GUI_GR_LINE, $nWidth, $i)
            Next
            
        Next
    EndIf
EndFunc



Func _GUICtrlCreateGradientFourColorBox($ColoursArray, $nX, $nY, $nWidth, $nHeight, $n_type = $G_VERTICAL)
    
    Local $nColors
;~   Local $amountA, $amountB, $amountC, $amountD
    Local $distanceA, $distanceB, $distanceC, $distanceD
    Local $pixelR, $pixelB, $pixelG

    $nColors = UBound($ColoursArray)
    
    If $nColors = 4 Then

        Local $colorAR = _ColorGetRed($ColoursArray[0])
        Local $colorAG = _ColorGetGreen($ColoursArray[0])
        Local $colorAB = _ColorGetBlue($ColoursArray[0])
        
        Local $colorBR = _ColorGetRed($ColoursArray[1])
        Local $colorBG = _ColorGetGreen($ColoursArray[1])
        Local $colorBB = _ColorGetBlue($ColoursArray[1])
        
        Local $colorCR = _ColorGetRed($ColoursArray[2])
        Local $colorCG = _ColorGetGreen($ColoursArray[2])
        Local $colorCB = _ColorGetBlue($ColoursArray[2])
        
        Local $colorDR = _ColorGetRed($ColoursArray[3])
        Local $colorDG = _ColorGetGreen($ColoursArray[3])
        Local $colorDB = _ColorGetBlue($ColoursArray[3])
        
        
        GuiCtrlCreateGraphic($nX, $nY, $nWidth, $nHeight)
        
        For $x = 0 To $nWidth
            For $y = 0 To $nHeight
                
;~              $amountA = (($nWidth/$nWidth)-($x/$nWidth))*(($nHeight/$nHeight)-($y/$nHeight))
;~              $amountB = ($x/$nWidth)*(($nHeight/$nHeight)-($y/$nHeight))
;~              $amountC = (($nWidth/$nWidth)-($x/$nWidth))*($y/$nHeight)
;~              $amountD = ($x/$nWidth)*($y/$nHeight)
                
                $distanceA = 1-(Sqrt($x^2 + $y^2)) / $nWidth
                $distanceB = 1-(Sqrt(($nWidth-$x)^2 + $y^2)) / $nWidth
                $distanceC = 1-(Sqrt($x^2 + ($nHeight-$y)^2)) / $nWidth
                $distanceD = 1-(Sqrt(($nWidth-$x)^2 + ($nHeight-$y)^2)) / $nWidth
                
                $pixelR = ($colorAR*$distanceA) + ($colorBR*$distanceB) + ($colorCR*$distanceC) + ($colorDR*$distanceD)
                $pixelG = ($colorAG*$distanceA) + ($colorBG*$distanceB) + ($colorCG*$distanceC) + ($colorDG*$distanceD)
                $pixelB = ($colorAB*$distanceA) + ($colorBB*$distanceB) + ($colorCB*$distanceC) + ($colorDB*$distanceD)
                
                $sColor = "0x" & StringFormat("%02X%02X%02X", $pixelR, $pixelG, $pixelB)
                GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $sColor, 0xffffff)
                GUICtrlSetGraphic(-1, $GUI_GR_PIXEL, $x, $y)
            Next
        Next
        
    EndIf
        
EndFunc

Share this post


Link to post
Share on other sites

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

AutoIt is slow. There's nothing you can do about it.

Sure there is. I posted the names of the two GDI+ functions that do this. There are even a few examples of the lineargradientbrush in the forum that do exactly what the OP wants.

Edited by weaponx

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