gpt4 book ai didi

Swift:对给定属性进行操作

转载 作者:搜寻专家 更新时间:2023-10-31 22:47:40 25 4
gpt4 key购买 nike

我有一个以 RGBA 形式表示位图图像的结构。该结构具有每个颜色 channel (红色、蓝色和绿色)的属性。

我正在尝试构建一个调整特定颜色级别的图像过滤器。这个版本工作正常,但显然 pixel.COLOR 属性是硬编码的。

func boostColor(inputColor: String) -> UIImage {
let avgColor = "avg" + inputColor
// let color = inputColor.lowercaseString // plan to use this to set the property
for y in 0..<self.inputRGBA.height {
for x in 0..<self.inputRGBA.width {
let index = y * self.inputRGBA.width + x
var pixel = self.inputRGBA.pixels[index]
// see how far this pixel's chosen color varies from the average
let colorDiff = Int(pixel.red) - Int(self.pixelColorAverages[avgColor]!)
// if less than average red,
if(colorDiff>0){
pixel.red = UInt8( max(0,min(255,Int(self.pixelColorAverages[avgColor]!) + colorDiff*100 ) ) )
// write the adjusted pixel back to the image object
self.inputRGBA.pixels[index] = pixel
}
}
}
// write the filtered RGBA image to a UIImage
return self.inputRGBA.toUIImage()!
}

这个循环的函数接受一个字符串值“red”、“blue”或“green”。我想做的是替换

的实例
pixel.red

pixel.[ONE OF THE INPUT COLORS]

谢谢!(这是一个学术 Playground 项目。我意识到我可能正在通过构建滤色器来重新发明轮子。我感兴趣的不是滤色器本身,而是如何在 Swift 中解决这个属性问题。 )这是结构:

public struct Pixel {
public var value: UInt32

public var red: UInt8 {
get {
return UInt8(value & 0xFF)
}
set {
value = UInt32(newValue) | (value & 0xFFFFFF00)
}
}

public var green: UInt8 {
get {
return UInt8((value >> 8) & 0xFF)
}
set {
value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF)
}
}

public var blue: UInt8 {
get {
return UInt8((value >> 16) & 0xFF)
}
set {
value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF)
}
}

public var alpha: UInt8 {
get {
return UInt8((value >> 24) & 0xFF)
}
set {
value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF)
}
}

最佳答案

通过 reflecting 可以实现键值编码,但它非常不快速。在您的情况下,这也是一个坏主意,因为有更强大和有用的替代方案。

我会选择 OptionSetType,因为只有固定数量的可能性 (RGBA)。 enum 也可以,但是 OptionSetType 具有作为“集合”的额外好处。因此,您可以选择以非常方便的方式更改 RGB 而不是 A。

interesting read on OptionSetType


这是一个枚举,用于存储OptionSetType 的原始值。可以单独使用。

public enum RGBAColor : Int, CustomStringConvertible {
case Red = 1, Green = 2, Blue = 4, Alpha = 8

public var description : String { // if you still want to have a String value for each color
var shift = 0
while (rawValue >> shift != 1) { shift++ }
return ["Red","Green","Blue","Alpha"][shift]
}
}

这是OptionSetType。我创建了这些结构的片段,并在需要时复制/粘贴/修改它们。无需重新发明轮子,因为它始终是相同的模式。

public struct RGBAColors : OptionSetType, CustomStringConvertible {

public let rawValue : Int
public init(rawValue:Int) { self.rawValue = rawValue}
private init(_ color:RGBAColor) { self.rawValue = color.rawValue }

static let Red = RGBAColors(RGBAColor.Red)
static let Green = RGBAColors(RGBAColor.Green)
static let Blue = RGBAColors(RGBAColor.Blue)
static let Alpha = RGBAColors(RGBAColor.Alpha)

public var description : String {
var result = ""
var shift = 0

while let currentcolor = RGBAColor(rawValue: 1 << shift++){
if self.contains(RGBAColors(currentcolor)){
result += (result.characters.count == 0) ? "\(currentcolor)" : ",\(currentcolor)"
}
}

return "[\(result)]"
}
}

现在您可以向 Pixel 结构添加一个函数,该函数将根据 enum case 返回颜色值。您还可以选择将 OptionSetType 直接传递给此函数。

public struct Pixel {

...

public func getValueForColor(color:RGBAColor) -> UInt8 {
switch color {
case .Red : return self.red
case .Green : return self.green
case .Blue : return self.blue
case .Alpha : return self.alpha
}
}
}

这只是一个控制流函数。您现在可以根据需要更改的颜色执行不同的操作。 (或对所有人都适用)这也是选择 OptionSetType 的唯一原因,如果您不需要它,只需选择 enum。由于 OptionSetType 建立在 enum 之上,您可以随时添加它。

func someFuncWithOptions(colors:RGBAColors) {

if colors.contains(.Red) {
someFunc(.Red)
}
if colors.contains(.Blue) {
someFunc(.Blue)
}
if colors.contains(.Green) {
someFunc(.Green)
}
if colors.contains(.Alpha) {
someFunc(.Alpha)
}
}

这将是您的实际功能

func someFunc(color:RGBAColor) {
// do something with pixel.getValueForColor(color)
print(color)
}

这将暴露给您代码的其他部分。所以所有这些的实际使用将非常简单。只需输入 .SomeColor

最好的是,如果您将所有这些都更改为 CMYK,编译器会警告您。键值编码永远不会产生警告,它只会崩溃。

因为它是一个“集合”而不是每个定义的一种颜色,所以您传递了一组选项。 [.option1,.option2,...] 或者您什么都不传递 []

someFuncWithOptions([.Red]) // red
someFuncWithOptions([.Green,.Red]) // red green

您还可以将方便的集合添加到 OptionSetType。这将是一组所有 RGB 颜色,但没有 Alpha channel 。

static let RGB : RGBAColors = [.Red,.Green,.Blue]

someFuncWithOptions([.RGB]) // red blue green

关于Swift:对给定属性进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34359914/

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