gpt4 book ai didi

swift - 检查颜色是否为蓝色(ish)、红色(ish)、绿色(ish)、

转载 作者:行者123 更新时间:2023-12-03 09:22:51 27 4
gpt4 key购买 nike

我想实现一个可以按颜色过滤图像的搜索。我的图像模型包含多达 10 个出现在该特定图像中的 UIColors,现在我想要一个过滤器,例如蓝色,绿色,红色,黄色。我如何检查(以指定的容差)该特定图像是否包含蓝色/绿色/...?

我尝试使用 CIE94-difference,但这与人眼感知的相似性不符。我还尝试比较色调和饱和度值,但这也不起作用。

举个例子:

`#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

等等...

最佳答案

如果您只想检查哪种颜色是您的 UIColor,您可以简单地获取您的 HSB 颜色分量并比较其色调值:
您需要一个 helper 来转换您的 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



关于swift - 检查颜色是否为蓝色(ish)、红色(ish)、绿色(ish)、,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60047318/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com