Kennt ihr sicher: Hintergrundfarbe für ein Ctrl gesetzt und schon ist der Text nicht mehr vernünftig lesbar.
Ich habe nun mal mit Komplementärfarben und (fixen) Kontrastfarben gespielt - s. Bsp.
Für die Komplementärfarben hatte ich mich am Farbkreis von Itten orientiert. Wenn die Farben großflächig nebeneinander dargestellt sind ist das brauchbar, aber bei Text auf farbigem Grund nicht wirklich immer.
Somit errechne ich jetzt die Helligkeit der zu setzenden Hintergrundfarbe und wähle dann die festgelegte Textfarbe für hellen/dunklen Hintergrund.
In der Standardeinstellung ist das ein tiefes Blau und ein Fast-Weiß. Das ist aber direkt im Funktionsaufruf anpassbar.
EDIT: Ich habe mal die Helligkeitsschwelle auf 140 gesetzt. Dann wird Rot auch als dunkle Farbe betrachtet - ist dann besser lesbar.
; #FUNCTION# =======================================================================================
; Name ..........: _GuiCtrl_SetColorContrast
; Description ...: Sets the background color for a control and depending on its brightness the
; ...............: appropriate contrast color for the text.
; Parameter(s)...: $_ID The control ID
; ...............: $_BkCol The background color
; ....[optional].: $_ColOnDark The text color if BG is dark (Default: -1, almost white)
; ....[optional].: $_colOnBright The text color if BG is bright (Default: -1, very deep blue)
; Return values .: Success 1
; ...............: Failure 0
; Author ........: BugFix
; ==================================================================================================
Func _GuiCtrl_SetColorContrast($_ID, $_BkCol, $_ColOnDark=-1, $_colOnBright=-1)
$_ColOnDark = $_ColOnDark = -1 ? 0xFAFAFA : $_ColOnDark
$_colOnBright = $_colOnBright = -1 ? 0x000088 : $_colOnBright
GUICtrlSetBkColor($_ID, $_BkCol)
; get contrast
Local $R = BitAND(BitShift($_BkCol, 16), 0xFF)
Local $G = BitAND(BitShift($_BkCol, 8), 0xFF)
Local $B = BitAND($_BkCol, 0xFF)
Local $Brightness = Sqrt($R^2*0.299 + $G^2*0.587 + $B^2*0.114)
;~ Local $iCol = $Brightness < 128 ? $_ColOnDark : $_colOnBright
Local $iCol = $Brightness < 140 ? $_ColOnDark : $_colOnBright ; Rot wird dann auch als dunkel betrachtet
Return GUICtrlSetColor($_ID, $iCol)
EndFunc
Alles anzeigen
;-- TIME_STAMP 2022-07-11 12:27:02
Global $aBGcol[] = [0x110000,0x440000,0x770000,0xA10000,0xFF0000, _
0x001100,0x004400,0x007700,0x00A100,0x00FF00, _
0x000011,0x000044,0x000077,0x0000A1,0x0000FF, _
0xF5F5DC,0x7FFF00,0xDC143C,0x00FFFF,0xFFD700, _
0xADFF2F,0xF0FFF0,0x4B0082,0xE6E6FA,0xFFFACD]
GUICreate('Test: Komplementär vs. Kontrast', 360, 650)
Global $x = 10, $y = 10
For $i = 0 To UBound($aBGcol) -1
GUICtrlCreateLabel('0x' & Hex($aBGcol[$i], 6), $x, $y, 80)
$x += 90
GUICtrlCreateLabel(' << COMPLEMENT >>', $x, $y, 120, 17)
GUICtrlSetBkColor(-1, $aBGcol[$i])
GUICtrlSetColor(-1, _RGB_GetComplement($aBGcol[$i]))
$x += 130
GUICtrlCreateLabel(' << CONTRAST >>', $x, $y, 120, 17)
_GuiCtrl_SetColorContrast(-1, $aBGcol[$i])
$x = 10
$y += 25
Next
GUISetState()
Do
Until GUIGetMsg() = -3
Func _RGB_GetComplement($_RGB)
; Farbkreis von Itten
; additive Farbmischung: Komplementärfarben löschen sich gegenseitig aus
Local $cR = 0xFF - BitAND(BitShift($_RGB, 16), 0xFF)
Local $cG = 0xFF - BitAND(BitShift($_RGB, 16), 0xFF)
Local $cB = 0xFF - BitAND($_RGB, 0xFF)
Return '0x' & Hex($cR, 2) & Hex($cG, 2) & Hex($cB, 2)
EndFunc
; #FUNCTION# =======================================================================================
; Name ..........: _GuiCtrl_SetColorContrast
; Description ...: Sets the background color for a control and depending on its brightness the
; ...............: appropriate contrast color for the text.
; Parameter(s)...: $_ID The control ID
; ...............: $_BkCol The background color
; ....[optional].: $_ColOnDark The text color if BG is dark (Default: -1, almost white)
; ....[optional].: $_colOnBright The text color if BG is bright (Default: -1, very deep blue)
; Return values .: Success 1
; ...............: Failure 0
; Author ........: BugFix
; ==================================================================================================
Func _GuiCtrl_SetColorContrast($_ID, $_BkCol, $_ColOnDark=-1, $_colOnBright=-1)
$_ColOnDark = $_ColOnDark = -1 ? 0xFAFAFA : $_ColOnDark
$_colOnBright = $_colOnBright = -1 ? 0x000088 : $_colOnBright
GUICtrlSetBkColor($_ID, $_BkCol)
; get contrast
Local $R = BitAND(BitShift($_BkCol, 16), 0xFF)
Local $G = BitAND(BitShift($_BkCol, 8), 0xFF)
Local $B = BitAND($_BkCol, 0xFF)
Local $Brightness = Sqrt($R^2*0.299 + $G^2*0.587 + $B^2*0.114)
;~ Local $iCol = $Brightness < 128 ? $_ColOnDark : $_colOnBright
Local $iCol = $Brightness < 140 ? $_ColOnDark : $_colOnBright ; Rot wird dann auch als dunkel betrachtet
Return GUICtrlSetColor($_ID, $iCol)
EndFunc
#cs in Funktion integriert
Func _RGB_GetContrast($_RGB, $_ColOnDark=0xFAFAFA, $_colOnBright=0x000088)
; get brightness
Local $R = BitAND(BitShift($_RGB, 16), 0xFF)
Local $G = BitAND(BitShift($_RGB, 8), 0xFF)
Local $B = BitAND($_RGB, 0xFF)
Local $Brightness = Sqrt($R^2*0.299 + $G^2*0.587 + $B^2*0.114)
;~ If $Brightness < 128 Then ; dark
If $Brightness < 140 Then ; dark
Return $_ColOnDark ; contrast text color if background is dark
Else
Return $_colOnBright ; contrast text color if background is bright
EndIf
EndFunc
#ce
Alles anzeigen