Check if UIColor is dark or bright?

前端 未结 14 2015
醉酒成梦
醉酒成梦 2020-11-29 17:20

I need to determine whether a selected UIColor (picked by the user) is dark or bright, so I can change the color of a line of text that sits on top of that color, for better

相关标签:
14条回答
  • 2020-11-29 17:38

    For me using only CGColorGetComponents didn't worked, I get 2 components for UIColors like white. So I have to check the color spaceModel first. This is what I came up with that ended up being the swift version of @mattsven's answer.

    Color space taken from here: https://stackoverflow.com/a/16981916/4905076

    extension UIColor {
        func isLight() -> Bool {
            if let colorSpace = self.cgColor.colorSpace {
                if colorSpace.model == .rgb {
                    guard let components = cgColor.components, components.count > 2 else {return false}
    
                    let brightness = ((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000
    
                    return (brightness > 0.5)
                }
                else {
                    var white : CGFloat = 0.0
    
                    self.getWhite(&white, alpha: nil)
    
                    return white >= 0.5
                }
            }
    
            return false
        }
    
    0 讨论(0)
  • 2020-11-29 17:39

    If you want to find the brightness of the color, here is some pseudo code:

    public float GetBrightness(int red, int blue, int green)
    {
        float num = red / 255f;
        float num2 = blue / 255f;
        float num3 = green / 255f;
        float num4 = num;
        float num5 = num;
        if (num2 > num4)
            num4 = num2;
        if (num3 > num4)
            num4 = num3;
        if (num2 < num5)
            num5 = num2;
        if (num3 < num5)
            num5 = num3;
        return ((num4 + num5) / 2f);
    }
    

    If it is > 0.5 it is bright, and otherwise dark.

    0 讨论(0)
  • 2020-11-29 17:44

    My solution to this problem in a category (drawn from other answers here). Also works with grayscale colors, which at the time of writing none of the other answers do.

    @interface UIColor (Ext)
    
        - (BOOL) colorIsLight;
    
    @end
    
    @implementation UIColor (Ext)
    
        - (BOOL) colorIsLight {
            CGFloat colorBrightness = 0;
    
            CGColorSpaceRef colorSpace = CGColorGetColorSpace(self.CGColor);
            CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace);
    
            if(colorSpaceModel == kCGColorSpaceModelRGB){
                const CGFloat *componentColors = CGColorGetComponents(self.CGColor);
    
                colorBrightness = ((componentColors[0] * 299) + (componentColors[1] * 587) + (componentColors[2] * 114)) / 1000;
            } else {
                [self getWhite:&colorBrightness alpha:0];
            }
    
            return (colorBrightness >= .5f);
        }
    
    @end
    
    0 讨论(0)
  • 2020-11-29 17:44

    For everything that's not grayish, the RGB inverse of a color is usually highly contrasted with it. The demo just inverts the color and desaturates it (converts it to a gray).

    But generating a nice soothing combination of colors is quite complicated. Look at :

    http://particletree.com/notebook/calculating-color-contrast-for-legible-text/

    0 讨论(0)
  • 2020-11-29 17:47

    Swift3

    extension UIColor {
        var isLight: Bool {
            var white: CGFloat = 0
            getWhite(&white, alpha: nil)
            return white > 0.5
        }
    }
    
    // Usage
    if color.isLight {
        label.textColor = UIColor.black
    } else {
        label.textColor = UIColor.white
    }
    
    0 讨论(0)
  • 2020-11-29 17:47

    Swift 4 Version

    extension UIColor {
        func isLight() -> Bool {
            guard let components = cgColor.components, components.count > 2 else {return false}
            let brightness = ((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000
            return (brightness > 0.5)
        }
    }
    
    0 讨论(0)
提交回复
热议问题