Jump to content
argumentum

[solved] Help with fake GUI coloring

Recommended Posts

Derp. Thanks @Nine. I was failing. I passed a hex to Dec and it needed to be a string. I see now. I should be able to convert the function then¬†ūüôĄ


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites
12 minutes ago, seadoggie01 said:

Can you convert hex to integers?

#include <Array.au3>

_ArrayDisplay(CalcRGBAvg(20, 50, 100, 0, 0, 0))
_ArrayDisplay(CalcRGBAvg(0, 0, 255, 255, 0, 0))

Func CalcRGBAvg($iRed, $iGreen, $iBlue, $iRed2, $iGreen2, $iBlue2, $iMidPoints = 9)
    ; Add one to midpoints... don't know why :D
    $iMidPoints += 1
    Local $aColors[0]
    Local $iRedDiff = Abs($iRed - $iRed2)
    Local $bRedRev = ($iRedDiff = $iRed - $iRed2)
    Local $iGreenDiff = Abs($iGreen - $iGreen2)
    Local $bGreenRev = ($iGreenDiff = $iGreen - $iGreen2)
    Local $iBlueDiff = Abs($iBlue - $iBlue2)
    Local $bBlueRev = ($iBlueDiff = $iBlue - $iBlue2)

    For $i = 0 To $iMidPoints
;~         _ArrayAdd($aColors, "RGB(" & _
;~             Abs(($bRedRev ? 0 : -255) + $iRedDiff/$iMidPoints * $i) & ", " & _
;~             Abs(($bGreenRev ? 0 : -255) + $iGreenDiff/$iMidPoints * $i) & ", " & _
;~             Abs(($bBlueRev ? 0 : -255) + $iBlueDiff/$iMidPoints * $i) & ")")
        _ArrayAdd($aColors, "0x" & _
                Hex(Int(Abs(($bRedRev ? 0 : -255) + $iRedDiff / $iMidPoints * $i)), 2) & _
                Hex(Int(Abs(($bGreenRev ? 0 : -255) + $iGreenDiff / $iMidPoints * $i)), 2) & _
                Hex(Int(Abs(($bBlueRev ? 0 : -255) + $iBlueDiff / $iMidPoints * $i)), 2))
    Next
    Return $aColors

EndFunc   ;==>CalcRGBAvg

Thanks :thumbsup:

Share this post


Link to post
Share on other sites

Oh. That's way easier. I was trying to bit shift, rotate, and string mid... was going nowhere fast. That's a much better solution :D


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

@seadoggie01, looked good but it does not do it right for all combinations: 

#include <Misc.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

