祝您有美好的一天。我正在做RGB LED变色应用程序。我正在使用HSB选择颜色。这些颜色具有色相,饱和度,亮度和Alpha值。我将这些值保存在数据库中。我正在用arduino读取此数据。但是我如何找出这些颜色值属于哪种颜色?那么如何找到与通过arduino选择的颜色相当的颜色呢?
import Foundation
import UIKit
// The view to edit HSB color components.
public class EFHSBView: UIView, EFColorView, UITextFieldDelegate {
let EFColorSampleViewHeight: CGFloat = 30.0
let EFViewMargin: CGFloat = 20.0
let EFColorWheelDimension: CGFloat = 200.0
private let colorWheel: EFColorWheelView = EFColorWheelView()
let brightnessView: EFColorComponentView = EFColorComponentView()
private let colorSample: UIView = UIView()
private var colorComponents: HSB = HSB(1, 1, 1, 1)
private var layoutConstraints: [NSLayoutConstraint] = []
weak public var delegate: EFColorViewDelegate?
public var isTouched: Bool {
if self.colorWheel.isTouched {
return true
}
if self.brightnessView.isTouched {
return true
}
return false
}
public var color: UIColor {
get {
return UIColor(
hue: colorComponents.hue,
saturation: colorComponents.saturation,
brightness: colorComponents.brightness,
alpha: colorComponents.alpha
)
}
set {
colorComponents = EFRGB2HSB(rgb: EFRGBColorComponents(color: newValue))
self.reloadData()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
self.ef_baseInit()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.ef_baseInit()
}
func reloadData() {
colorSample.backgroundColor = self.color
colorSample.accessibilityValue = EFHexStringFromColor(color: self.color)
self.ef_reloadViewsWithColorComponents(colorComponents: colorComponents)
self.colorWheel.display(self.colorWheel.layer)
}
override public func updateConstraints() {
self.ef_updateConstraints()
super.updateConstraints()
}
// MARK:- Private methods
private func ef_baseInit() {
self.accessibilityLabel = "hsb_view"
colorSample.accessibilityLabel = "color_sample"
colorSample.layer.borderColor = UIColor.black.cgColor
colorSample.layer.borderWidth = 0.5
colorSample.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(colorSample)
colorWheel.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(colorWheel)
brightnessView.title = NSLocalizedString("Brightness", comment: "")
brightnessView.maximumValue = EFHSBColorComponentMaxValue
brightnessView.format = "%.2f"
brightnessView.translatesAutoresizingMaskIntoConstraints = false
brightnessView.setColors(colors: [UIColor.black, UIColor.white])
self.addSubview(brightnessView)
colorWheel.addTarget(
self, action: #selector(ef_colorDidChangeValue(sender:)), for: UIControl.Event.valueChanged
)
brightnessView.addTarget(
self, action: #selector(ef_brightnessDidChangeValue(sender:)), for: UIControl.Event.valueChanged
)
self.setNeedsUpdateConstraints()
}
private func ef_updateConstraints() {
// remove all constraints first
if !layoutConstraints.isEmpty {
self.removeConstraints(layoutConstraints)
}
layoutConstraints = UIUserInterfaceSizeClass.compact == self.traitCollection.verticalSizeClass
? self.ef_constraintsForCompactVerticalSizeClass()
: self.ef_constraintsForRegularVerticalSizeClass()
self.addConstraints(layoutConstraints)
}
private func ef_constraintsForRegularVerticalSizeClass() -> [NSLayoutConstraint] {
let metrics = [
"margin" : EFViewMargin,
"height" : EFColorSampleViewHeight,
"color_wheel_dimension" : EFColorWheelDimension
]
let views = [
"colorSample" : colorSample,
"colorWheel" : colorWheel,
"brightnessView" : brightnessView
]
var layoutConstraints: [NSLayoutConstraint] = []
let visualFormats = [
"H:|-margin-[colorSample]-margin-|",
"H:|-margin-[colorWheel(>=color_wheel_dimension)]-margin-|",
"H:|-margin-[brightnessView]-margin-|",
"V:|-margin-[colorSample(height)]-margin-[colorWheel]-margin-[brightnessView]-(>=margin@250)-|"
]
for visualFormat in visualFormats {
layoutConstraints.append(
contentsOf: NSLayoutConstraint.constraints(
withVisualFormat: visualFormat,
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
metrics: metrics,
views: views
)
)
}
layoutConstraints.append(
NSLayoutConstraint(
item: colorWheel,
attribute: NSLayoutConstraint.Attribute.width,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: colorWheel,
attribute: NSLayoutConstraint.Attribute.height,
multiplier: 1,
constant: 0)
)
return layoutConstraints
}
private func ef_constraintsForCompactVerticalSizeClass() -> [NSLayoutConstraint] {
let metrics = [
"margin" : EFViewMargin,
"height" : EFColorSampleViewHeight,
"color_wheel_dimension" : EFColorWheelDimension
]
let views = [
"colorSample" : colorSample,
"colorWheel" : colorWheel,
"brightnessView" : brightnessView
]
var layoutConstraints: [NSLayoutConstraint] = []
let visualFormats = [
"H:|-margin-[colorSample]-margin-|",
"H:|-margin-[colorWheel(>=color_wheel_dimension)]-margin-[brightnessView]-(margin@500)-|",
"V:|-margin-[colorSample(height)]-margin-[colorWheel]-(margin@500)-|"
]
for visualFormat in visualFormats {
layoutConstraints.append(
contentsOf: NSLayoutConstraint.constraints(
withVisualFormat: visualFormat,
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
metrics: metrics,
views: views
)
)
}
layoutConstraints.append(
NSLayoutConstraint(
item: colorWheel,
attribute: NSLayoutConstraint.Attribute.width,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: colorWheel,
attribute: NSLayoutConstraint.Attribute.height,
multiplier: 1,
constant: 0)
)
layoutConstraints.append(
NSLayoutConstraint(
item: brightnessView,
attribute: NSLayoutConstraint.Attribute.centerY,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: self,
attribute: NSLayoutConstraint.Attribute.centerY,
multiplier: 1,
constant: 0)
)
return layoutConstraints
}
private func ef_reloadViewsWithColorComponents(colorComponents: HSB) {
colorWheel.hue = colorComponents.hue
colorWheel.saturation = colorComponents.saturation
colorWheel.brightness = colorComponents.brightness
self.ef_updateSlidersWithColorComponents(colorComponents: colorComponents)
}
private func ef_updateSlidersWithColorComponents(colorComponents: HSB) {
brightnessView.value = colorComponents.brightness
}
@objc private func ef_colorDidChangeValue(sender: EFColorWheelView) {
colorComponents.hue = sender.hue
colorComponents.saturation = sender.saturation
self.delegate?.colorView(self, didChangeColor: self.color)
self.reloadData()
}
@objc private func ef_brightnessDidChangeValue(sender: EFColorComponentView) {
colorComponents.brightness = sender.value
self.colorWheel.brightness = sender.value
self.delegate?.colorView(self, didChangeColor: self.color)
self.reloadData()
}
}
答案 0 :(得分:0)
色相饱和度值模型是一种广泛使用的颜色编码方法。
色调是一个循环参数,这就是为什么要以度(360度=完整周期)进行编码的原因。颜色的红色,绿色和蓝色分量沿HUE轴定期更改,在其较低值处保持120度,然后在60度内上升到较高值,在较高值处保持120度,然后在接下来的60度处再次减小。 360度后重复该循环。
红色绿色和蓝色分量彼此相对移动了120度,因此可以对它们之间的任何比例进行编码。
在上图中,您可以看到,对于每个H值,RGB分量之一处于较高的值,另一个RGB分量处于较低的值,而第三个在这两个分量之间改变。
值组件对RGB的上限值进行编码。即0%值等于黑色(无论色相和饱和度值如何),而100%-最亮的颜色
HSV模型的 Saturation 分量编码R G B分量的上限值和下限值之间的差异,相对于V值 。即如果“值”为50%,“饱和度”为30%,则较低的值为35%,较高的值为50%。
饱和度值是零,然后颜色是灰色,无论Hue值是什么。
因此,将HSV转换为RGB的代码可能如下:
float h = ..., s = ..., v = ...; // input values
float r, g, b;
float d = v * s; // difference between upper and lower value
float l = v - d; // calculating the lower value
// code below assumes 0 <= h < 360. Otherwise wrap the value before
if (h < 60) { // 0..60
r = v;
g = l + (h / 60.0) * d;
b = l;
} else if (h < 120) { // 60..120
r = u - ((h - 60) / 60.0) * d;
g = v;
b = l;
} else if (h < 180) { // 120..180
r = l;
g = v;
b = l + ((h - 120) / 60.0) * d;
} else if (h < 240) { // 180..240
r = l;
g = u - ((h - 180) / 60.0) * d;
b = v;
} else if (h < 300) { // 240..300
r = l + ((h - 240) / 60.0) * d;
g = l;
b = v;
} else { // 300..360
r = v;
g = l;
b = u - ((h - 300) / 60.0) * d;
}
请注意,当使用MCU点亮RGB LED时,您获得的颜色可能与显示屏上显示的颜色不同。这是因为显示器正在使用Gamma Correction,即50%并不意味着50%的发光(通常接近25%)。因此,您可能需要对获得的RGB值应用相同的伽马校正,然后再将其传递给LED PWM。