How to output a random color from a set of selecte

2019-05-04 18:50发布

So I want a string to be given a random color any time the user inputs an answer. My issue is that I'm not sure how to make that random color of a string be a color of a specific range. For example, if I wanted the string to be randomly become blue, red, green, pink, white, or brown. Only these colors, none other.

So far I have managed a completely random color out of all possible RBG variations using the following code:

Random rand = new Random();
            int r = rand.nextInt(254)+1;
            int g = rand.nextInt(254)+1;
            int b = rand.nextInt(254)+1;

            int randomColor = Color.rgb(r,g,b);
            word.setTextColor(randomColor);

Though as previously mentioned, this is not what I aim to achieve. Instead of this, I want set colors that can be randomly applied to the string. These are colors that I would pick, then have randomly set as the string color. This sets a completely random color out of a range I do not intend to have. I could end up with 5 variations of blue for example.

If anyone could put forward a solution, I'd appreciate it. Thank you.

4条回答
贼婆χ
2楼-- · 2019-05-04 19:12

Side note:

rand.nextInt(254)+1; 

generates a number between 1 and 255, the color range however is between 0 and 255, so

rand.nextInt(256); 

should really include all possibilities.

Now to your question

If you only want to variate between a certain number of colors, dont't randomize over all 16 million possibilities, but predefine a set of nice colors.

Either you create an array with predefined colors and just randomly pick one, or you generate the colors but limit the outcome:

    int r = 0;
    int g = 0;
    int b = 0;
    switch(rand.nextInt(8)){    // switch over 8 possible colors
        case (0):   // red
            r = 205;
            break;
        case (1):   // green
            g = 205;
            break;
        case (2):   // blue
            b = 205;
            break;
        case (3):   // pink
            r = 255;
            g = 20;
            b = 147;
            break;
        case (4):   // yellow
            r = 200;
            g = 200;
            break;
        case (5):   // magenta
            r = 200;
            b = 200;
            break;
        case (6):   // orange
            r = 255;
            g = 165;
            break;
        case (7):   // purple
            r = 128;
            b = 128;
            break;
    }

That is of course a "large" solution, but would avoid creating an array that resides in your memory.

查看更多
贼婆χ
3楼-- · 2019-05-04 19:15

First of all in color.xml define your colors and create array of it.

<?xml version="1.0" encoding="utf-8"?>
<resources>

<item name="blue" type="color">#FF33B5E5</item>
<item name="purple" type="color">#FFAA66CC</item>
<item name="green" type="color">#FF99CC00</item>
<item name="orange" type="color">#FFFFBB33</item>
<item name="red" type="color">#FFFF4444</item>
<item name="darkblue" type="color">#FF0099CC</item>
<item name="darkpurple" type="color">#FF9933CC</item>
<item name="darkgreen" type="color">#FF669900</item>
<item name="darkorange" type="color">#FFFF8800</item>
<item name="darkred" type="color">#FFCC0000</item>

<integer-array name="androidcolors">
    <item>@color/blue</item>
    <item>@color/purple</item>
    <item>@color/green</item>
    <item>@color/orange</item>
    <item>@color/red</item>
    <item>@color/darkblue</item>
    <item>@color/darkpurple</item>
    <item>@color/darkgreen</item>
    <item>@color/darkorange</item>
    <item>@color/darkred</item>
</integer-array>

</resources>

Now generate random color like below in onCreate method.

int[] androidColors = getResources().getIntArray(R.array.androidcolors);
int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];

Lastly set this generated color.

view.setBackgroundColor(randomAndroidColor);  

Source taken from here.

查看更多
Lonely孤独者°
4楼-- · 2019-05-04 19:20

An easy way to do this would be to define a LIST of colors. Then use a random to to select one of the colors on your action.

Create List of Desired Colors

This is not an extensive list but includes 4 colors to start, RED, Blue, Green and Pink.

List<Integer> colors = new ArrayList<>();
colors.add(Color.rgb(235,22,220));
colors.add(Color.BLUE);
colors.add(Color.GREEN);
colors.add(Color.RED);

Create a simple method to return a random color

Now we just need a method to return a random color from our list. It is a whopping 2 lines long.

private int randomColor(){
    Random rand = new Random();
    return colors.get(rand.nextInt(colors.size()));
}

Change textview color

Now we can just change the color. An example below is in a button onClickListener. You can also add in some validation here to make sure you don't get a duplicate color, say blue twice in a row.

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int color = randomColor();
        if(txtView.getCurrentTextColor() != color){
             txtView.setTextColor(color);
        }else{
             txtView.setTextColor(randomColor());
        }
    }
});

Easy, clean and quick to write.

查看更多
对你真心纯属浪费
5楼-- · 2019-05-04 19:27

Work with a Color Selector tool to have some idea how RGB values work. For example, if you keep R=255 and then set G=B=Some same number, you will get some shade of Red. Same for Green and Blue. If however you keep the other two same and change the remaining value only, it will again return some lighter or darker version of Red, Green or Blue (depending on whether that remaining value is R,G or B). This shorcut works reasonably well for each color. So, for example, find a Purple. Then you will have to change two of the RGB values identically to get different-looking Purples.

Example: R=255, G=65, B=255 is a purple.

R=190, G=65, B=190 is also purple.

R=190, 6=95, B=190 is ALSO purple.

However, there is a range. I mean, values cannot always be 0-255. To find that range, you can simply try several values for finding what the minimum is and what the maximum is.

Mind, this is only a shortcut but I think it will be useful enough for you.

Edit: For a more acceptable solution, try learning about how hex colors work. Then you can define ranges for certain color types. Start here: http://blog.digitaltutors.com/understanding-hexadecimal-colors-simple/

查看更多
登录 后发表回答