41

I'm working on a simple bar graph application that uses a static array of colors for divvying out bar colors. I would like the functionality to either draw bars normally, or slightly transparent.

Is there a way to programmatically adjust a color integer so that it's slightly transparent? Or will I have to statically define a transparent version of each color and then switch to using these versions whenever I want transparency?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
TrolliOlli
  • 909
  • 2
  • 10
  • 18
  • Have a look at setAlpha http://developer.android.com/reference/android/view/View.html#setAlpha(float) – Ken Wolf Jun 21 '13 at 14:24

8 Answers8

131

If you are using support library, you can use:

ColorUtils.setAlphaComponent(int color, int alpha);

If you are not using support library, one-line solution taken from it's source code is:

int res = (color & 0x00ffffff) | (alpha << 24);
Ashu
  • 2,970
  • 1
  • 19
  • 31
Anton Ryabyh
  • 1,621
  • 2
  • 12
  • 9
  • 1
    This should be the correct answer. It's simpler and setAlphaComponent works very well as long as the alpha int is between 0 and 250. – I'm_With_Stupid Oct 11 '15 at 19:23
  • 4
    I'm_With_Stupid between 0 and 255 to be more precise – Vitaly Zinchenko Oct 29 '16 at 11:35
  • ColorUtils.setAlphaComponent was added to the API (22.1) ~2 years after this question was posted. The whole ColorUtils class has a whole bunch of neat things in it. Anyone stumbling on this thread now would be well served to review it (https://developer.android.com/reference/android/support/v4/graphics/ColorUtils.html) – spartygw Jun 12 '17 at 18:55
  • This is the only solution answer! – Vitaly Jun 23 '21 at 05:25
16

Sure...Look at Color and there's a function:

static int   argb(int alpha, int red, int green, int blue)

Return a color-int from alpha, red, green, blue components.

So your RGB values could be static and you just bump the alpha value to get a new transparent version of the color.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
spartygw
  • 3,289
  • 2
  • 27
  • 51
  • 2
    Thanks a ton! For those of you wondering how to get the RGB values from an integer, take a look here: http://stackoverflow.com/questions/5526690/convert-a-raw-negative-rgb-int-value-back-to-a-3-number-rgb-value – TrolliOlli Jun 21 '13 at 14:45
7

Hi there you could use the:

android.support.v4.graphics.ColorUtils#setAlphaComponent

note: the alpha here is from 0 to 255 and not % based.

There are also other util methods such contract and luminosity calculations in there.

Regards

Joao Gavazzi
  • 1,818
  • 18
  • 8
4

Try following code

int color = (int)Long.parseLong(your_color, 16);
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = (color >> 0) & 0xFF;

if color code has alpha then

int alpha= (color >> 24) & 0xFF;
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
3

From the top answer I created a method to do this:

private Android.Graphics.Color AddTransparencyToColour(Android.Graphics.Color color, int transparancy)
{
    return Android.Graphics.Color.Argb(transparancy, color.R, color.G, color.B);
}

It's also worth noteing that this can be changed to an extension method like so

public static ColorExtensions
{
    public static Android.Graphics.Color AddTransparency(this Android.Graphics.Color color, int transparancy)
    {
        return Android.Graphics.Color.Argb(transparancy, color.R, color.G, color.B);
    }
}

In regards to the alpha value, from MSDN Color.FromArgb:

Remarks

To create an opaque color, set alpha to 255. To create a semitransparent color, set alpha to any value from 1 through 254.

JKennedy
  • 18,150
  • 17
  • 114
  • 198
2

I use extension functions.

fun Int.withAlpha(@IntRange(from = 0, to = 255) alpha: Int): Int {
    return (alpha shl 24) or (this and 0xFFFFFF)
}

Also possible with ColorUtils

ColorUtils.setAlphaComponent(color, alpha)
UdaraWanasinghe
  • 2,622
  • 2
  • 21
  • 27
1

You could create a colour helper which returns same colour with applied alpha. - Lets say you want to change visibility from 0.0 to 1.0 (double)

 val originalColour: Int = primaryColor
 val generatedColor = ColorUtil.generateTransparentColor(originalColour, 0.5)
 view.setBackgroundColor(generatedColor)

Create a colour generator helper

object ColorUtil {
  fun generateTransparentColor(color: Int, alpha: Double?): Int {
    val defaultAlpha = 255 // (0 - Invisible / 255 - Max visibility)
    val colorAlpha = alpha?.times(defaultAlpha)?.roundToInt() ?: defaultAlpha
    return ColorUtils.setAlphaComponent(color, colorAlpha)
  }
}
Jonas Simonaitis
  • 170
  • 1
  • 13
0

Those in the compose world using androidx.compose.ui.graphics.Color can just use the copy-method:

val slightlyTransparentRed = Color.Red.copy(alpha = 0.9f)
m.reiter
  • 1,796
  • 2
  • 11
  • 31