CalcRGBAvg_Example()
Func CalcRGBAvg_Example()
    Local $aColors, $n, $nMsg, $aLabels[11]
    Local $Form1 = GUICreate(Chr(160) & "CalcRGBAvg Example", 615, 436, 192, 114)
    Local $Button1 = GUICtrlCreateButton("Blend", 128, 24, 75, 25)
    Local $Color1 = GUICtrlCreateLabel("0xFF7979", 16, 8, 76, 25, BitOR($SS_CENTER, $SS_CENTERIMAGE), $WS_EX_STATICEDGE)
    GUICtrlSetBkColor($Color1, GUICtrlRead($Color1))
    GUICtrlSetColor($Color1, _CalcContrastColor(GUICtrlRead($Color1)))
    GUICtrlSetTip($Color1, "Click to seleect color")
    Local $Color2 = GUICtrlCreateLabel("0x9D0000", 17, 41, 76, 25, BitOR($SS_CENTER, $SS_CENTERIMAGE), $WS_EX_STATICEDGE)
    GUICtrlSetBkColor($Color2, GUICtrlRead($Color2))
    GUICtrlSetColor($Color2, _CalcContrastColor(GUICtrlRead($Color2)))
    For $n = 0 To 10
        $aLabels[$n] = GUICtrlCreateLabel("Label" & $n, 18, 81 + (25 * $n), 76, 25, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    Next

    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
            Case $Color1
                $n = _ChooseColor(2, GUICtrlRead($Color1), 2, $Form1)
                If $n = -1 Then ContinueLoop
                GUICtrlSetBkColor($Color1, $n)
                GUICtrlSetData($Color1, "0x" & Hex($n, 6))
                GUICtrlSetColor($Color1, _CalcContrastColor(GUICtrlRead($Color1)))

            Case $Color2
                $n = _ChooseColor(2, GUICtrlRead($Color2), 2, $Form1)
                If $n = -1 Then ContinueLoop
                GUICtrlSetBkColor($Color2, $n)
                GUICtrlSetData($Color2, "0x" & Hex($n, 6))
                GUICtrlSetColor($Color2, _CalcContrastColor(GUICtrlRead($Color2)))

            Case $Button1
                $aColors = CalcRGBAvg(GUICtrlRead($Color1), GUICtrlRead($Color2))
                For $n = 0 To UBound($aColors) -1
                    GUICtrlSetBkColor($aLabels[$n], $aColors[$n])
                    GUICtrlSetData($aLabels[$n], "0x" & Hex($aColors[$n], 6))
                    GUICtrlSetColor($aLabels[$n], _CalcContrastColor($aColors[$n]))
                Next

        EndSwitch
    WEnd
EndFunc   ;==>CalcRGBAvg_Example

Func CalcRGBAvg($iBaseColor1, $iBaseColor2, $iMidPoints = 11)

    $iRed2 = BitAND(BitShift($iBaseColor1, 16), 0xFF)
    $iGreen2 = BitAND(BitShift($iBaseColor1, 8), 0xFF)
    $iBlue2 = BitAND($iBaseColor1, 0xFF)
    $iRed = BitAND(BitShift($iBaseColor2, 16), 0xFF)
    $iGreen = BitAND(BitShift($iBaseColor2, 8), 0xFF)
    $iBlue = BitAND($iBaseColor2, 0xFF)

    Local $aColors[$iMidPoints]
    $iMidPoints -= 1

    Local $iRedDiff = Abs($iRed - $iRed2)
    Local $bRedRev = ($iRedDiff = $iRed - $iRed2)
    Local $iGreenDiff = Abs($iGreen - $iGreen2)
    Local $bGreenRev = ($iGreenDiff = $iGreen - $iGreen2)
    Local $iBlueDiff = Abs($iBlue - $iBlue2)
    Local $bBlueRev = ($iBlueDiff = $iBlue - $iBlue2)
    For $i = 0 To $iMidPoints
        $aColors[$i] = "0x" & _
                Hex(Ceiling(Abs(($bRedRev ? 0 : -255) + $iRedDiff / $iMidPoints * $i)), 2) & _
                Hex(Ceiling(Abs(($bGreenRev ? 0 : -255) + $iGreenDiff / $iMidPoints * $i)), 2) & _
                Hex(Ceiling(Abs(($bBlueRev ? 0 : -255) + $iBlueDiff / $iMidPoints * $i)), 2)
    Next
    Return $aColors
EndFunc   ;==>CalcRGBAvg

Func _CalcContrastColor($crBg) ; https://www.autoitscript.com/forum/topic/97345-contrast-color/?do=findComment&comment=700911
   ; http://www.codeproject.com/KB/tips/JbColorContrast.aspx
    Local Const $TOLERANCE = 30
    If (Abs(BitAND($crBg  , 0xFF) - 0x80) <= $TOLERANCE And _
        Abs(BitAND(BitShift($crBg , 8) , 0xFF) - 0x80) <= $TOLERANCE And _
        Abs(BitAND(BitShift($crBg , 16),  0xFF) - 0x80) <= $TOLERANCE) _
    Then Return BitAND((0x7F7F7F + $crBg) , 0xFFFFFF);
    Return BitXOR($crBg ,0xFFFFFF);
EndFunc

Red and blue was good so I thought that was it but, no dice :( 

image.png.69849babff27899a441d5fa783393dda.png

( my screen colors are tweaked with high contrast. That's why the GUI looks like that )

Edited by argumentum

Share this post


Link to post
Share on other sites

I still can't get the hex colors to work, but I got my old function fixed :)

; Gets the difference in two colors, and multiplies the first color by a percent of the difference to get the averages
Func CalcRGBAvg($iRed, $iGreen, $iBlue, $iRed2, $iGreen2, $iBlue2, $iMidPoints = 9)
    $iMidPoints += 1
    Local $aColors[0]
    Local $iRedDiff = $iRed - $iRed2
    If $iRed > $iRed2 Then $iRedDiff *= -1
    Local $iGreenDiff = $iGreen - $iGreen2
    If $iGreen > $iGreen2 Then $iGreenDiff *= -1
    Local $iBlueDiff = $iBlue - $iBlue2
    If $iBlue > $iBlue2 Then $iBlueDiff *= -1

    For $i = 0 To $iMidPoints
        _ArrayAdd($aColors, "RGB(" & _
            $iRed + $iRedDiff/$iMidPoints * $i & ", " & _
            $iGreen + $iGreenDiff/$iMidPoints * $i & ", " & _
            $iBlue + $iBlueDiff/$iMidPoints * $i & ")")
    Next
    Return $aColors
EndFunc   ;==>CalcRGBAvg

TLDR: I forgot to add the original color's value in before, so it was just a percent of the difference


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites
11 minutes ago, seadoggie01 said:

got my old function fixed

Spoiler
#include <Misc.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

CalcRGBAvg_Example()
Func CalcRGBAvg_Example()
    Local $aColors, $n, $nMsg, $aLabels[11]
    Local $Form1 = GUICreate(Chr(160) & "CalcRGBAvg Example", 615, 436, 192, 114)
    Local $Button1 = GUICtrlCreateButton("Blend", 128, 24, 75, 25)
    Local $Color1 = GUICtrlCreateLabel("0xFF7979", 16, 8, 76, 25, BitOR($SS_CENTER, $SS_CENTERIMAGE), $WS_EX_STATICEDGE)
    GUICtrlSetBkColor($Color1, GUICtrlRead($Color1))
    GUICtrlSetColor($Color1, _CalcContrastColor(GUICtrlRead($Color1)))
    GUICtrlSetTip($Color1, "Click to seleect color")
    Local $Color2 = GUICtrlCreateLabel("0x9D0000", 17, 41, 76, 25, BitOR($SS_CENTER, $SS_CENTERIMAGE), $WS_EX_STATICEDGE)
    GUICtrlSetBkColor($Color2, GUICtrlRead($Color2))
    GUICtrlSetColor($Color2, _CalcContrastColor(GUICtrlRead($Color2)))
    For $n = 0 To 10
        $aLabels[$n] = GUICtrlCreateLabel("Label" & $n, 18, 81 + (25 * $n), 76, 25, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    Next

    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
            Case $Color1
                $n = _ChooseColor(2, GUICtrlRead($Color1), 2, $Form1)
                If $n = -1 Then ContinueLoop
                GUICtrlSetBkColor($Color1, $n)
                GUICtrlSetData($Color1, "0x" & Hex($n, 6))
                GUICtrlSetColor($Color1, _CalcContrastColor(GUICtrlRead($Color1)))

            Case $Color2
                $n = _ChooseColor(2, GUICtrlRead($Color2), 2, $Form1)
                If $n = -1 Then ContinueLoop
                GUICtrlSetBkColor($Color2, $n)
                GUICtrlSetData($Color2, "0x" & Hex($n, 6))
                GUICtrlSetColor($Color2, _CalcContrastColor(GUICtrlRead($Color2)))

            Case $Button1
                $aColors = CalcRGBAvg(GUICtrlRead($Color1), GUICtrlRead($Color2))
                For $n = 0 To UBound($aColors) -1
                    GUICtrlSetBkColor($aLabels[$n], $aColors[$n])
                    GUICtrlSetData($aLabels[$n], "0x" & Hex($aColors[$n], 6))
                    GUICtrlSetColor($aLabels[$n], _CalcContrastColor($aColors[$n]))
                Next

        EndSwitch
    WEnd
EndFunc   ;==>CalcRGBAvg_Example

; Gets the difference in two colors, and multiplies the first color by a percent of the difference to get the averages
;~ Func CalcRGBAvg($iRed, $iGreen, $iBlue, $iRed2, $iGreen2, $iBlue2, $iMidPoints = 9)
Func CalcRGBAvg($iBaseColor1, $iBaseColor2, $iMidPoints = 11)

    ;;; to get each color from hex
    Local $iRed2 = BitAND(BitShift($iBaseColor1, 16), 0xFF)
    Local $iGreen2 = BitAND(BitShift($iBaseColor1, 8), 0xFF)
    Local $iBlue2 = BitAND($iBaseColor1, 0xFF)
    Local $iRed = BitAND(BitShift($iBaseColor2, 16), 0xFF)
    Local $iGreen = BitAND(BitShift($iBaseColor2, 8), 0xFF)
    Local $iBlue = BitAND($iBaseColor2, 0xFF)
    ;;; to get each color from hex

    ;;; to fix the extra entry
    Local $aColors[$iMidPoints]
    $iMidPoints -= 1
    ;;; to fix the extra entry


    Local $iRedDiff = $iRed - $iRed2
    If $iRed > $iRed2 Then $iRedDiff *= -1
    Local $iGreenDiff = $iGreen - $iGreen2
    If $iGreen > $iGreen2 Then $iGreenDiff *= -1
    Local $iBlueDiff = $iBlue - $iBlue2
    If $iBlue > $iBlue2 Then $iBlueDiff *= -1

    For $i = 0 To $iMidPoints
;~         _ArrayAdd($aColors, "RGB(" & _
;~             $iRed + $iRedDiff/$iMidPoints * $i & ", " & _
;~             $iGreen + $iGreenDiff/$iMidPoints * $i & ", " & _
;~             $iBlue + $iBlueDiff/$iMidPoints * $i & ")")

        $aColors[$i] = "0x" & _
         Hex(Ceiling(   $iRed + $iRedDiff/$iMidPoints * $i    ), 2) & _
         Hex(Ceiling( $iGreen + $iGreenDiff/$iMidPoints * $i  ), 2) & _
         Hex(Ceiling(  $iBlue + $iBlueDiff/$iMidPoints * $i   ), 2)

    Next
    Return $aColors
EndFunc   ;==>CalcRGBAvg

Func _CalcContrastColor($crBg) ; https://www.autoitscript.com/forum/topic/97345-contrast-color/?do=findComment&comment=700911
   ; http://www.codeproject.com/KB/tips/JbColorContrast.aspx
    Local Const $TOLERANCE = 30
    If (Abs(BitAND($crBg  , 0xFF) - 0x80) <= $TOLERANCE And _
        Abs(BitAND(BitShift($crBg , 8) , 0xFF) - 0x80) <= $TOLERANCE And _
        Abs(BitAND(BitShift($crBg , 16),  0xFF) - 0x80) <= $TOLERANCE) _
    Then Return BitAND((0x7F7F7F + $crBg) , 0xFFFFFF);
    Return BitXOR($crBg ,0xFFFFFF);
EndFunc

 

still not doing it :( 

Share this post


Link to post
Share on other sites

ok, solved the gradient code:
image.png.8547e9881c96056930a04dae6ed49899.png 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Color.au3>
#include <Misc.au3>

CalcRGBAvg_Example()
Func CalcRGBAvg_Example()
    Local $aColors, $n, $nMsg, $aLabels[11]
    Local $Form1 = GUICreate(Chr(160) & "CalcRGBAvg Example", 370, 370, -1, -1)
    Local $Button1 = GUICtrlCreateButton("Blend", 128, 24, 75, 25)
    Local $Color1 = GUICtrlCreateLabel("0xFFDDDD", 16, 8, 76, 25, BitOR($SS_CENTER, $SS_CENTERIMAGE), $WS_EX_STATICEDGE)
    GUICtrlSetBkColor($Color1, GUICtrlRead($Color1))
    GUICtrlSetColor($Color1, _LabelSetBlackOrWhiteTextColor(GUICtrlRead($Color1)))
    GUICtrlSetTip($Color1, "Click to seleect color")
    Local $Color2 = GUICtrlCreateLabel("0xFF3333", 17, 41, 76, 25, BitOR($SS_CENTER, $SS_CENTERIMAGE), $WS_EX_STATICEDGE)
    GUICtrlSetBkColor($Color2, GUICtrlRead($Color2))
    GUICtrlSetColor($Color2, _LabelSetBlackOrWhiteTextColor(GUICtrlRead($Color2)))
    For $n = 0 To 10
        $aLabels[$n] = GUICtrlCreateLabel("Label" & $n, 18, 81 + (25 * $n), 76, 25, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    Next

    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
            Case $Color1
                $n = _ChooseColor(2, GUICtrlRead($Color1), 2, $Form1)
                If $n = -1 Then ContinueLoop
                GUICtrlSetBkColor($Color1, $n)
                GUICtrlSetData($Color1, "0x" & Hex($n, 6))
                GUICtrlSetColor($Color1, _LabelSetBlackOrWhiteTextColor(GUICtrlRead($Color1)))

            Case $Color2
                $n = _ChooseColor(2, GUICtrlRead($Color2), 2, $Form1)
                If $n = -1 Then ContinueLoop
                GUICtrlSetBkColor($Color2, $n)
                GUICtrlSetData($Color2, "0x" & Hex($n, 6))
                GUICtrlSetColor($Color2, _LabelSetBlackOrWhiteTextColor(GUICtrlRead($Color2)))

            Case $Button1
                $aColors = CalcRGBAvg(GUICtrlRead($Color1), GUICtrlRead($Color2))
                For $n = 0 To UBound($aColors) - 1
                    GUICtrlSetBkColor($aLabels[$n], $aColors[$n])
                    GUICtrlSetData($aLabels[$n], "0x" & Hex($aColors[$n], 6))
                    GUICtrlSetColor($aLabels[$n], _LabelSetBlackOrWhiteTextColor($aColors[$n]))
                Next

        EndSwitch
    WEnd
EndFunc   ;==>CalcRGBAvg_Example

Func CalcRGBAvg($Color1 = "0xFF0000", $Color2 = "0xFFFF00", $Step = 11)
    Local $aColor[$Step] ; based on https://www.autoitscript.com/forum/topic/56365-gradient-color-box-thing/
    $Step -= 1
    Local $ColorR = _ColorGetRed($Color1)
    Local $ColorG = _ColorGetGreen($Color1)
    Local $ColorB = _ColorGetBlue($Color1)
    Local $StepR = (_ColorGetRed($Color2) - $ColorR) / $Step
    Local $StepG = (_ColorGetGreen($Color2) - $ColorG) / $Step
    Local $StepB = (_ColorGetBlue($Color2) - $ColorB) / $Step
    For $i = 0 To $Step
        $sColor = "0x" & StringFormat("%02X%02X%02X", $ColorR + $StepR * $i, $ColorG + $StepG * $i, $ColorB + $StepB * $i)
        $aColor[$i] = $sColor
    Next
    Return $aColor
EndFunc   ;==>CalcRGBAvg

Func _ColorGetBrightness($Color)
    Return Round((299 * BitAND(BitShift($Color, 16), 0xFF) + 587 * BitAND(BitShift($Color, 8), 0xFF) + 114 * BitAND($Color, 0xFF)) / 1000)
EndFunc   ;==>_ColorGetBrightness

Func _LabelSetBlackOrWhiteTextColor($Color)
    Local $iBrightness1 = _ColorGetBrightness($Color)
    Local $iBrightness2 = _ColorGetBrightness(0xFFFFFF)
    Local $iBrightnessDiff1 = Abs($iBrightness1 - $iBrightness2)
    $iBrightness2 = _ColorGetBrightness(0x000000)
    Local $iBrightnessDiff2 = Abs($iBrightness1 - $iBrightness2)
    If $iBrightnessDiff1 >= $iBrightnessDiff2 Then
        Return 0xFFFFFF ; '0x' & Hex ( 0xFFFFFF, 6 )
    Else
        Return 0x000000 ; '0x' & Hex ( 0x000000, 6 )
    EndIf
EndFunc   ;==>_LabelSetBlackOrWhiteTextColor

 

Edited by argumentum

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...