Is there an easy way to draw a text label with complementary color?
I have a text over a color changing background, so I need a way to make sure it's always readable.
Or are you saying the color is constantly changing? In that case your only real option is to set the Label1.Color to something not fully transparent thereby making it more readable regardless of background.
If you're looking for some way to draw the label with XOR filter, then perhaps there's a way with JavaObject and Reflection, but I don't think there's a way with the standard label.
1/ how about semi-transparent label background (and perhaps the text too) eg Colors.ARGB(128, HowMuchRed, HowMuchGreen, HowMuchBlue)
1a/ or eg
B4X:
Dim BackgroundColor As Int = Colors.Whatever 'defaults to opaque (solid) (alpha = 255)
Dim TextColor As Int = Colors.Whatever 'ditto
BackgroundColor = Bit.Or(Bit.And(BackGroundColor, 0xFFFFFF), 0x40000000) 'change alpha to 64 (0x40) = mostly transparent
TextColor = Bit.Or(Bit.And(TextColor, 0xFFFFFF), 0xC0000000) 'change alpha to 192 (0xC0) = mostly opaque (solid)
2/ or perhaps a shadow effect, eg, if the label text is white, then put an identical label underneath, but offset a few pixels down and right (or whatever direction you like) and with color black or grey
2b/ or you could likewise do a border, using eight labels underneath (offset in combinations of +/- vertically and +/- horizontally) but that's getting a bit tedious
What abvout HSB/HSV color model? You could use the brightness part to catch whether the background turned darker or lighter and operate accordingly on the foreground text.
Just occurred to me that ifyou're adding the text to an image using Canvas.DrawText then it's pretty easy... instead of
B4X:
DrawText(S, X, Y, Black)
you'd have
B4X:
for XOffset = -1 to 1
for YOffset = -1 to 1
if XOffset <> 0 or YOffset <> 0 then
DrawText(S, X+XOffset, Y+YOffset, LightGray) 'draw gray border
end if
next
next
DrawText(S, X, Y, Black) 'draw text inside border
and again you could experiment with making the border color semi-transparent with Colors.ARGB(A,R,G,B) where A is less than 255