2

For example, if the background was white, the text color would be black. If the BG was black, the text would be white. Blue BG, yellow text, etc. Update:

// method in MyActivity class
void changeBackgroundColor(int newColor) {
    activityLayout.setBackgroundColor(newColor);
    int invertingColor = ColorInvertor.invert(newColor);
    someTextView.setTextColor(invertingColor);
}

If I call activity.changeBackgroundColor(Color.WHITE), then someTextView has to change the text color to black, i.e. ColorInvertor.invert(Color.WHITE) == Color.BLACK, and ColorInvertor.invert(Color.BLACK) == Color.WHITE etc.

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94

3 Answers3

6

Get the rgb values of your color and substract them from 255 :

yourColor = Color.rgb(0,130,20);

invertedRed = 255 - 0;
invertedGreen = 255 - 130;
invertedBlue = 255 - 20;

text.setTextColor(Color.rgb(invertedRed,invertedGreen,invertedBlue));

If you want to use hex values see How to get RGB value from hexadecimal color code in java

Community
  • 1
  • 1
mirenibg
  • 96
  • 1
  • 2
0

It is possible just by using simple conditions:

1.Get the color

2.Check the condition

3.Set the color

To get the color:

TextView tv1;
tv1=(TextView)findViewById(R.id.tv1);
ColorDrawable tv1color = (ColorDrawable) tv1.getBackground();

If you're on Android 3.0+, you can get out the resource ID of the color:

int tv1colorId = tv1color.getColor();

To set the Color:

TextView tv2;
tv2=(TextView)findViewById(R.id.tv2);
tv2.setBackgroundColor(0xFF00FF00);

Then put a condition as you want:

if (tv1colorID == R.color.green) {
   tv2.setBackgroundColor(color.WHITE); // As your choice color
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

You can inverse a color by using those function...

//color
public static String getHex(int intColor) {
    String hexColor = String.format("#%06X", (0xFFFFFF & intColor));
    return hexColor;
}

public static String getRGBStringfromHex(String hexColor) {
    int color = Color.parseColor(hexColor);
    int red = Color.red(color);
    int green = Color.green(color);
    int blue = Color.blue(color);
    return red + "," + green + "," + blue;
}

public static int[] getRGBStringtoRGBInt(String splits[]) {
    int list[] = new int[3];
    list[0]=Integer.parseInt(splits[0]);
    list[1]=Integer.parseInt(splits[1]);
    list[2]=Integer.parseInt(splits[2]);
    return list;
}

public static int getRGBColor(int rgb[]) {
    int color = Color.rgb(rgb[0],rgb[1],rgb[2]);
    return color;
}

public static String invertedRGBfromRGB(int rgb[]) {
    int invertedRed = 255 - rgb[0];
    int invertedGreen = 255 - rgb[1];
    int invertedBlue = 255 - rgb[2];
    return invertedRed + "," + invertedGreen + "," + invertedBlue;
}

public static int getColorFromRGBString(String color) {
    ArrayList<Integer> list = new ArrayList<>();
    String splits[] = color.split(Pattern.quote(","));
    list.add(Integer.parseInt(splits[0]));
    list.add(Integer.parseInt(splits[1]));
    list.add(Integer.parseInt(splits[2]));
    return Color.rgb(list.get(0), list.get(1), list.get(2));
}

Ok now if you convert a int color...

String rgb[]=getRGBStringfromHex(getHex(colorThatWanttoInvert)).split(",");
int desireinvertedcolor=getColorFromRGBString(invertedRGBfromRGB(getRGBStringtoRGBInt(rgb)));

Hope this will help.