Jump to content
Sign in to follow this  
Zepx

Colour Gradient Algorithm

Recommended Posts

Zepx

Hi,

wanted to ask if anyone here knows how I can create gradients with a suitable algorithm?

Share this post


Link to post
Share on other sites
weaponx

I converted this script here:

http://community.livejournal.com/digitela/4525.html

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

Dim $GUIWidth = 400
Dim $GUIHeight = 400

GUICreate("Vertical Gradient", $GUIWidth, $GUIHeight)  ; will create a dialog box that when displayed is centered
GUISetState(@SW_SHOW)       ; will display an empty dialog box

$Graphic = GUICtrlCreateGraphic (0,0, $GUIWidth, $GUIHeight)

;Fill GUI wwith vertical gradient
GradientFill($Graphic, 0, 0, $GUIWidth, $GUIHeight, _ArrayCreate(0,0,0), _ArrayCreate(255,255,255)); 

GUICtrlSetGraphic($Graphic,$GUI_GR_REFRESH)

While 1
    $msg = GUIGetMsg()

    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

Func GradientFill($im, $x1, $y1, $width, $height, $left_color, $right_color)

    $color0=($left_color[0]-$right_color[0])/$height
    $color1=($left_color[1]-$right_color[1])/$height
    $color2=($left_color[2]-$right_color[2])/$height
    For $Y=0 to $height

        $red=$left_color[0]-floor($Y*$color0)
        $green=$left_color[1]-floor($Y*$color1)
        $blue=$left_color[2]-floor($Y*$color2)
        
        ;Convert RGB to decimal
        $col = Dec(Hex($blue,2) & Hex($green,2) & Hex($red,2))
        
        GUICtrlSetGraphic($im,$GUI_GR_COLOR, $col)
        GUICtrlSetGraphic($im,$GUI_GR_MOVE,0,$Y)
        GUICtrlSetGraphic($im,$GUI_GR_LINE,$GUIWidth,$Y)
    Next
EndFunc

EDIT: Cleaned up some ugly code

Edited by weaponx

Share this post


Link to post
Share on other sites
weaponx

Radial gradient:

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

Dim $GUIWidth = 400
Dim $GUIHeight = 400

GUICreate("Radial Gradient", $GUIWidth, $GUIHeight)  ; will create a dialog box that when displayed is centered
GUISetBkColor(0xFFFFFF)
GUISetState(@SW_SHOW)       ; will display an empty dialog box

$Graphic = GUICtrlCreateGraphic (0,0, $GUIWidth, $GUIHeight)

;Fill GUI with radial gradient
RadialGradientFill($Graphic, $GUIWidth/2, $GUIHeight/2, 200, _ArrayCreate(0,0,0), _ArrayCreate(255,255,255)); 

GUICtrlSetGraphic($Graphic,$GUI_GR_REFRESH)

While 1
    $msg = GUIGetMsg()

    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

;im = Handle to graphic control
;x1 = Circle center X
;y1 = Circle center Y
;radius = Circle radius
;left_color = start color
;right_color = end color
Func RadialGradientFill($im, $x1, $y1, $radius, $left_color, $right_color)

    $color0=($left_color[0]-$right_color[0])/$radius
    $color1=($left_color[1]-$right_color[1])/$radius
    $color2=($left_color[2]-$right_color[2])/$radius
    For $Y=$radius to 0 Step -1

        $red=$left_color[0]-floor($Y*$color0)
        $green=$left_color[1]-floor($Y*$color1)
        $blue=$left_color[2]-floor($Y*$color2)
        
        ;Convert RGB to decimal
        $col = Dec(Hex($blue,2) & Hex($green,2) & Hex($red,2))
        
        GUICtrlSetGraphic($im,$GUI_GR_COLOR, $col,$col)
        GUICtrlSetGraphic($im,$GUI_GR_ELLIPSE,$x1-$Y,$y1-$Y,$Y*2,$Y*2)
    Next
EndFunc
Edited by weaponx

Share this post


Link to post
Share on other sites
Greenhorn

Moin,

it looks very nice ! :(

Thank you very much weaponx !

Maybe it would be nice if you could create an UDF collection of gradient fill and post it in the Example Script Forum ... :)

Greetz

Greenhorn

Share this post


Link to post
Share on other sites
martin

Circular gradient:

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

Dim $GUIWidth = 400
Dim $GUIHeight = 400

GUICreate("Vertical Gradient", $GUIWidth, $GUIHeight)  ; will create a dialog box that when displayed is centered
GUISetBkColor(0xFFFFFF)
GUISetState(@SW_SHOW)       ; will display an empty dialog box

$Graphic = GUICtrlCreateGraphic (0,0, $GUIWidth, $GUIHeight)

;Fill GUI wwith vertical gradient
CircularGradientFill($Graphic, $GUIWidth/2, $GUIHeight/2, 200, _ArrayCreate(0,0,0), _ArrayCreate(255,255,255)); 

GUICtrlSetGraphic($Graphic,$GUI_GR_REFRESH)

While 1
    $msg = GUIGetMsg()

    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

;im = Handle to graphic control
;x1 = Circle center X
;y1 = Circle center Y
;radius = Circle radius
;left_color = start color
;right_color = end color
Func CircularGradientFill($im, $x1, $y1, $radius, $left_color, $right_color)

    $color0=($left_color[0]-$right_color[0])/$radius
    $color1=($left_color[1]-$right_color[1])/$radius
    $color2=($left_color[2]-$right_color[2])/$radius
    For $Y=$radius to 0 Step -1

        $red=$left_color[0]-floor($Y*$color0)
        $green=$left_color[1]-floor($Y*$color1)
        $blue=$left_color[2]-floor($Y*$color2)
        
        ;Convert RGB to decimal
        $col = Dec(Hex($blue,2) & Hex($green,2) & Hex($red,2))
        
        GUICtrlSetGraphic($im,$GUI_GR_COLOR, $col,$col)
        GUICtrlSetGraphic($im,$GUI_GR_ELLIPSE,$x1-$Y,$y1-$Y,$Y*2,$Y*2)
    Next
EndFunc
Very nice weaponx.

Don't forget to add GUICtrlSetState($Graphic,$GUI_DISABLE) in case you want buttons etc on the form.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
weaponx

Some more examples:

Vertical, Horizontal, Reflected, Radial

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

Dim $GUIWidth = 400
Dim $GUIHeight = 800

GUICreate("Gradient Example", $GUIWidth, $GUIHeight)  ; will create a dialog box that when displayed is centered
GUISetBkColor(0xFFFFFF)
GUISetState(@SW_SHOW)       ; will display an empty dialog box

$Handle = GUICtrlCreateGraphic (0,0, $GUIWidth, $GUIHeight)

;Vertical gradient
VGradientFill($Handle, 0, 0, $GUIWidth, 200, _ArrayCreate(0,0,0), _ArrayCreate(255,255,255))

;Horizontal gradient
HGradientFill($Handle, 0, 200, $GUIWidth, 200, _ArrayCreate(0,0,0), _ArrayCreate(255,255,255))

;Reflected gradient
RGradientFill($Handle, 0, 400, $GUIWidth, 200, _ArrayCreate(0,0,0), _ArrayCreate(255,255,255))

;Radial gradient
RadialGradientFill($Handle, 200, 700, 100, _ArrayCreate(0,0,0), _ArrayCreate(255,255,255))

GUICtrlSetGraphic($Handle,$GUI_GR_REFRESH)

While 1
    $msg = GUIGetMsg()

    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

Func VGradientFill($im, $left, $top, $width, $height, $left_color, $right_color)
    
    Local $MaxY = $height+$top

    $color0=($left_color[0]-$right_color[0])/($MaxY)
    $color1=($left_color[1]-$right_color[1])/($MaxY)
    $color2=($left_color[2]-$right_color[2])/($MaxY)
    For $Y=$top to $MaxY

        $red=$left_color[0]-floor($Y*$color0)
        $green=$left_color[1]-floor($Y*$color1)
        $blue=$left_color[2]-floor($Y*$color2)
       
        ;Convert RGB to decimal
        $color = RGB2DEC($Red,$Green,$Blue)
        
        DrawLine($im,$left, $Y, $left+$width, $Y, $Color)
    Next
