android - Converting ARGB to RGB in java -
i'm trying algorithms write method remove alpha value color , give identical rgb values seems test failing. belive called alpha blending? i'm not sure. algorithm use converting.
public static int removealpha(int foreground, int background) { int redforeground = color.red(foreground); int redbackground = color.red(background); int greenforeground = color.green(foreground); int greenbackground = color.green(background); int blueforeground = color.blue(foreground); int bluebackground = color.blue(background); int alphaforeground = color.alpha(foreground); int rednew = (redforeground * alphaforeground) + (redbackground * (1 - alphaforeground)); int greennew = (greenforeground * alphaforeground) + (greenbackground * (1 - alphaforeground)); int bluenew = (blueforeground * alphaforeground) + (bluebackground * (1 - alphaforeground)); return color.rgb(rednew, greennew, bluenew); }
and test this
@test public void removealpha() { int red = color.red; assert.assertequals(0xffff7f7f, heatmap.removealpha(red, 0xffffffff)); } junit.framework.assertionfailederror: expected :-32897 actual :-258
when draw red in photoshop , set opacity 50%, gives me 255,127,127 rgb seems identical 50% opaque pure red. think there algorithm false. helps appreciated.
edit: here mock color:
powermockito.mockstatic(color.class); powermockito.when(color.rgb(mockito.anyint(), mockito.anyint(), mockito.anyint())).thenanswer(new answer<object>() { @override public object answer(invocationonmock invocation) throws throwable { int red = (int) invocation.getarguments()[0]; int green = (int) invocation.getarguments()[1]; int blue = (int) invocation.getarguments()[2]; return (0xff << 24) | (red << 16) | (green << 8) | blue; } }); powermockito.when(color.alpha(mockito.anyint())).thenanswer(new answer<object>() { @override public object answer(invocationonmock invocation) throws throwable { return ((int)invocation.getarguments()[0])>>>24; } }); powermockito.when(color.red(mockito.anyint())).thenanswer(new answer<object>() { @override public object answer(invocationonmock invocation) throws throwable { return (((int)invocation.getarguments()[0])>>16) & 0xff; } }); powermockito.when(color.green(mockito.anyint())).thenanswer(new answer<object>() { @override public object answer(invocationonmock invocation) throws throwable { return (((int)invocation.getarguments()[0])>>8) & 0xff; } }); powermockito.when(color.blue(mockito.anyint())).thenanswer(new answer<object>() { @override public object answer(invocationonmock invocation) throws throwable { return (int)invocation.getarguments()[0] & 0xff; } });
note: i'm not java, can wrong. i'm using common programming notions, tweakings can needed.
i think messing data types... getting integer representation of color, i.e. 0-255, , multiplying if 0-1 representation. try this:
double alphaforeground = ((double)color.alpha(foreground)) / 255.0; int rednew = ((int)round((redforeground * alphaforeground) + (redbackground * (1 - alphaforeground)))); int greennew = ((int)round((greenforeground * alphaforeground) + (greenbackground * (1 - alphaforeground)))); int bluenew = ((int)round((blueforeground * alphaforeground) + (bluebackground * (1 - alphaforeground))));
there can rounding issues, but... should work.
just remark: color.red
has 255 alpha channel. means removealpha(red, 0xffffffff)
returns red itself, not 0xffff7f7f. in order value should write
int red = color.red; red.alpha = 0x80;
(or close value)
Comments
Post a Comment