检查颜色是否为蓝色(红色),红色(绿色),绿色(红色)

时间:2020-02-03 21:20:10

标签: swift uicolor

我想执行一个搜索,其中可以按颜色过滤图像。我的图像模型最多包含10个在该特定图像中出现的UIColor,现在我想使用一个滤镜,例如蓝色,绿色,红色,黄色。如何检查(具有指定的公差)该特定图像是否包含蓝色/绿色/...?

我尝试使用CIE94差异,但这与人眼感知到的相似性不匹配。我还尝试比较色相和饱和度值,但这也不起作用。

例如:

`#23567E` should be blue
`#7A010B` should be red as well as `#FD4E57`
`#0F8801` should be found for green as well as `#85FE97`

我有一个UIColor的特定实例,例如

[UIColor colorWithRed:0.137 green:0.337 blue:0.494 alpha:1] // #23567E

这应该等同于.blue

[UIColor colorWithRed:0.478 green:0.00392 blue:0.0431 alpha:1] // #7A010B

应该与.red“相等”

以此类推...

3 个答案:

答案 0 :(得分:7)

如果您只想检查UIColor是哪种颜色,则只需获取HSB颜色分量并比较其色相值即可:

您将需要一个助手来转换您的hexa string to UIColor

extension UIColor {
    convenience init?(hexString: String) {
        var chars = Array(hexString.hasPrefix("#") ? hexString.dropFirst() : hexString[...])
        switch chars.count {
        case 3: chars = chars.flatMap { [$0, $0] }; fallthrough
        case 6: chars = ["F","F"] + chars
        case 8: break
        default: return nil
        }
        self.init(red: .init(strtoul(String(chars[2...3]), nil, 16)) / 255,
                green: .init(strtoul(String(chars[4...5]), nil, 16)) / 255,
                 blue: .init(strtoul(String(chars[6...7]), nil, 16)) / 255,
                alpha: .init(strtoul(String(chars[0...1]), nil, 16)) / 255)
    }
}

和一些助手来提取其色相成分:

extension UIColor {
    enum Color {
        case red, orange, yellow, yellowGreen, green, greenCyan, cyan, cyanBlue, blue, blueMagenta, magenta, magentaRed
    }
    func color(tolerance: Int = 15) -> Color? {
        precondition(0...15 ~= tolerance)
        guard let hsb = hsb else { return nil }
        if hsb.saturation == 0 { return nil }
        if hsb.brightness == 0 { return nil }
        let hue = Int(hsb.hue * 360)
        switch hue {
        case 0 ... tolerance: return .red
        case 30 - tolerance ... 30 + tolerance: return .orange
        case 60 - tolerance ... 60 + tolerance: return .yellow
        case 90 - tolerance ... 90 + tolerance: return .yellowGreen
        case 120 - tolerance ... 120 + tolerance: return .green
        case 150 - tolerance ... 150 + tolerance: return .greenCyan
        case 180 - tolerance ... 180 + tolerance: return .cyan
        case 210 - tolerance ... 210 + tolerance: return .cyanBlue
        case 240 - tolerance ... 240 + tolerance: return .blue
        case 270 - tolerance ... 270 + tolerance: return .blueMagenta
        case 300 - tolerance ... 300 + tolerance: return .magenta
        case 330 - tolerance ... 330 + tolerance: return .magentaRed
        case 360 - tolerance ... 360 : return .red
        default: break
        }
        return nil
    }
    var hsb: (hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat)? {
        var hue: CGFloat = 0, saturation: CGFloat = 0, brightness: CGFloat = 0, alpha: CGFloat = 0
        guard getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) else {
            return nil
        }
        return (hue, saturation, brightness, alpha)
    }
}

游乐场测试:

let blue: UIColor.Color? = UIColor(hexString: "#23567E").color()    // r 0.137 g 0.337 b 0.494 a 1.0
let red1: UIColor.Color? = UIColor(hexString: "#7A010B").color()    // r 0.478 g 0.004 b 0.043 a 1.0
let red2: UIColor.Color? = UIColor(hexString: "#FD4E57").color()    // r 0.992 g 0.306 b 0.341 a 1.0
let green1: UIColor.Color? = UIColor(hexString: "#0F8801").color()  // r 0.059 g 0.533 b 0.004 a 1.0
let green2: UIColor.Color? = UIColor(hexString: "#85FE97").color()  // t 0.522 g 0.996 b 0.592 a 1.0

UIColor(hue: 90/360, saturation: 0, brightness: 1, alpha: 1).color()  // nil
UIColor(hue: 90/360, saturation: 1, brightness: 0, alpha: 1).color()  // nil
UIColor(hue: 90/360, saturation: 0, brightness: 0, alpha: 1).color()  // nil
UIColor.black.color()       // nil
UIColor.white.color()       // nil
UIColor.lightGray.color()   // nil
UIColor.darkGray.color()    // nil

UIColor.red.color()     // 0...15 && 346...360 = red
UIColor.orange.color()  // 16...45 = orange
UIColor.yellow.color()  // 46...75 = yellow
UIColor(hue: 90/360, saturation: 1, brightness: 1, alpha: 1).color()   // 76...105 yellowGreen
UIColor.green.color()   // 106...135 = green
UIColor(hue: 150/360, saturation: 1, brightness: 1, alpha: 1).color()  // 136...165 greenCyan
UIColor.cyan.color()    // 166...195 = cyan
UIColor(hue: 210/360, saturation: 1, brightness: 1, alpha: 1).color()  // 196...225 cyanBlue
UIColor.blue.color()    // 226...255 = blue
UIColor(hue: 270/360, saturation: 1, brightness: 1, alpha: 1).color() // 256...285 blueMagenta
UIColor.magenta.color()  // 286...315 = magenta
UIColor(hue: 330/360, saturation: 1, brightness: 1, alpha: 1).color()  // 316...345 = magentaRed

答案 1 :(得分:2)

UIColor的{​​{1}}具有CGColor属性,该属性返回由红色,绿色,蓝色,alpha值组成的数组,components

它可以根据您的要求使用基于RGB的颜色。也可以使用其他颜色,但是将返回不同的值集,而不是r,g,b和a值

使用您提供的颜色进行了测试:

[CGFloat]?

这是您要找的吗?

答案 2 :(得分:0)

您可以使用内置方法从UIColor实例中获取RGB值:

let color = UIColor(red: 0.3, green: 0.8, blue: 0.4, alpha: 0.9)
var red = CGFloat.zero
var green = CGFloat.zero
var blue = CGFloat.zero
var alpha = CGFloat.zero
color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
print("r: \(red), g: \(green), b: \(blue)")
相关问题