V
V
Valentine2019-11-14 18:52:05
C++ / C#
Valentine, 2019-11-14 18:52:05

How to generate colors dissimilar to each other?

In my game, balls of different colors are generated on the field.
5dcd76e1d406a382570061.jpeg
The color of each ball is set using a simple function:

Color RandomColor()
    {
        float r = (float)mRandom.GetRandomNumber(0, 1f);
        float g = (float)mRandom.GetRandomNumber(0, 1f);
        float b = (float)mRandom.GetRandomNumber(0, 1f);
        float a = (float)mRandom.GetRandomNumber(0.7, 1);
        return new Color(r, g, b, a);
    }

Players complain that sometimes the balls are too similar in color and cannot be distinguished.
5dcd779bccf4e471308672.png
I knew about this and thought it added interest to the game, but the players think otherwise and it really pisses them off. How can I change the function in such a way to exclude such situations? Will it be efficient to compare the rgba values ​​of the balls and regenerate the color in case of close values?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
GavriKos, 2019-11-14
@val_gr

1) The most primitive - color presets. And choose from an array of presets.
2) Try using HSL instead of RGB. Firstly, it is easier to detect "similar" colors in it, secondly, it is easier to get more different colors - just generate the H parameter with some step, and not the entire range from 0 to 1.
Why do you randomize the alpha? )

D
devalone, 2019-11-14
@devalone

IMHO, it's better to use HSV(hue, saturation, value) for generation, generating colors with very different Hue

D
DanielMcRon, 2019-11-14
@DanielMcRon

Can make a list, somewhere ~ 30 colors, and a ball will be taken from it at random

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question