简体   繁体   中英

Random Color generation with one RGB set to 255

I want to get a random color for my background. Only one thing, one of the value should be 255. Like (234,85,38) is not fine, but (33,255,82) is fine.

Currently I'm trying to first switch a random for which value (R,G or B) should be 255. Then in each case I'm defining the other values at random. But it doesn't work.

void setBackgroundColor(){
        Color color = new Color();
        switch(Random.Range(1,3)){
            case 1:
                color = new Color(255, Random.Range(1,255), Random.Range(1,255));
                break;
            case 2:
                color = new Color(Random.Range(1,255), 255, Random.Range(1,255));
                break;
            case 3:
                color = new Color(Random.Range(1,255), Random.Range(1,255), 255);
                break;
        }
        Camera.main.backgroundColor = color;
    }

Okay, couple of issues. The first is that the Color class uses float values from 0 to 1 to represent its colors. If you're looking explicitly to set values based on the 0-255 scale, you want the Color32 class. That said, Color probably serves you fine and has more features to boot, you just need to remember that you want to use the float version of Random.Range() , rather than the integer version. Specifying the parameters as floats fixes that:

void setBackgroundColor(){
    Color color = new Color();
    switch(Random.Range(1,3)){
        case 1:
            color = new Color(1f, Random.Range(0f ,1f), Random.Range(0f, 1f));
            break;
        case 2:
            color = new Color(Random.Range(0f, 1f), 1f, Random.Range(0f, 1f));
            break;
        case 3:
            color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), 1f);
            break;
    }
    Camera.main.backgroundColor = color;
}

Now, we can actually compress this further. Something to remember is that you can access the RGBA components of a color using color[0] through color[3] . In that, you can actually simplify your code to just generate a random color and then overwrite a random index:

void setBackgroundColor() {
    Color color = new Color(Random.Range(0f, 1f), Random.Range(0f ,1f), Random.Range(0f, 1f));
    color[Random.Range(0,2)] = 1f;
    Camera.main.backgroundColor = color;
}

Now this is better, but thinking on what you're trying to do, I'm going to hazard a guess and say you want a color that's not too dark or desaturated. Consider explicitly generating a color from HSV (Hue, Saturation, Value) values using the Color.HSVToRGB() function:

void setBackgroundColor() {
    Camera.main.backgroundColor = Color.HSVToRGB(Random.Range(0f, 1f), 1f, 1f);
}

With this, we just specify that it's the hue of the color we want to randomize, rather than anything else. And in fact, there's one more optimization: This is a one line function, and it probably only gets called in one place. You can likely just delete the function call entirely and replace it with the contents of the function:

Camera.main.backgroundColor = Color.HSVToRGB(Random.Range(0f, 1f), 1f, 1f);

EDIT: You know what? There's an even cleaner way to do this. Because it turns out, Random has a function called Random.ColorHSV() :

Camera.main.backgroundColor = Random.ColorHSV(0f, 1f, 1f, 1f, 1f, 1f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM