Jump to content

Is there better way to write this function?


Recommended Posts

Is there better way to write that function? I know there is always better way to make that script. But I ran out of ideas :S

Func _InvertColor($iColor, $mode = 2)
    $iColor = String(Hex($iColor, 6))
    If $mode = 0 Then
        $RColor = StringTrimLeft(StringTrimRight($iColor, 4), 0)
        $GColor = StringTrimLeft(StringTrimRight($iColor, 2), 2)
        $BColor = StringTrimLeft(StringTrimRight($iColor, 0), 4)
        $InvertedRColor = Hex(255 - Dec($RColor), 2)
        $InvertedGColor = Hex(255 - Dec($GColor), 2)
        $InvertedBColor = Hex(255 - Dec($BColor), 2)
    ElseIf $mode = 1 Then
        $InvertedBColor = StringTrimLeft(StringTrimRight($iColor, 4), 0)
        $InvertedGColor = StringTrimLeft(StringTrimRight($iColor, 2), 2)
        $InvertedRColor = StringTrimLeft(StringTrimRight($iColor, 0), 4)
    ElseIf $mode = 2 Then   
        $BColor = StringTrimLeft(StringTrimRight($iColor, 4), 0)
        $GColor = StringTrimLeft(StringTrimRight($iColor, 2), 2)
        $RColor = StringTrimLeft(StringTrimRight($iColor, 0), 4)
        $InvertedRColor = Hex(255 - Dec($RColor), 2)
        $InvertedGColor = Hex(255 - Dec($GColor), 2)
        $InvertedBColor = Hex(255 - Dec($BColor), 2)
    EndIf
    $InvertColor = $InvertedRColor & $InvertedGColor & $InvertedBColor
    Return "0x" & $InvertColor
EndFunc   ;==>_InvertColor

edited

Link to comment
Share on other sites

ConsoleWrite(_InvertColor(0x000000))
Func _InvertColor($iColor)

    Return  0xFFFFFF - $icolor
EndFunc   ;==>_InvertColor
I don't think that will work, or at least not very often.

Sorry misunderstood what was happening there, it's nice and simple.

There are some approaches in this thread.

Edited by martin
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.
Link to comment
Share on other sites

Try this.

;
Local $col = 0x7AB809

ConsoleWrite(_InvertColor($col, 0) & @CRLF)
ConsoleWrite(_InvertColor($col, 1) & @CRLF)
ConsoleWrite(_InvertColor($col, 2) & @CRLF & @CRLF)

ConsoleWrite(_InvertColorA($col, 0) & @CRLF)
ConsoleWrite(_InvertColorA($col, 1) & @CRLF)
ConsoleWrite(_InvertColorA($col, 2) & @CRLF)

Func _InvertColor($iColor, $mode = 0)
    Switch $mode
        Case 0
            Return "0x" & Hex(0xFFFFFF - $iColor, 6)
        Case 1
            Return "0x" & StringRegExpReplace(Hex($iColor, 6), "(.{2})(.{2})(.{2})", "\3\2\1")
        Case 2
            Return "0x" & StringRegExpReplace(Hex(0xFFFFFF - $iColor, 6), "(.{2})(.{2})(.{2})", "\3\2\1")
    EndSwitch
EndFunc  ;==>_InvertColor

Func _InvertColorA($iColor, $mode = 2)
    $iColor = String(Hex($iColor, 6))
    If $mode = 0 Then
        $RColor = StringTrimLeft(StringTrimRight($iColor, 4), 0)
        $GColor = StringTrimLeft(StringTrimRight($iColor, 2), 2)
        $BColor = StringTrimLeft(StringTrimRight($iColor, 0), 4)
        $InvertedRColor = Hex(255 - Dec($RColor), 2)
        $InvertedGColor = Hex(255 - Dec($GColor), 2)
        $InvertedBColor = Hex(255 - Dec($BColor), 2)
    ElseIf $mode = 1 Then
        $InvertedBColor = StringTrimLeft(StringTrimRight($iColor, 4), 0)
        $InvertedGColor = StringTrimLeft(StringTrimRight($iColor, 2), 2)
        $InvertedRColor = StringTrimLeft(StringTrimRight($iColor, 0), 4)
    ElseIf $mode = 2 Then
        $BColor = StringTrimLeft(StringTrimRight($iColor, 4), 0)
        $GColor = StringTrimLeft(StringTrimRight($iColor, 2), 2)
        $RColor = StringTrimLeft(StringTrimRight($iColor, 0), 4)
        $InvertedRColor = Hex(255 - Dec($RColor), 2)
        $InvertedGColor = Hex(255 - Dec($GColor), 2)
        $InvertedBColor = Hex(255 - Dec($BColor), 2)
    EndIf
    $InvertColor = $InvertedRColor & $InvertedGColor & $InvertedBColor
    Return "0x" & $InvertColor
EndFunc  ;==>_InvertColorA
;
Link to comment
Share on other sites

it returns lightblue (blue + green) from red :D

I'm not sure if you're saying it's wrong but it works for me.

Red 0xff0000 -> Cyan 0x00ffff

Green 0x00ff00 -> Magenta 0xff00ff

Blue 0x0000ff -> Yellow 0xffff00

Edited by weaponx
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...