Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get a random UIColor but restrict it to subset of the colors

I know how get a random UIColor, but I wonder if exist a way to only return colors with some specific quality of it. I don't know if exist a specific term, but for example, I need to get only opaque colors, dark tones... colors that are good for backgrounds with bright text/ images on it.

I could hand-pick some, but in my case i want to provide a very good defaults for a large list of icons backgrounds.

UPDATE:

My solution based on the answer here:

float r = arc4random() % 12; 

return [UIColor colorWithHue:(30*r)/360 saturation:0.5f brightness:0.8f alpha:1.0f];

I get a very small list of possible answers, but this give me a good start.

like image 791
mamcx Avatar asked Nov 04 '22 18:11

mamcx


1 Answers

What I would do is use the HSV/HSL colour space and then convert the results to RGB. Keep a constant value and saturation (S and V) whose qualities you like, and then alter the hue (H) value for the number of colours you want. Convert the results to RGB and you'll have a nice set of colours that appear to work well together. You can create any set of nice colours that are 'saturated', 'bright', 'dark', etc. just by picking nice combinations of S and V for your needs (low V is dark, high V is bright, low S is unsaturated, high S is saturated).

Take a look at this wiki page for a discussion on converting HSV to RGB. I'd recommend you start with saturation (S) at 0.5 and value (V) at 0.8. Then, to generate nice colours, do the HSV -> RGB conversion for H values of 0, 30, 60, 90... all the way up to 360. It will generate a nice palette of colours that look good together. Later, you can adjust your S and V values if you want different colour 'qualities'.

like image 151
aardvarkk Avatar answered Nov 12 '22 14:11

aardvarkk