Android Question Generate Random Text Colors For a White Background

Mahares

Expert
Licensed User
Longtime User
I would like to generate the best readable multi DARK text colors on a WHITE background using the following:
B4X:
Dim y(6) As Int =ArrayAsInt(0,256,0,128,0,256)
Colors.RGB(Rnd(y(0),y(1)),Rnd(y(2),y(3)), Rnd(y(4),y(5)) )

It is acceptable with the above array of numbers, but not the best text colors. What do you think the elements of the array of integers should be changed to so I always get the best contrast.

Thank you
 

DPaul

Active Member
Licensed User
Longtime User
Hi,

1) I think you should change 256 into 255, there are only 255 shades plus zero. (total = 256)
2) If your BG is always white, you may want to make sure that 2 of the 3 are well below, say 128...

Alternatively, you may want to make a vector with 10, 20, 30 ... nice predefined colors, and choose from those.
There are zillions of RGB lists on the net.

Dpaul
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
1) I think you should change 256 into 255, there are only 255 shades plus zero. (total = 256)
1. The reason I have 256 is because the Random function (Rnd) always excludes the 2nd number. If I make it 255, it will exclude 255, which is not something we want to do.
2. Setting 2 out of the 3 color elements to less than 128 will work, but you will miss out on many dark colors in green, blue or red depending which 2 were singled out.
3. I thought perhaps there is a formula I can plug in which gives me a broader range of deep dark text colors and yet maintain a white background..
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Put these RGB values in an array and select one at random.
Your answer is very good and identical to @DPaul 's bottom half of Post #2. That is acceptable, except that using an array of 10, 20 or 30 colors, you will miss out on a multitude of other desirable ones. That is why I am applying the Rnd function on the R, G and B.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
My two cents

What we all know:
  • White color is 255,255,255.
  • Any color with "high" RGB values will be more similar to white, that's clear. So it makes sense to limit them somehow. The key here is how to limit them.
A weighted approach:
  • Not all the components (R,G,B) play the same 'role' in human lightness/luminance perception. That's why luminance, in different models is calculated as a weighted sum of all the components. The assigned weights vary between models, but most of them give a much higher weight to the green component.
  • As a very rough approach, you could start using 0.25,0.5,0.25 (as said, green is the one that most contributes to luminance).
  • Then, you could limit the total lightness to, for instance, 180

So the constraint would be , 0.25*R + 0.5*G + 0.25*B <= 180 '<-- or any other

Then a random 'dark' color picker would be like (not tested)
B4X:
Sub GenerateDarkEnoughColor( maxAllowedLuminance as Int) as int
  Dim R,G,B, luminance as int
  Do until luminance<=maxAllowedLuminance)
    R = rnd( 0, 256)
    G = rnd( 0, 256)
    B = rnd( 0, 256)
   luminance = 0.25*R + 0.5*G + 0.25*B   
  Loop
  return Colors.RGB(R,G,B)
End Sub

'call the function with this
myColor = GenerateDarkEnoughColor(180)

You can play with the weights and the max allowed value
 
Upvote 0
Top