<p>I read this in a magazine article probably 25 years ago. I don't recall the full derivation, but they said to take each component (red, green, and blue) and invert the high bit of the color. In a C-like programming language, you could do:</p>

<pre><code> component = (~component &amp; 0x80) | (component &amp; 0x7F);
</code></pre>

<p>This can be simplified to:</p>

<pre><code>component ^= 0x80;
</code></pre>

<p>for  a single component or to:</p>

<pre><code>component ^= 0x80808000;
</code></pre>

<p>if you want to do 4 components at once (and don't affect the alpha).</p>

<p>This assumes your components are 8-bit values. This avoids the problems you get with the middle values if you simply invert them. In that case 127 becomes 128, and vice-versa. But with the above solution, 127 becomes 255 and 128 becomes 0.</p>

CC BY-SA 4.0
2019-02-04T15:34:12.840Z
This is a great way, but as i get closer to 127 in R or G or B values, lower is the difference between colors
Strader
CC BY-SA 4.0
2019-02-05T06:47:24.717Z
@Strader I don't understand what mean. The difference is a constant 128.
user1118321
CC BY-SA 4.0
2019-02-05T11:30:39.950Z
"component = 0x80 ^ component;" should do the trick more succinctly, with a trivial extension to all three components in parallel via 0x808080.
Simon F
CC BY-SA 4.0
2019-02-05T15:56:23.053Z
Found it to be better way to manually lower the alpha of background color for better contrast and calculate inverted one using full alpha
Strader
CC BY-SA 4.0
2019-02-03T01:54:46.523Z
2019-02-07T04:16:37.750Z
2019-02-07T04:16:37.750Z
user1118321
<p>I am looking for a stable way to find a contrast/inverted/opposite color to a given one.</p>

<p>Application of this is to find best possible color for foreground to give background.</p>

CC BY-SA 4.0
2019-02-02T23:24:05.097Z
You could try the complementary colors. So if you have $c \in [0,1]^3$ then $(1,1,1) - c$. You can use luma to control luminance.
lightxbulb
<color>
Getting contrast color to a given color in RGB or ARGB or HEX
Strader
# Getting contrast color to a given color in RGB or ARGB or HEX

(Asked by: username_0 on 2019-02-01)

I am looking for a stable way to find a contrast/inverted/opposite color to a given one.

Application of this is to find best possible color for foreground to give background.

**Tags:** color

> **Comments:**
> **username_1**: You could try the complementary colors. So if you have $c \in [0,1]^3$ then $(1,1,1) - c$. You can use luma to control luminance.  

---

## 1 answers

### Answer by username_2 on 2019-02-03. Score: 4 | Accepted answer

I read this in a magazine article probably 25 years ago. I don't recall the full derivation, but they said to take each component (red, green, and blue) and invert the high bit of the color. In a C-like programming language, you could do:

```
 component = (~component & 0x80) | (component & 0x7F);

```

This can be simplified to:

```
component ^= 0x80;

```

for a single component or to:

```
component ^= 0x80808000;

```

if you want to do 4 components at once (and don't affect the alpha).

This assumes your components are 8-bit values. This avoids the problems you get with the middle values if you simply invert them. In that case 127 becomes 128, and vice-versa. But with the above solution, 127 becomes 255 and 128 becomes 0.

> **Comments:**
> **username_0**: This is a great way, but as i get closer to 127 in R or G or B values, lower is the difference between colors  
> **username_2**: @username_0 I don't understand what mean. The difference is a constant 128.  
> **username_3**: "component = 0x80 ^ component;" should do the trick more succinctly, with a trivial extension to all three components in parallel via 0x808080.  
> **username_0**: Found it to be better way to manually lower the alpha of background color for better contrast and calculate inverted one using full alpha  

---

