Jump to content

Color contrast issue


nend
 Share

Recommended Posts

I have a program which has a progress bar, the for ground has a random color (this color is made out an average color of a random picture).
Before the progress bar change checks if it has enough contrast color with the background color, if not change the brightness with plus or minus 10% in color. (not changing the color but make it brighter or darker).
Whatever color is used it always has to have enough contrasted color with the background color.
I don’t have a clue how to make that, can you help me?

Link to comment
Share on other sites

Here's a snippet I use for contrast calculation:

Func _CalcContrastColor($crBg)
    ; 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   ;==>_CalcContrastColor

 

Link to comment
Share on other sites

@KaFu If I'm right this wil give the contrasted color but that is not what I'm looking for.
I only want to adjust the brightness when the for and background color looks the same or near the same so that there is more contrast when needed.

I'm allready have a UDF to adjust the brightness but now how to detect when needed.
 

Func _AlterBrightness($StartCol, $adjust, $Select = 7)
    Local $Red = String($adjust * (BitAND(1, $Select) <> 0) + BitAND($StartCol, 0xFF0000) / 0x10000)
    Local $grn = String($adjust * (BitAND(2, $Select) <> 0) + BitAND($StartCol, 0x00FF00) / 0x100)
    Local $blu = String($adjust * (BitAND(4, $Select) <> 0) + BitAND($StartCol, 0x0000FF))

    Return Hex(_AlterBrightness_Limit_Column($Red), 2) & Hex(_AlterBrightness_Limit_Column($grn), 2) & Hex(_AlterBrightness_Limit_Column($blu), 2)
EndFunc   ;==>_AlterBrightness

Func _AlterBrightness_Limit_Column($cc)
    If $cc > 255 Then Return 255
    If $cc < 0 Then Return 0
    Return $cc
EndFunc   ;==>_AlterBrightness_Limit_Column

 

Edited by nend
Link to comment
Share on other sites

Split both colors into R G B, compare the components separately against a total threshold (e.g. threshold 10, test and tweak to find a good value) and alter brightness if sum of distances is too low? Here in pseudo-code:

Color 1: 0x121212

Color 2: 0x141414

if abs(12-14) + abs(12-14) + abs(12-14) < 10 then adjust brightness

Link to comment
Share on other sites

Here another way 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 = 0xFFFF00
Local $c2 = 0x0000FF

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, 300, 100, 30, $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

High contrast is 7 or +

Link to comment
Share on other sites

You could also convert your RGB values to/from HSL.  HSL will let you deal with your colors in terms of Hue, Saturation, and Lightness.  There are functions in the WinApiGdi UDF that will do this for you:

_WinAPI_ColorAdjustLuma()   Changes the luminance of a RGB value
_WinAPI_ColorHLSToRGB()     Converts colors from hue-luminance-saturation (HLS) to RGB format
_WinAPI_ColorRGBToHLS()     Converts colors from RGB to hue-luminance-saturation (HLS) format

Check out the help file's example for _WinAPI_ColorAdjustLuma to see how easy it is to adjust just the brightness/lightness/lumiance of a given color as a percentage or an absolute value.  The other 2 functions will let you easily switch between RGB & HSL, when/if necessary.  The RGB to HSL function will allow you to compare the lightness values of 2 colors to give you an idea of whether you may want to adjust your color.

Edited by TheXman
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...