nend Posted January 4, 2021 Share Posted January 4, 2021 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 More sharing options...
KaFu Posted January 4, 2021 Share Posted January 4, 2021 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 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
nend Posted January 4, 2021 Author Share Posted January 4, 2021 (edited) @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 January 4, 2021 by nend Link to comment Share on other sites More sharing options...
KaFu Posted January 4, 2021 Share Posted January 4, 2021 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 nend 1 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
nend Posted January 4, 2021 Author Share Posted January 4, 2021 @KaFu Thanks thats a good start, thanks for your help! Link to comment Share on other sites More sharing options...
KaFu Posted January 4, 2021 Share Posted January 4, 2021 Here's a different calculation method for brightness that sounds reasonable too: Sqrt( .241 * $R ^ 2 + .691 * $G ^ 2 + .068 * $B ^ 2 ) / 2.55 therks 1 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Nine Posted January 4, 2021 Share Posted January 4, 2021 Here another way based on W3C : expandcollapse popup#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 + “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
TheXman Posted January 4, 2021 Share Posted January 4, 2021 (edited) 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 January 4, 2021 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now