0

Every color has a perfect contrast color black=white, yellow=blue,purple=green, etc.. So how can I find a perfect contrast color for any give rgb value? Is there a formula or something I can use?

user2741831
  • 2,120
  • 2
  • 22
  • 43
  • 1
    Are you sure every colour has a universally agreed unique "perfect contrast colour"? According to what source? If you find a source for that claim, likely that source also contains the formula. – Andreas Rejbrand May 13 '19 at 06:36
  • 2
    The idea of text in green on purple (or purple on green) is making my eyes water. – High Performance Mark May 13 '19 at 07:02
  • 1
    Is the opposite of grey grey? – Nick is tired May 13 '19 at 07:10
  • Good question I hadn't though of that. – user2741831 May 13 '19 at 07:40
  • 1
    this might be helpful https://www.w3.org/TR/WCAG20-TECHS/G17.html – eparvan May 13 '19 at 08:07
  • Related: https://stackoverflow.com/questions/946544/good-text-foreground-color-for-a-given-background-color, https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color, https://stackoverflow.com/questions/3116260/given-a-background-color-how-to-get-a-foreground-color-that-makes-it-readable-o, https://stackoverflow.com/questions/2241447/make-foregroundcolor-black-or-white-depending-on-background, https://ux.stackexchange.com/questions/107318/formula-for-color-contrast-between-text-and-background, and many, many more. – Andreas Rejbrand May 13 '19 at 09:00
  • The question of contrasting text and background colors, especially for readability purposes, is a question being debated right now. For example, according to [this issue on the WCAG issues page](https://github.com/w3c/wcag/issues/695), "color contrast" is only one part of the readability equation and other factors such as font size and font weight also come into play. – Peter O. May 13 '19 at 10:25

1 Answers1

-1

Inverting all bits in RGBA will get you an opposite color

oppositeColor = ~color & 0xffffff;

 Color     Opposite
#ff0000   #00ffff
#00ff00   #ff00ff
#0000ff   #ffff00
eparvan
  • 1,639
  • 1
  • 15
  • 26
  • 2
    Is #7f7f7f contrast enough to #808080? – MBo May 13 '19 at 07:28
  • it seems the farther the color is from gray (#FFFFFF / 2) the better contrast will turn out after inverting all bits – eparvan May 13 '19 at 08:05
  • Although this answer makes sense technically, I wouldn't say this is a good method to find a text colour given a background colour. (And that Q has been asked many times at SO and its sister sites.) – Andreas Rejbrand May 13 '19 at 08:55