Android Question Negative Text Label

Alessandro71

Well-Known Member
Licensed User
Longtime User
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.
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
This example is static: my background is dynamic and text may even be half over a color and half over another.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
So,
B4X:
Background.Color = NewColor
Label1.TextColor = ContrastColor(NewColor)
is not possible in your situation?

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.
 
Last edited:
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
here is a bad mockup
the yellow background can change in color, and the level can wave just like water
 

Attachments

  • mockup.png
    mockup.png
    1.9 KB · Views: 135
Upvote 0

emexes

Expert
Licensed User
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
 
Upvote 0

udg

Expert
Licensed User
Longtime User
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.
 
Upvote 0

emexes

Expert
Licensed User
Just occurred to me that if you'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
 
Upvote 0
Top