Color.st
changeset 7639 78da2ec0985d
parent 7620 3b59f3c6764f
child 7642 9285a6619b7c
--- a/Color.st	Fri Oct 28 21:23:45 2016 +0200
+++ b/Color.st	Sun Oct 30 14:33:44 2016 +0100
@@ -5090,9 +5090,55 @@
 !
 
 contrastingBlackOrWhite
-    "answer either black or white, whichever gives a better contrast"
-
-    ^ self brightness < 0.60 ifTrue:[self class white] ifFalse:[self class black]
+    "answer either black or white, whichever gives a better contrast
+     for drawing text on a background with my color.
+     (i.e. if I am dark, return white; if I am bright, return black"
+
+    ^ self brightness < 0.55 
+        ifTrue:[self class white] 
+        ifFalse:[self class black]
+
+    "
+     (Color blue) contrastingBlackOrWhite
+     (Color red) contrastingBlackOrWhite
+     (Color green) contrastingBlackOrWhite
+     (Color yellow) contrastingBlackOrWhite
+    "
+!
+
+contrastingColorFor:aBackgroundColor
+    "answer a slightly brightened or darkened variant of myself,
+     to ensure a good contrast when showing text on a background color.
+     i.e. when drawing read on grey, it might be better to darken or brighten 
+     the red, if its brightness is too near to the grey's brightness.
+     Use this for alert strings shown on a color background."
+
+    |colorUsed bgBrightness|
+
+    colorUsed := self.
+    bgBrightness := aBackgroundColor brightness.
+    
+    (bgBrightness dist:colorUsed brightness) < 0.5 ifTrue:[
+        bgBrightness > 0.5 ifTrue:[
+            colorUsed := self slightlyDarkened.
+            (bgBrightness dist:colorUsed brightness) < 0.5 ifTrue:[
+                colorUsed := self darkened.
+            ].
+        ] ifFalse:[
+            colorUsed := self slightlyLightened.
+            (bgBrightness dist:colorUsed brightness) < 0.5 ifTrue:[
+                colorUsed := self lightened.
+            ].
+        ].    
+    ].
+    ^ colorUsed.
+
+    "
+     (Color blue) contrastingColorFor:Color white.
+     (Color blue) contrastingColorFor:Color blue.
+     (Color red) contrastingColorFor:Color grey
+     (Color blue) contrastingColorFor:Color black
+    "
 !
 
 darkened