jeudi 27 octobre 2016

How to output a random color from a set of selected colors in Java? (Android)

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.




Aucun commentaire:

Enregistrer un commentaire