我正在尝试使用以下函数组装UIColor:
var backgroundColor: UIColor = UIColor.clearColor()
let colorsDictionary = chemistryDictionary["backgroundColor"] as! [String: CGFloat]
backgroundColor = rgbColorFromDictionary(colorsDictionary)
}
func rgbColorFromDictionary(colorDictionary: [String: CGFloat]) -> UIColor {
let red = colorDictionary["red"]!
let green = colorDictionary["green"]!
let blue = colorDictionary["blue"]!
let alpha = colorDictionary["alpha"]!
return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha)
}
}
我正在从以下库中绘制彩妆(使用缩放键):
struct ColorMatchLibrary {
let library = [
// pH Array
[ "title": "pH",
"description": "Most living things depend on proper pH level to sustain life.",
"scale":
["6.2":
["red": 212, "green": 142, "blue": 69, "alpha": 1.0],
"6.8":
["red": 209, "green": 122, "blue": 31, "alpha": 1.0],
"7.2":
["red": 196, "green": 80, "blue": 9, "alpha": 1.0],
"7.8":
["red": 194, "green": 74, "blue": 58, "alpha": 1.0],
"8.4":
["red": 208, "green": 48, "blue": 75, "alpha": 1.0]
]
],
// Ammonia Array
[ "title": "Ammonia",
"description": "",
"scale":
[
"0":
["red": 244, "green": 235, "blue": 130, "alpha": 1.0],
".25":
["red": 233, "green": 233, "blue": 156, "alpha": 1.0],
".5":
["red": 223, "green": 238, "blue": 141, "alpha": 1.0],
"1.0":
["red": 221, "green": 236, "blue": 210, "alpha": 1.0],
"3.0":
["red": 202, "green": 227, "blue": 191, "alpha": 1.0],
"6.0":
["red": 186, "green": 216, "blue": 173, "alpha": 1.0]
]
]
]
}
我想知道该函数是否采用了不正确的参数,或者我是否必须修改我的代码以适应该函数。我试图修改参数但是我遇到了很多错误。所以我想我的问题如下:
如何创建一个接受缩放键颜色值的函数,或者如果不起作用,我是否必须更改整个代码或库本身?
这是我的所有代码:
var backgroundColor: UIColor = UIColor.clearColor()
let colorsDictionary = chemistryDictionary["backgroundColor"] as! [String: CGFloat]
backgroundColor = rgbColorFromDictionary(colorsDictionary)
}
func rgbColorFromDictionary(colorDictionary: [String: CGFloat]) -> UIColor {
let red = colorDictionary["red"]!
let green = colorDictionary["green"]!
let blue = colorDictionary["blue"]!
let alpha = colorDictionary["alpha"]!
return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha)
}
}
其他档案:
struct ColorMatchLibrary {
let library = [
// pH Array
[ "title": "pH",
"description": "Most living things depend on proper pH level to sustain life.",
"scale":
["6.2":
["red": 212, "green": 142, "blue": 69, "alpha": 1.0],
"6.8":
["red": 209, "green": 122, "blue": 31, "alpha": 1.0],
"7.2":
["red": 196, "green": 80, "blue": 9, "alpha": 1.0],
"7.8":
["red": 194, "green": 74, "blue": 58, "alpha": 1.0],
"8.4":
["red": 208, "green": 48, "blue": 75, "alpha": 1.0]
]
],
// Ammonia Array
[ "title": "Ammonia",
"description": "",
"scale":
[
"0":
["red": 244, "green": 235, "blue": 130, "alpha": 1.0],
".25":
["red": 233, "green": 233, "blue": 156, "alpha": 1.0],
".5":
["red": 223, "green": 238, "blue": 141, "alpha": 1.0],
"1.0":
["red": 221, "green": 236, "blue": 210, "alpha": 1.0],
"3.0":
["red": 202, "green": 227, "blue": 191, "alpha": 1.0],
"6.0":
["red": 186, "green": 216, "blue": 173, "alpha": 1.0]
]
]
]
}
非常感谢任何建议或意见。请在答案中留下更新,删除,添加或替换的代码,并解释您为什么要这样做。
提前致谢。
修改
如何修改以下代码以适应更改?
struct Chemistry {
var title: String?
var description: String?
var backgroundColor: UIColor = UIColor.clearColor()
init(index: Int) {
let colorMatchLibrary = ColorMatchLibrary.library
let chemistryDictionary = colorMatchLibrary[index]
title = chemistryDictionary["title"] as! String!
description = chemistryDictionary["description"] as! String!
let colorsDictionary = chemistryDictionary["backgroundColor"] as! [String: UIColor]
backgroundColor = rgbColorFromDictionary(colorsDictionary)
}
func rgbColorFromDictionary(colorDictionary: [String: UIColor]) -> UIColor {
let red = colorDictionary["red"]!
let green = colorDictionary["green"]!
let blue = colorDictionary["blue"]!
let alpha = colorDictionary["alpha"]!
return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha)
}
}
答案 0 :(得分:2)
尝试这种类型的组织:
struct ColorMatchInfo {
let title:String
let description:String
let scaleColors:[String:UIColor]
}
extension UIColor {
convenience init(red:Int, green:Int, blue:Int) {
self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: 1.0)
}
}
let colorMatches = [
ColorMatchInfo(title: "ph",
description: "Most living things depend on proper pH level to sustain life.",
scaleColors: [
"6.2": UIColor(red: 212, green: 142, blue: 69),
"6.8": UIColor(red: 209, green: 122, blue: 31),
"7.2": UIColor(red: 196, green: 80, blue: 9),
"7.8": UIColor(red: 194, green: 74, blue: 58),
"8.4": UIColor(red: 208, green: 48, blue: 75)
]),
ColorMatchInfo(title: "Ammonia",
description: "",
scaleColors: [
"0.00": UIColor(red: 244, green: 235, blue: 130),
"0.25": UIColor(red: 233, green: 233, blue: 156),
"0.50": UIColor(red: 223, green: 238, blue: 141),
"1.00": UIColor(red: 221, green: 236, blue: 210),
"3.00": UIColor(red: 202, green: 227, blue: 191),
"6.00": UIColor(red: 202, green: 216, blue: 173)
]),
]
let aColor = colorMatches[0].scaleColors["6.2"]