# How to generate random RGB values through which black text is reliably visible?

**URL:** <https://discourse.julialang.org/t/how-to-generate-random-rgb-values-through-which-black-text-is-reliably-visible/92148>\
**Category:** Visualization\
**Created:** [December 26, 2022, 3:39pm UTC](https://discourse.julialang.org/t/how-to-generate-random-rgb-values-through-which-black-text-is-reliably-visible/92148 "2022-12-26T15:39:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin\_Beckingham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colin_beckingham/32/4878_2.png) [@Colin\_Beckingham](https://discourse.julialang.org/u/Colin_Beckingham)\
**Post date:** [December 26, 2022, 3:39pm UTC](https://discourse.julialang.org/t/how-to-generate-random-rgb-values-through-which-black-text-is-reliably-visible/92148/1 "2022-12-26T15:39:56Z")

</div>

I’d like to constrain the randomly coloured nodes in my `graphplot` to light colours through which black font text is reliably visible. The functions in `Random` do not operate well with `RGB` since math is not permitted. How to approach this issue?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 26, 2022, 4:13pm UTC](https://discourse.julialang.org/t/how-to-generate-random-rgb-values-through-which-black-text-is-reliably-visible/92148/2 "2022-12-26T16:13:25Z")

</div>

Do you actually want _random_ colors? For graphics, it’s often more important that your colors are distinguishable from one another. Colors.jl offers `distinguishable_colors` for that purpose: [Colormaps and Colorscales · Colors](https://juliagraphics.github.io/Colors.jl/dev/colormapsandcolorscales/#Generating-distinguishable-colors)

---

<div class="post-metadata">

**Author:** ![Colin\_Beckingham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colin_beckingham/32/4878_2.png) [@Colin\_Beckingham](https://discourse.julialang.org/u/Colin_Beckingham)\
**Post date:** [December 26, 2022, 5:45pm UTC](https://discourse.julialang.org/t/how-to-generate-random-rgb-values-through-which-black-text-is-reliably-visible/92148/3 "2022-12-26T17:45:42Z")

</div>

Well, I guess I was looking for some approach that is a bit more intuitive than the distinguishable colors route. Yes, laziness on my part. Generating some random numbers in a specific range is quickly checkable, which is not as easy via the distinguishable route.

However your suggestion encouraged me to go back to distinguishable colours again with renewed vigor, and I now, after some effort seem to have fallen into a set of values that almost does what I am looking for. I wish I could say it was solid reasoning and understanding that led me to this point, but my current settings are giving me node colours that permit text to show fairly clearly.

One oddity that results from my muddled appreciation of what the automated routine is trying to do is that one of the nodes appears completely white on every graphplot I produce. If there are no edges attached to that node or text to signify its presence the viewer would not know it was there on a white background which is what is used on a browser viewing of a png. A rand() + RGB() approach would enable me to quickly eliminate that possibility. Perhaps if it is possible to put a coloured ring around all the nodes then white might be acceptable but that is only half a solution.

---

<div class="post-metadata">

**Author:** ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Post date:** [December 26, 2022, 7:02pm UTC](https://discourse.julialang.org/t/how-to-generate-random-rgb-values-through-which-black-text-is-reliably-visible/92148/4 "2022-12-26T19:02:45Z")

</div>

RGB is a poor color model for this. You’ll find it easier to pick values with the HSL color model, then converting back to RGB.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [December 26, 2022, 8:40pm UTC](https://discourse.julialang.org/t/how-to-generate-random-rgb-values-through-which-black-text-is-reliably-visible/92148/5 "2022-12-26T20:40:10Z")

</div>

Perhaps try the `dropseed=true` option?
