Reverse opposing colors

complementary colors examples
complementary colors list
complementary color of blue
invert rgb color
green complementary color
how to invert colors in java
contrasting colours
complementary color scheme

I have a user setup where they can choose the colors of the alerts. the Alert is the background color on a text or button. But the problem comes in that if they select a dark blue and we have black letters the contrast isnt enough and you cannot read it.

I have tried to make a function to get the reverse opposing color but havent got too far.

Is there such a function?

Use complementary color:

Algo is simple, substract each color component from 255 to get new color components

Color textColor = Color.rgb(255-Color.red(bgColor),
                         255-Color.green(bgColor),
                         255-Color.blue(bgColor));

----- EDIT (As RGB based complement may not work always --------

These two links are very much helpful and on topic:

http://www.splitbrain.org/blog/2008-09/18-calculating_color_contrast_with_php

http://24ways.org/2010/calculating-color-contrast

How to Apply Color Correcting Concealer, creates a vibrant look especially when used at full saturation. Invert the colors of image files, white becomes black, black becomes white, orange becomes blue and so on.

I found that the best solution for me is to convert the RGB values into YIQ values. As we are only interested in the brightness value (represented by Y), there is one single calculation to be done: Y = (299*R + 587*G + 114*B)/1000. The Java code for that would look like this:

public static Color getContrastColor(Color color) {
  double y = (299 * color.getRed() + 587 * color.getGreen() + 114 * color.getBlue()) / 1000;
  return y >= 128 ? Color.black : Color.white;
}

You can see that it simply decides to use black or white, based upon the brightness of the original color. And the result works very nice in my opinion. The weights (299, 587, 114) are proportional to the sensitivity of the eyes (or rather the sensitivity of the retina) to the corresponding color.

Color Harmonies: complementary, analogous, triadic color schemes, each other, such as red and green and red-purple and yellow-green. Opposite Color Tool Two colors are considered complimentary if they produce a neutral color — black, white, or grey — when mixed evenly. But, that is a mouthful.

Based on Marks solution I would suggest:

public static int getComplementaryColor(int colorToInvert) {
    float[] hsv = new float[3];
    Color.RGBToHSV(Color.red(colorToInvert), Color.green(colorToInvert),
            Color.blue(colorToInvert), hsv);
    hsv[0] = (hsv[0] + 180) % 360;
    return Color.HSVToColor(hsv);
}

And additionally I now created a similar method, for calculating a default background for a given color:

public static int getContrastVersionForColor(int color) {
    float[] hsv = new float[3];
    Color.RGBToHSV(Color.red(color), Color.green(color), Color.blue(color),
            hsv);
    if (hsv[2] < 0.5) {
        hsv[2] = 0.7f;
    } else {
        hsv[2] = 0.3f;
    }
    hsv[1] = hsv[1] * 0.2f;
    return Color.HSVToColor(hsv);
}

Basic Color Theory, of webpages, turning white background black and making them much easier on the eyes. Invert image is a free online tool, helps to reverse the colors of image, where red color reversed to cyan, green reversed to magenta and blue reversed to yellow, and vice versa. Upload your image in the tool, click invert button, then preview the inverted image in container.

I got it working, I think :)

Here is the function:

public static int OpposeColor(int ColorToInvert)
{
     int RGBMAX = 255;

     float[] hsv = new float[3];
     float H;

     Log.i("HSV_H", "Start Color=" +  ColorToInvert);

     Color.RGBToHSV( Color.red( ColorToInvert),  RGBMAX - Color.green( ColorToInvert), Color.blue(ColorToInvert), hsv);

     Log.i("HSV_H", "Hue=" + hsv[0]);
     Log.i("HSV_H", "Saturation=" + hsv[1]);
     Log.i("HSV_H", "Value=" + hsv[2]);

    H = (float) (hsv[0] + 0.5);

    if (H > 1) H -= 1;

    Log.i("HSV_H", "Hue2=" + H);         

    Log.i("HSV_H", "Color=" +  Color.HSVToColor(hsv ));

    return Color.HSVToColor(hsv );


}

What is the use of invert color option in mobiles?, The traditional color wheel model dates to the 18th century and is still used by many artists today. This model designates red, yellow and blue as primary colors with the primary–secondary complementary pairs of red–green, blue-orange, and yellow–purple. One more common method used to invert colors on Windows 10, is by changing the DWORD. Method 4 (Using DWORD) Step 1: Take the cursor to the Cortana search box and click on it. Step 2: Type Regedit in the text box and click on the Enter key when the same command is visible in the result. Step 3: Once the UAC dialog asks for permission, click on

integer solution:

public static int getContrastColor(int color) {
        double y = (299 * Color.red(color) + 587 * Color.green(color) + 114 * Color.blue(color)) / 1000;
        return y >= 128 ? Color.BLACK : Color.WHITE;
    }

Complementary colors, Invert a color, black becomes white, white becomes black, blue becomes orange and so on. Original color. less. cancel choose. HEX. RGB. R G B. HSV. H S V. In the Document Colors Options section, check the “Replace Document Colors” box. Then, select the “Use High-Contrast colors” option and select a color scheme from the “High-contrast color combination” drop-down list. Click “OK” to save your changes and close “Preferences” dialog box. The color scheme changes in the document area.

Invert a color online, Complementary colors are opposite to each other on the color wheel, so they create a strong contrast. Monochromatic color schemes can be subtle and  Reverse opposing colors. I have a user setup where they can choose the colors of the alerts. the Alert is the background color on a text or button. But the problem comes in that if they select a dark blue and we have black letters the contrast isnt enough and you cannot read it.

Color Wheel, Opposite Color Tool. Two colors are considered complimentary if they produce a neutral color — black, white, or grey — when mixed evenly. But, that is a  Set the foreground color to white and then press Alt+Delete on the new layer created above the image layer. This will fill the selection with white color. Deselect the selection. Now create a new layer between the the original image and the image with white fill. Select the brown color with eyedropper tool and fill the background with brown color.

Opposite Color Tool, Colors that are opposite each other on the color wheel are considered to be complementary colors (example: red and green). The high contrast of complementary 

Comments
  • There are at least two questions on here for this. One is stackoverflow.com/questions/596216/…. I know because I implemented an algorithm from one of them and it works well.
  • Doesn't this give a big issue when the user selects the color 128/128/128 (or something really near).
  • I have included a link from Wikipedia in the answer. RGB based complement is easiest to understand. HSV based complement will work in this case. @Shynhriir: Yes.. RGB based complement will not work in this range, you are absolutely right
  • How would you code into Android the complementary color from the Wiki link?
  • @Mark: It is not very hard. I have previously wrote my own 3d perspective transformation code just reading the corresponding theories.
  • If you are new to Java and Android it is :) I got it though... see above
  • Thanks. This may not cover all cases, but is pefect for my usecase (make text readable on colored background)
  • You always can read the text pretty good. The disadvantage is, that the text will be black or white, color information of the background is "lost".
  • You getComplementaryColor is working perfectly but not getContrastVersionForColor can you please fix?
  • What do you mean it does not work? You don't like the result? Can you provide a color int value for which it does not look right? I use it quite often and never had problems
  • I like this solution a lot. A perfect alternative if you don't like the black/white YIQ approach.