hier eine contrast-ermittlungs-prog mit demo.
wie Raven vorschlug:
> Aber theoretisch könnte man doch die Werte die sehr nahe an der mitte sind in ein "Feld" packen, und sobald die Gewählte Farbe sich innerhalb dieses Felde befindet eine standard Kontrastfarbe nehmen, bzw. irgendeinen bestimmten Wert zu der errechneten Kontrastfarbe dazu addieren.
wenn der wert eines farbkanals zu nahe an der mitte liegt, wird als
kontrastwert nicht das komplement genommen, sondern einfach Null.
das für jeden kanal einzeln, d.h. nur die farben, beidenen alle farbkanäle zwischen 96 und 160 liegen, bekommen einen schwarzen hintergrund.
Code: Alles auswählen
; ersetzt Abs(), da das nur mit Floats funktioniert
; es tut nix anderes als den absoluten wert zu ermitteln...
Macro AbsEx(Expression)
( ( ((Expression) >= 0 Or 0) *2 - 1 ) * (Expression) )
EndMacro
Procedure.l Contrast( Color.l )
bred = Color & $FF
bgrn = (Color >> 8 ) & $FF
bblu = (Color >>16 ) & $FF
; Rot-Kontrast
If AbsEx(128 - bred) < 32
cred = 0
Else
cred = 255-bred
EndIf
; Grün-Kontrast
If AbsEx(128 - bgrn) < 32
cgrn = 0
Else
cgrn = 255-bgrn
EndIf
; Blau-Kontrast
If AbsEx(128 - bblu) < 32
cblu = 0
Else
cblu = 255-bblu
EndIf
CColor = cred + ( cgrn << 8 ) + ( cblu << 16 )
ProcedureReturn CColor
EndProcedure
OpenWindow(0,0,0,800,600,"BackGroundTest")
StartDrawing(WindowOutput(0))
For n=0 To 19
For t=0 To 14
ForeCol = Random($FFFFFF)
BackCol = Contrast(ForeCol)
PX = 40*n : PY = 40*t
Box(PX,PY,40,40,BackCol)
Circle(PX+ 8, PY+ 8, 4, ForeCol)
Circle(PX+32, PY+ 8, 4, ForeCol)
Circle(PX+ 8, PY+32, 4, ForeCol)
Circle(PX+32, PY+32, 4, ForeCol)
LineXY(PX+ 8, PY+ 8,PX+32, PY+32,ForeCol)
LineXY(PX+ 8, PY+32,PX+32, PY+ 8,ForeCol)
Next
Next
StopDrawing()
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Code: Alles auswählen
If AbsEx(128 - bred) < 32
Code: Alles auswählen
If 128 - bred < 32 And 128 - bred > -32
etwas einfacher:
es wird die relative helligkeit der vordergrundfarbe ermittelt.
liegt diese unter 96, wird der hintergrund weiß, sonst schwarz.
die genaue mitte ist nicht nötig, 96 ist für bevorzugt dunklen hintergrund.
für bevorzugt hellen hintergrund wäre es z.b. 160,
für neutral wäre es 128.
auch als ergebnis kann man was anderes als schwarz und weiß wählen.
Code: Alles auswählen
Procedure EasyContrast( Color.l )
bred = Color & $FF
bgrn = (Color >> 8 ) & $FF
bblu = (Color >>16 ) & $FF
LumaCol = bred * 0.299 + bgrn * 0.587 + bblu * 0.114
If LumaCol > 96
ProcedureReturn 0 ; schwarz
Else
ProcedureReturn $FFFFFF ; weiß
EndIf
EndProcedure