EndFunc

Func HGradientFill($im, $left, $top, $width, $height, $left_color, $right_color)
    
    Local $MaxX = $width+$left

    $color0=($left_color[0]-$right_color[0])/($MaxX)
    $color1=($left_color[1]-$right_color[1])/($MaxX)
    $color2=($left_color[2]-$right_color[2])/($MaxX)
    For $X=$left to $MaxX

        $red=$left_color[0]-floor($X*$color0)
        $green=$left_color[1]-floor($X*$color1)
        $blue=$left_color[2]-floor($X*$color2)
        
        ;Convert RGB to decimal
        $color = RGB2DEC($Red,$Green,$Blue)
        
        DrawLine($im,$X, $top, $X, $top+$height, $Color)
    Next
EndFunc

Func RGradientFill($im, $left, $top, $width, $height, $left_color, $right_color)
    
    Local $MaxX = $width+$left
    Local $Middle = $MaxX/2

    $color0=($left_color[0]-$right_color[0])/($Middle)
    $color1=($left_color[1]-$right_color[1])/($Middle)
    $color2=($left_color[2]-$right_color[2])/($Middle)
    For $X=$left to $Middle

        $red=$left_color[0]-floor($X*$color0)
        $green=$left_color[1]-floor($X*$color1)
        $blue=$left_color[2]-floor($X*$color2)
        
        ;Convert RGB to decimal
        $color = RGB2DEC($Red,$Green,$Blue)
        
        DrawLine($im,$X, $top, $X, $top+$height, $Color)
    Next
    
    For $X=$Middle to $MaxX

        $red=$left_color[0]-floor(($MaxX-$X)*$color0)
        $green=$left_color[1]-floor(($MaxX-$X)*$color1)
        $blue=$left_color[2]-floor(($MaxX-$X)*$color2)
        
        ;Convert RGB to decimal
        $color = RGB2DEC($Red,$Green,$Blue)
        
        DrawLine($im,$X, $top, $X, $top+$height, $Color)
    Next
EndFunc

;im = Handle to graphic control
;x1 = Circle center X
;y1 = Circle center Y
;radius = Circle radius
;left_color = start color
;right_color = end color
Func RadialGradientFill($im, $x1, $y1, $radius, $left_color, $right_color)

    $color0=($left_color[0]-$right_color[0])/$radius
    $color1=($left_color[1]-$right_color[1])/$radius
    $color2=($left_color[2]-$right_color[2])/$radius
    For $Y=$radius to 0 Step -1

        $red=$left_color[0]-floor($Y*$color0)
        $green=$left_color[1]-floor($Y*$color1)
        $blue=$left_color[2]-floor($Y*$color2)
        
        ;Convert RGB to decimal
        $col = Dec(Hex($blue,2) & Hex($green,2) & Hex($red,2))
        
        GUICtrlSetGraphic($im,$GUI_GR_COLOR, $col,$col)
        GUICtrlSetGraphic($im,$GUI_GR_ELLIPSE,$x1-$Y,$y1-$Y,$Y*2,$Y*2)
    Next
EndFunc

Func RGB2DEC($RR,$GG,$BB)
    Return Dec(Hex($BB,2) & Hex($GG,2) & Hex($RR,2))
EndFunc

;Draw a line between 2 points with given color
Func DrawLine($GRAPHIC,$X1,$Y1,$X2,$Y2,$COLOR)
    GUICtrlSetGraphic($GRAPHIC,$GUI_GR_COLOR, $COLOR)
    GUICtrlSetGraphic($GRAPHIC,$GUI_GR_MOVE,$X1,$Y1)
    GUICtrlSetGraphic($GRAPHIC,$GUI_GR_LINE,$X2,$Y2)    
EndFunc

Share this post


Link to post
Share on other sites
Zepx

Thanks guys!

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  

×