问题描述
我正在构建一个用于家庭自动化系统中照明管理的接口。我设法为各种提供商控制了标准的开/关和可调光灯,但几乎没有问题,但是现在我遇到了与RGB灯有关的问题。
I am building an interface for light management in a home automation system. I managed to control standard on/off and dimmable light for various providers with little problem, but now I am stuck with a problem related to RGB light.
我所使用的灯当前使用的是RGBW led灯带-具体来说,我正在使用一种低成本RGBW灯:该灯由四个led组成,每个led都可以单独控制。
The light I am currently using is an RGBW led strip - specifically, I am working with a low-cost RGBW light: the light is composed by four led and every led can be controlled individually.
更清楚-我正在处理一些c#代码,这些代码应检索当前选择的颜色并将其显示在UI中,并使用户能够为光源指定新的颜色。为了设置颜色并检索颜色,我必须使用命令提供程序,该命令提供程序使我能够通过Web服务发送和接收命令。
To be more clear - I am working on some c# code that should retrieve the currently selected color and display it in the UI, and enable the user to specify a new color for the light. For setting the color and retrieving it I must use a command provider that enables me to send and receive commands via a web service.
该提供程序使用RGBW颜色-四种使用红色,绿色,蓝色和白色的组件。为了表示界面上的当前灯光颜色,我想将服务返回的RGBW颜色转换为更标准的RGB / HSB方案。
The provider works with RGBW colors - the four component for Red, Green, Blue and White are used. To represent the current light color on my interface I would like to translate the RGBW color returned by the service to a more standard RGB/HSB scheme.
在网络上搜索我发现的唯一有关颜色转换的参考(不包括从rgb到rgbw的c ++示例转换,根据我的理解,该示例必须有一些严重的bug)是本文显示的是从HSI到RGBW的转换,这与我的理解相反需要:
Searching the web, the only reference for color conversion that I found (excluding a c++ sample for rgb to rgbw conversion that, based on my understanding, must have some severe bug) is this article that shows a conversion from HSI to RGBW, which is the inverse of what I needed: link here
我正在寻找一些有关如何实现这种转换的见解(或简单解释为何无法实现转换)。据我所知,从RGB到RGBW的转换是任意的-一个RGB值可以表示为多个RGBW值,但是相反的转换应该是明确的。还要注意,当我使用c#时,也可以随意用其他语言引用算法-语言不是问题,问题在于我不知道进行颜色转换的数学方法。
I am searching for some insight about how I can achieve this conversion (or a simple explanation of why it isn't possible). As far as I get the conversion from RGB to an RGBW is arbitrary - a single RGB value can be represented as multiple RGBW values, but the opposite conversion should be univocal. Also note that while I am using c#, feel free to refer to algorithms in other language too - language isn't the problem, the problem is that I don't know the math to do the color conversion.
推荐答案
我一直在审查Roberto提出的答案,但是在许多情况下,颜色似乎更暗淡和饱和。以RGB =(255,255,2)为例,得出RGBW =(128,128,1,2)。
I have been reviewing the answer suggested by Roberto but the colors seemed dimmer and undersaturated in many cases. Taking the example of RGB = (255,255,2) leads to RGBW = (128,128,1,2).
进一步挖掘,看来Chul Lee的论文在K方程中存在错误。该方程来自Wang Lili的论文( Trade-移动电话使用的RGBW显示器中亮度和颜色之间的偏差)实际上是:
Digging further, it seems that the paper by Chul Lee has an error in its equation for K. The equation, which comes from a paper by Lili Wang ("Trade-off between luminance and color in RGBW displays for mobile-phone usage") is actually:
K = (Wo + M)/M
请注意,它是大写字母M,而不是小写字母m。有了此更改,您也不需要Q,因为它可以自然缩放。在相同的RGB =(255,255,2)上使用新的K会导致更合理的RGBW =(255,255,0,2)。
Note that it is a capital M, not a lowercase m. Given this change, you also do not need Q since it scales properly by nature. Using the new K on the same RGB = (255,255,2) example leads to a much more reasonable RGBW = (255,255,0,2).
将它们放在一起:
M = max(Ri,Gi,Bi)
m = min(Ri,Gi,Bi)
Wo = if (m/M < 0.5) use ( (m*M) / (M-m) ) else M
K = (Wo + M) / M
Ro = floor[ ( K * Ri ) - Wo ]
Go = floor[ ( K * Gi ) - Wo ]
Bo = floor[ ( K * Bi ) - Wo ]
这篇关于将RGBW颜色转换为标准RGB / HSB表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!