Jump to content

How to filter out the color matching with obvious contrast?[Solved]


Recommended Posts

#include <Color.au3>

Local $aColor = [ _
        ["0x858585", "0x7A7A7A"], _
        ["0x848484", "0x7B7B7B"], _
        ["0x8470FF", "0x7B8F00"], _
        ["0x838B8B", "0x7C7474"], _
        ["0x838B83", "0x7C747C"], _
        ["0x836FFF", "0x7C9000"], _
        ["0x828282", "0x7D7D7D"], _
        ["0x7FFFD4", "0x80002B"], _
        ["0x7FFF00", "0x8000FF"]]

Local $hGUI = GUICreate("Color", 300, 560)

For $i = 0 To 8
    ;If _lumin($aColor[$i][0], $aColor[$i][1]) > 100 And _light($aColor[$i][0], $aColor[$i][1]) > 100 Then
        GUICtrlCreateLabel($aColor[$i][0], 10, 10 + (25 * $i), 200, 20, 0x0200 + 0x01)
        GUICtrlSetColor(-1, $aColor[$i][0])
        GUICtrlSetBkColor(-1, $aColor[$i][1])
    ;EndIf
Next

For $i = 0 To 8
    ;If _lumin($aColor[$i][0], $aColor[$i][1]) > 125 And _light($aColor[$i][0], $aColor[$i][1]) > 125 Then
        GUICtrlCreateLabel($aColor[$i][1], 10, 30 + (25 * ($i + 9)), 200, 20, 0x0200 + 0x01)
        GUICtrlSetColor(-1, $aColor[$i][1])
        GUICtrlSetBkColor(-1, $aColor[$i][0])
    ;EndIf
Next

GUISetState(@SW_SHOW, $hGUI)

While 1
    Switch GUIGetMsg()
        Case -3
            ExitLoop
    EndSwitch
WEnd

Func _lumin($hexColora, $hexColorb)
    Local $Difa = (_ColorGetRed($hexColora) * 299 + _ColorGetGreen($hexColora) * 587 + _ColorGetBlue($hexColora) * 114) / 1000
    Local $Difb = (_ColorGetRed($hexColorb) * 299 + _ColorGetGreen($hexColorb) * 587 + _ColorGetBlue($hexColorb) * 114) / 1000
    Return Abs($Difb - $Difa)
EndFunc   ;==>_lumin

Func _light($hexColora, $hexColorb)
    Local $dRed = Abs(_ColorGetRed($hexColora) - _ColorGetRed($hexColorb))
    Local $dGrn = Abs(_ColorGetGreen($hexColora) - _ColorGetGreen($hexColorb))
    Local $dBlu = Abs(_ColorGetBlue($hexColora) - _ColorGetBlue($hexColorb))
    Return ($dRed + $dGrn + $dBlu)
EndFunc   ;==>_light

The codes As shown in the above. The Result display As follows:

20210805170623.png.3a019a311a0506ff3dd6131b81760ad3.png

How to screen out the more obvious color matching through contrast and brightness?

Specifically, how much is the contrast and brightness difference set to?

Edited by Letraindusoir
Link to comment
Share on other sites

Not sure what you are asking help for, but this may help you.  It calculates the contrast between 2 colors, and if it larger than 7:1, it is considered as a high contrast. (based on W3C)

#include <GUIConstants.au3>
#include <Constants.au3>
#include <Color.au3>

; see https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure

Local $c1 = 0xFFFF80
Local $c2 = 0x000000

GUICreate("Calculate color Contrast", 550, 400)

GUICtrlCreateLabel("", 50, 50, 200, 200)
GUICtrlSetBkColor(-1, $c1)

GUICtrlCreateLabel("", 300, 50, 200, 200)
GUICtrlSetBkColor(-1, $c2)

Local $iContrast = ColorContrast($c1, $c2)
GUICtrlCreateLabel(Round($iContrast, 3), 225, 280, 100, 30, $SS_CENTERIMAGE+$SS_CENTER)
GUICtrlSetFont(-1, 20)
GUICtrlCreateLabel(($iContrast >= 7 ? "High" : "Low") & " Contrast", 10, 320, 530, 35, $SS_CENTERIMAGE+$SS_CENTER)
GUICtrlSetFont(-1, 24)
GUISetState()

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
  EndSwitch
WEnd

Func ColorContrast($iC1, $iC2)
  Local $iL1 = RelativeLuminescence($iC1)
  Local $iL2 = RelativeLuminescence($iC2)
  Return $iL1 > $iL2 ? ($iL1+0.05)/($iL2+0.05) : ($iL2+0.05)/($iL1+0.05)
EndFunc

Func RelativeLuminescence($iColor)
  Local $aCol = _ColorGetRGB($iColor)
  Local $R = $aCol[0]/255, $G = $aCol[1]/255, $B = $aCol[2]/255
  $R = $R <= 0.03928 ? $R /12.92 : (($R + 0.055)/1.055) ^ 2.4
  $G = $G <= 0.03928 ? $G /12.92 : (($G + 0.055)/1.055) ^ 2.4
  $B = $B <= 0.03928 ? $B /12.92 : (($B + 0.055)/1.055) ^ 2.4
  Return 0.2126 * $R + 0.7152 * $G + 0.0722 * $B
EndFunc

 

Edited by Nine
Link to comment
Share on other sites

1 hour ago, Nine said:

Not sure what you are asking help for, but this may help you.  It calculates the contrast between 2 colors, and if it larger than 7:1, it is considered as a high contrast. (based on W3C)

That is exactly the answer I want.

Thanks for the help!

Link to comment
Share on other sites

  • Letraindusoir changed the title to How to filter out the color matching with obvious contrast?[Solved]

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...