0
votes

How do I create a color int from alpha, red, green, blue values (all from 0-255)?

I need this color int to set a view's background color.

I tried this:

    protected int colorStringToColor(String colorString){ // whereas colorString is i.e. "214+13+22+255" or "214+13+22+85"
        String[] comps = colorString.split("\\+");
        int myColor = 0;
        if(comps.length == 3){
            int a = 255;
            int r = Integer.parseInt(comps[0]);
            int g = Integer.parseInt(comps[1]);
            int b = Integer.parseInt(comps[2]);
            myColor = Color.argb(a, r, g, b);
        } else if (comps.length == 4){
            int a = Integer.parseInt(comps[3]);
            int r = Integer.parseInt(comps[0]);
            int g = Integer.parseInt(comps[1]);
            int b = Integer.parseInt(comps[2]);
            myColor = Color.argb(a, r, g, b);
        }
        return myColor;
    }

However, when I use the result for setting a views background color, both example colorString are of the same red???

Thanks a lot.

6
What it your expected output and what did you get?Rajat Mehra
have you checked your index value? you can reverse index value to get proper color valuePrachi Singh

6 Answers

3
votes

Your code is perfect there is no issue.

One thing is that, if you send this "214+13+22+85" value as integer then you will get the result value as the sum of these values. so may you have done mistake there.

import android.graphics.Color;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            textView = findViewById(R.id.txt);
            textView.setTextColor(colorStringToColor("214+13+22+235")); 
        }

        public int colorStringToColor(String colorString){ // whereas colorString is i.e. "214+13+22+255" or "214+13+22+85"
            String[] comps = colorString.split("\\+");
            int myColor = 0;
            if(comps.length == 3){
                int a = 255;
                int r = Integer.parseInt(comps[0]);
                int g = Integer.parseInt(comps[1]);
                int b = Integer.parseInt(comps[2]);
                myColor = Color.argb(a, r, g, b);
            } else if (comps.length == 4){
                int a = Integer.parseInt(comps[3]);
                int r = Integer.parseInt(comps[0]);
                int g = Integer.parseInt(comps[1]);
                int b = Integer.parseInt(comps[2]);
                myColor = Color.argb(a, r, g, b);
            }
            return myColor;
        }

I Implement your code in my project, and I checked it by well. What I saw there! It's working.

2
votes

You can convert it by using the argb(int red, int green, int blue) method from Color class like this :

int convertedColor= Color.argb(red, green, blue);

and set it into your view like this:

yourView.setBackgroundColor(convertedColor);
1
votes
 Color opaqueRed = Color.valueOf(0xffff0000); // from a color int
 Color translucentRed = Color.valueOf(1.0f, 0.0f, 0.0f, 0.5f);
0
votes
int nonTransparentRed = Color.argb(255, 255, 0, 0);
0
votes

From your question, it seems you're not annotating the return value of the function by @ColorInt.

Change your function to

@ColorInt
private int colorStringToColor(){...};

Also, in your function, annotate

@ColorInt int myColor = 0;

Android has android.graphics.Color class, which provides methods to operate on Colors.

To get a ColorInt from ARGB values, you can use Color.argb(int alpha, int red, int green, int blue); method, which will return you corresponding ColorInt value.

You can find more about Color class from here

android.graphics.Color

0
votes

I'm going to go ahead an agree with Prince here, your code is fine. But, also offer a theory. You are setting the view color and it's stripping the transparency. Check your numbers are they different? They should be. When you feed them into setBackgroundColor they are do the same thing. If I recall correctly it doesn't actually set the alpha. Try stripping off the alpha and using in view.setAlpha(color>>24) after you set the color. Is that functionally different?

Prince is feeding it into setTextColor rather than setBackgroundColor.

view.setBackgroundColor(color);
view.setAlpha(color>>24);