【问题标题】:Randomly colorize UIButton how to avoid text (black) and background (white) color?随机着色 UIButton 如何避免文本(黑色)和背景(白色)颜色?
【发布时间】:2014-05-25 14:42:36
【问题描述】:

我有一个 UIButton 由标题文本和单色背景组成。按钮绘制在白色的 UIView 上,标题文字为黑色。

我想在每次启动应用程序时随机为按钮的背景着色。 这是我的实现:

#define KEY_COLOR_R @"KEY_COLOR_R"
#define KEY_COLOR_G @"KEY_COLOR_G"
#define KEY_COLOR_B @"KEY_COLOR_B"

+ (void)setThemeColorRandomly {

    //TODO: THEME COLOR NEEDS TO BE EASILY DISTINGUISHABLE WITH BLACK AND WHITE
    float r = [Helper getRandomFloatBetweenLow:0 high:1];
    float g = [Helper getRandomFloatBetweenLow:0 high:1];
    float b = [Helper getRandomFloatBetweenLow:0 high:1];

    [ModuleHelper cache_setFloat:r forKey:KEY_COLOR_R];
    [ModuleHelper cache_setFloat:g forKey:KEY_COLOR_G];
    [ModuleHelper cache_setFloat:b forKey:KEY_COLOR_B];
}

+ (UIColor *)themeColor {

    float r = [ModuleHelper cache_floatForKey:KEY_COLOR_R];
    float g = [ModuleHelper cache_floatForKey:KEY_COLOR_G];
    float b = [ModuleHelper cache_floatForKey:KEY_COLOR_B];

    return [UIColor colorWithRed:r green:g blue:b alpha:1];
}

+setThemeColorRandomly-didFinishLaunching 中被调用

现在我设置颜色:

[self.settingsButton setBackgroundColor:[CacheHelper themeColor]];

问题是,我需要避免颜色太浅(与背景混合)或太暗(与文本混合)。但我不想限制颜色的多样性,例如如果我将所有 3 种颜色的范围设置为 0.4-0.6,那么我会错过红色,它很容易区分白色和黑色

编辑:

我需要避免的颜色不仅仅是完全的黑白。我需要避免所有几乎无法区分的颜色。

关键部分是“可区分的”。例如,非常非常浅的蓝色背景是一个不好的选择。

【问题讨论】:

    标签: ios cocoa-touch random uikit


    【解决方案1】:

    我建议使用 hsb 颜色模型,这样您就可以完全控制颜色的亮度:

    //set your color randomly
    CGFloat hue = [Helper getRandomFloatBetweenLow:0 high:1]; 
    //you may play around with the saturation to get more colors as well
    CGFloat saturation = 1; 
    //adjust your desired min max values
    CGFloat brightness = [Helper getRandomFloatBetweenLow:0.3 high:0.7];
    
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
    

    【讨论】:

    • 调整亮度以区分黑白非常容易。
    • 在photoshop的帮助下,我得到了很好的色调(0-1)饱和度(0.4-1)和亮度(0.8-1)的结果
    【解决方案2】:

    看我的例子:

    float r,g,b;
    
    do {
    
        r = [Helper getRandomFloatBetweenLow:0 high:1];
        g = [Helper getRandomFloatBetweenLow:0 high:1];
        b = [Helper getRandomFloatBetweenLow:0 high:1];
    
    } while(r == 0 && g == 0 && b == 0);
    

    在这种情况下,r gb 如果都是 0(即黑色),则会重新计算。

    如果其中一个不等于0,则可以。

    您也可以为所有1 扩展示例(白色)。

    在您的情况下,您可以简单地避免按钮上的白色背景,然后是所有相等的颜色:

    float r,g,b, r1,g1,b1;
    
    //Calculate the background different from white
    do {
    
        r = [Helper getRandomFloatBetweenLow:0 high:1];
        g = [Helper getRandomFloatBetweenLow:0 high:1];
        b = [Helper getRandomFloatBetweenLow:0 high:1];
    
    } while(r == 1 && g == 1 && b == 1);
    
    //Calculate the text color different from the background
    do {
    
        r1 = [Helper getRandomFloatBetweenLow:0 high:1];
        g1 = [Helper getRandomFloatBetweenLow:0 high:1];
        b1 = [Helper getRandomFloatBetweenLow:0 high:1];
    
    } while(r1 == r && g1 == g && b1 == b);
    

    或者,如果您想使用一定范围的色调来确保 2 种颜色有足够的不同,您可以在最后一次使用例如:

    } while(ABS(r1-r) > 0.3 && ABS(g1-g) > 0.3 && ABS(b1-b) > 0.3);
    

    这是功能,你可以选择你的喜好和容忍度。

    结果如下:

    【讨论】:

    • 我需要避免的颜色不仅仅是完全的黑白。我需要避免所有几乎无法区分的颜色
    • 还有什么问题?使用此代码,您可以实现您想要的
    • 我认为isSimilar 函数返回 ABS(r1-r2)
    猜你喜欢
    • 2017-09-04
    • 2010-11-22
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    相关资源
    最近更新 更多