9

I have a colour #6A8F6509

I wish to remove the Alpha part of that colour and be left with only RGB components (i.e. #RRGGBB).

The resulting colour must look identical to the initial one without the transparency.

How do I go about this on Android's Java?

Update: The initial background is always white (#FFF)

3
  • Unfortunately the resultant colour will depend on what's behind the transparent element, so this may require some manual tweaking.
    – fredley
    Oct 12, 2011 at 13:53
  • You say you want it to look identical, however the color it appears depends on what is behind it, do you want it to keep what it was getting from the background?
    – dten
    Oct 12, 2011 at 13:55
  • The inital background colour is always set to white. The resulting colour needs to to look identical to the initial setting without any transparency. Oct 12, 2011 at 14:16

7 Answers 7

32
int newColor = oldColor | 0xFF000000;
1
13

If you want to do it with the code you can try following code:

    static int stripAlpha(int color){
    return Color.rgb(Color.red(color), Color.green(color), Color.blue(color));
}
6

For each color: C' = C(a/255) + 255(1-(a/255))

So for #6A8F6509:

R' = 143(106/255) + 255(1-(106/255) = (approx) 208

G' = 101(106/255) + 255(1-(106/255) = (approx) 191

B' = 9(106/255) + 255(1-(106/255) = (approx) 153

So your answer should be: #D0BF99, if my math is correct. This only applies to an all white background as well - for a non-white background, the second addend should have the appropriate corresponding color value of the background instead of 255.

-- EDIT --

Looking at the formula again, I'm not entirely sure whether the formula gets applied to each color or the entire color (a quick visual test should be able to tell you - I'm guessing per color). I should point out that this is the formula direct from the Alpha Compositing wiki page:

http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending

3
1

try

#FF8F6509

the first two digits indicate the alpha value of any color.

1

Calculate the way a semi-transparent color will look when placed on top of another color.

Kotlin color extension:

@ColorInt
fun @receiver:ColorInt Int.removeAlpha(@ColorInt underlyingColor: Int): Int {
    return ColorUtils.blendARGB(this, underlyingColor, alpha / 255F)
}

Usage:

Color.parseColor("#6A8F6509").removeAlpha(underlyingColor = Color.WHITE)
2
  • i got error alpha not found?
    – famfamfam
    Oct 12, 2022 at 4:44
  • alpha is a field of ColorInt for the above helper function. It's found in androidx.core.graphics so make sure you import it.
    – Lifes
    Oct 12, 2022 at 7:42
0

Jetpack Compose version is

Color(0x6A8F6509).compositeOver(Color.White)
-1

I'll admit to not knowing Android's Java, but can't you just change the color from #6A8F6509 to #FF8F6509? The in most modern programming languages, color is typically encoded as ##AARRGGBB.

AA = Hex of the Alpha, ranging from 0 (hex 00), full transparent, to 255 (hex FF), no transparency.

RR = Hex of the red part of the color. Similar to above, 0 is no red, 255 is full red.

GG = Hex of green.

BB = Hex of blue.

-Edit-

Based on your update, you can do this to get the color:

For each of the red, green, and blue: get the difference between the transparent color and the background color and multiply that by the alpha value. Divide that by 255 (hex FF, if you prefer). Finally, add that number to the transparent color's value.

In your case:

int transparentR = 0x8F;
int transparentG = 0x65;
int transparentB = 0x09;
int backgroundR = 0xFF;
int backgroundG = 0xFF;
int backgroundB = 0xFF;
int alpha = 0x6A;

int newR = transparentR + ( ( (backgroundR - transparentR) * alpha ) / 255 );
int newG = transparentR + ( ( (backgroundG - transparentG) * alpha ) / 255 );
int newB = transparentR + ( ( (backgroundB - transparentB) * alpha ) / 255 );

-Edit, again-

Ok, I've redone the formula based on LJ2's answer using Wikipedia's Alpha Blending function. and had a colleague confirm this with Paint.Net. This is the version where the background Color is fully opaque, and it's in C#, sorry:

 int transparentColor2(int intFgColor, int intBgColor, int intAlpha)
 {
double dAlpha = (Convert.ToDouble(intAlpha)/255.0);
double dFirst = Convert.ToDouble(intFgColor) * dAlpha;
double dSecond = Convert.ToDouble(intBgColor) * (1 - dAlpha);
return Convert.ToInt32(dFirst + dSecond);
 }

do this for each R, G, and B value, and you should get the right answer. Incase Android's Java has different, double could be Double/Single/Float, and int could be Int32/Int16/Int64/Byte. I don't remember how to Convert between the two in Java, unfortunately.

2
  • This does not work, the final colour differs completely form the original. Oct 13, 2011 at 13:12
  • What color are you getting with my method? I'm getting BDA56F.
    – Tory
    Oct 13, 2011 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.