开发者

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

开发者 https://www.devze.com 2023-03-18 03:16 出处:网络
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

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 ve开发者_如何学JAVAry 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.


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'.


Using a random color as a background is indeed a suboptimal solution as it will lead to visibility and readability problems of the text and information of your icon. There is some research going on in automatically finding an appropriate color that matches your foreground colors, leading to a harmonic result in terms of visual perception of the image. Have a look at http://cs.nyu.edu/~sorkine/ProjectPages/Harmonization/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号