0

Has anyone seen anything like this?

I'm drawing some rectangles on the canvas with the following code.

int col = pad.getColor();
paint.setColor(col);
Log.d("XXX","col " + Color.red(col) + "," + Color.green(col) + "," + Color.blue(col) + "  " + Color.alpha(col) );
canvas.drawRoundRect(pad.getFloatRect(), 20, 20, paint);

In some cases, this is working exactly as expected. I get the rectangle painted with the colour I stored in the pad object.

But in other cases the colour of the rectangle is coming out wrong.

For example, I have one pad whose colour is white. The colour comes from colors.xml where it was specified with

<color name="pad3">#FFFFFF</color>

And when I log the red, green and blue components of col, I get 255, 255, 255 as expected.

BUT the actual rectangle is getting painted in dark grey rather than white.

I'm completely mystified by this.

The only two possibilities I can think of are either:

a) that there's some setting (theme or style) within Android that is somehow overriding certain colours with alternatives. Even if the colours are being set explicitly. Is this possible? And if so, I didn't do this deliberately, but I'm using a project created by Android Studio which created a lot of boilerplate, where should I be looking for these settings to stop it?

b) I notice that it's the darker colours that are coming out OK, and the lightest colours that have this problem. And I wonder if it's because there's some kind of loss of higher-end bits when trying to store Android colors in ints. Although the fact that the red, green and blue components of the white all come out 255 suggests that this isn't the case.

I also considered that it's to do with the alpha channel, but that is 255 in all cases.

So I'm completely stumped. Some colours are fine. Others aren't. And I can't see any difference between the ones that are and the ones that aren't.

interstar
  • 26,048
  • 36
  • 112
  • 180

1 Answers1

0

OK.

I solved my problem with the help of this : https://stackoverflow.com/a/67399219/8482

I think it was basically adding this line

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

to my themes which fixed the problem. It turned off some automatic dark-mode from being forced.

Previously it looked like dark colours were being rendered normally, but light colours were automatically darkened. Now the light colours are back to normal.

Just to note. This is INSANE! How the hell is something like this a default you have to opt out of rather than something you explicitly opt into?

interstar
  • 26,048
  • 36
  • 112
  • 180