簡體   English   中英

將 RGB 轉換為 RGBW

[英]Converting RGB to RGBW

我知道這已經被問到了,但是那里給出的 anwser 不起作用。 我花了一個多小時尋找公式或算法,但一無所獲。 因此,我開始編寫自己的算法,以盡可能最有效的方式將 RGB 轉換為 RGBW。 這是我目前所擁有的:

        //'Ri', 'Gi', and 'Bi' correspond to the Red, Green, and Blue inputs.
        var M = Math.Max(Ri, Math.Max(Gi, Bi)); //The maximum value between R,G, and B.
        int Wo =0; //White output
        int Ro=0; //Red output
        int Go=0; //Green output
        int Bo=0; //Blue output

        int av = 0; //Average between the two minimum values
        int hR = 0; //Red with 100% hue
        int hG = 0; //Green with 100% hue
        int hB = 0; //Blue with 100% hue

        //These 4 lines serve to figure out what the input color is with 100% hue.
        float multiplier = 255.0f / M;
        hR = Convert.ToInt32(Ri * multiplier);
        hG = Convert.ToInt32(Gi * multiplier);
        hB = Convert.ToInt32(Bi * multiplier);

        //Depending on the maximum value, get an average of the least used colors, weighted for their importance in the overall hue.
        //This is the problematic part
        if (M == Ri)
           av = (Bi*hB + Gi*hG) / (hB+hG);
        else if (M == Gi)
            av = (Ri*hR + Bi*hB) / (hR+hB);
        else if (M == Bi)
            av = (Gi*hG + Ri*hR) / (hG+hR);

        //Set the rgbw colors
        Wo = av;
        Bo = Bi - av;
        Ro = Ri - av;
        Go = Gi - av;
        if (Wo < 1) Wo = 0;
        if (Bo < 1) Bo = 0;
        if (Ro < 1) Ro = 0;
        if (Go < 1) Go = 0;
        if (Wo > 255) Wo = 255;
        if (Bo > 255) Bo = 255;
        if (Ro > 255) Ro = 255;
        if (Go > 255) Go = 255;

如果我處理的顏色是原色,它就可以正常工作,但在任何其他情況下都不能。 什么使它在任何地方都能工作? 我是否在正確的軌道上?

編輯:這是我遇到的問題的 .gif。 RGBW 值一直在底部在此處輸入圖片說明

我終於弄清楚如何將 RGB 轉換為 RGBW,結果我以前的方法完全錯誤:

//Get the maximum between R, G, and B
float tM = Math.Max(Ri, Math.Max(Gi, Bi));

//If the maximum value is 0, immediately return pure black.
if(tM == 0)
   { return new rgbwcolor() { r = 0, g = 0, b = 0, w = 0 }; }

//This section serves to figure out what the color with 100% hue is
float multiplier = 255.0f / tM;
float hR = Ri * multiplier;
float hG = Gi * multiplier;
float hB = Bi * multiplier;  

//This calculates the Whiteness (not strictly speaking Luminance) of the color
float M = Math.Max(hR, Math.Max(hG, hB));
float m = Math.Min(hR, Math.Min(hG, hB));
float Luminance = ((M + m) / 2.0f - 127.5f) * (255.0f/127.5f) / multiplier;

//Calculate the output values
int Wo = Convert.ToInt32(Luminance);
int Bo = Convert.ToInt32(Bi - Luminance);
int Ro = Convert.ToInt32(Ri - Luminance);
int Go = Convert.ToInt32(Gi - Luminance);

//Trim them so that they are all between 0 and 255
if (Wo < 0) Wo = 0;
if (Bo < 0) Bo = 0;
if (Ro < 0) Ro = 0;
if (Go < 0) Go = 0;
if (Wo > 255) Wo = 255;
if (Bo > 255) Bo = 255;
if (Ro > 255) Ro = 255;
if (Go > 255) Go = 255;
return new rgbwcolor() { r = Ro, g = Go, b = Bo, w = Wo };

在此處輸入圖片說明

任何優化想法都非常受歡迎:)

我制定了一個算法,該算法考慮了“白色”LED 的色溫(因為這可能會因條帶而異 - 我的是暖白色,即 4500k)。 要點在這里,代碼在下面,還有更多上下文的博客文章是哦! 我的眼睛! Arduino WS2812B 設置和 RGBW 轉換(櫃燈 Pt. 3)

// Reference, currently set to 4500k white light:
// https://andi-siess.de/rgb-to-color-temperature/
const uint8_t kWhiteRedChannel = 255;
const uint8_t kWhiteGreenChannel = 219;
const uint8_t kWhiteBlueChannel = 186;

// The transformation has to be normalized to 255
static_assert(kWhiteRedChannel >= 255 ||
              kWhiteGreenChannel >= 255 ||
              kWhiteBlueChannel >= 255);

CRGBW GetRgbwFromRgb2(CRGB rgb) {
  uint8_t r = rgb.r;
  uint8_t g = rgb.g;
  uint8_t b = rgb.b;

  // These values are what the 'white' value would need to
  // be to get the corresponding color value.
  double whiteValueForRed = r * 255.0 / kWhiteRedChannel;
  double whiteValueForGreen = g * 255.0 / kWhiteGreenChannel;
  double whiteValueForBlue = b * 255.0 / kWhiteBlueChannel;

  // Set the white value to the highest it can be for the given color
  // (without over saturating any channel - thus the minimum of them).
  double minWhiteValue = min(whiteValueForRed,
                             min(whiteValueForGreen,
                                 whiteValueForBlue));
  uint8_t Wo = (minWhiteValue <= 255 ? (uint8_t) minWhiteValue : 255);

  // The rest of the channels will just be the original value minus the
  // contribution by the white channel.
  uint8_t Ro = (uint8_t)(r - minWhiteValue * kWhiteRedChannel / 255);
  uint8_t Go = (uint8_t)(g - minWhiteValue * kWhiteGreenChannel / 255);
  uint8_t Bo = (uint8_t)(b - minWhiteValue * kWhiteBlueChannel / 255);

  return CRGBW(Ro, Go, Bo, Wo);
}

我認為問題出在這里:

    if (M == Ri)
       av = (Bi*hB + Gi*hG) / (hB+hG);
    else if (M == Gi)
        av = (Ri*hR + Bi*hB) / (hR+hB);
    else if (M == Bi)
        av = (Gi*hG + Ri*hR) / (hG+hR);

你在這里做整數除法。 您的所有變量都是整數,因此除法將是整數除法。

    if (M == Ri)
       av = (int)((float)(Bi*hB + Gi*hG) / (hB+hG));
    else if (M == Gi)
        av = (int)((float)(Ri*hR + Bi*hB) / (hR+hB));
    else if (M == Bi)
        av = (int)((float)(Gi*hG + Ri*hR) / (hG+hR));

這將進行浮點除法,應該會為您提供所需的答案。 您仍然可能會發現有舍入錯誤 - 在這種情況下,將float更改為double

將乘數計算更改為浮動:

float multiplier = 255.0 / M;

只解決了一半的問題,但現在你的測試會導致另一個復雜問題:

if (M == Ri)
else if (M == Gi)
else if (M == Bi)

現在不太可能是真的。 您需要在測試中添加舍入誤差因子。

if (Math.Abs(M - Ri) < epsilon)
else if (Math.Abs(M - Gi) < epsilon)
else if (Math.Abs(M - Bi) < epsilon)

其中epsilon是一個合適的小值(通常我建議 10e-6,但您可以進行試驗。

此外,如果 M 與您未設置av任何 RGB 值不夠接近 - 它始終保持設置為零。 這也將為您提供您所看到的一切都是黑色的結果

我采取了不同的方法。 我只是檢查最低值並將其設為白色通道,然后從紅色、綠色和藍色通道中減去該值。 這有點簡化,但效果很好並且易於實現。 128,128,0 變為 128,128,0,0 64,64,128 變為 0,0,64,64 等等。這也允許輕松實現色彩校正。 當您構建 rgbw 值時,假設藍色有點強(典型)將藍色通道限制為較低的值,例如 250 而不是 255。 我不能說這在 C 或 C++ 中運行良好,但它在 ADA 中運行良好。

這個 repo 可以為你完成這項工作https : //github.com/iamh2o/rgbw_colorspace_converter/

我和一個朋友一起寫了這個模塊,這樣“顏色”對象可以通過幾種顏色系統實例化,並且對象可以將翻譯吐出到它支持的所有其他顏色系統——經過大量研究(一個關鍵部分是https://www.neltnerlabs.com/saikoled/how-to-convert-from-hsi-to-rgb-white ),我們終於搞定了 [HSI/HSL/HSV/RGB/HEX] -> RGBW 轉換。

有大量的軟件包可以解決一般的色彩空間問題,但似乎 RGBW 案例非常特定於物理照明/LED,不適用於數字顯示器,RGBW 未包含在我查看的任何模塊中。

這個模塊的殺手鐧是你實例化的顏色對象可以根據你的需要在幾個顏色系統中操作(你在不同的系統中創建它),並且它將保持對其他空間的所有翻譯都是最新的- 它非常快,我們還沒有讓它成為幀速率限制組件。

所以像這樣的事情將是一個循環通過完全明亮、完全飽和的彩虹(注意 RGB 與 HSV 代碼如何更不適合編程操作):

from rgbw_colorspace_converter.colors.converters import RGB

color = RGB(255,0,0)

ctr = 0

while ctr < 10:
     color.hsv_h += .1
     print(f"HSV:{color.hsv}  RGB:{color.rgb}  HSI:{color.hsi} HEX:{color.hex}")
     ctr += 1

# "H" in hsv is actually expressed in 360 degrees, and it is cylindrical. We've normalized it to being between 0-1 (so H=0=H=1 - both are red)
HSV:(0.0, 1.0, 1.0)  RGB:(255, 0, 0)  HSI:(0.0, 1.0, 0.33333) HEX:#ff0000
HSV:(0.1, 1.0, 1.0)  RGB:(255, 153, 0)  HSI:(36.0, 1.0, 0.533328) HEX:#ff9900
HSV:(0.2, 1.0, 1.0)  RGB:(203, 255, 0)  HSI:(72.23529411764707, 1.0, 0.5986868235294117) HEX:#cbff00
HSV:(0.3, 1.0, 1.0)  RGB:(51, 255, 0)  HSI:(108.0, 1.0, 0.399996) HEX:#33ff00
HSV:(0.4, 1.0, 1.0)  RGB:(0, 255, 102)  HSI:(144.0, 1.0, 0.46666199999999997) HEX:#00ff66
HSV:(0.5, 1.0, 1.0)  RGB:(0, 255, 255)  HSI:(180.0, 1.0, 0.66666) HEX:#00ffff
HSV:(0.6, 1.0, 1.0)  RGB:(0, 102, 255)  HSI:(216.0, 1.0, 0.46666199999999997) HEX:#0066ff
HSV:(0.7, 1.0, 1.0)  RGB:(50, 0, 255)  HSI:(251.76470588235296, 1.0, 0.39868882352941176) HEX:#3200ff
HSV:(0.8, 1.0, 1.0)  RGB:(204, 0, 255)  HSI:(288.0, 1.0, 0.599994) HEX:#cc00ff
HSV:(0.9, 1.0, 1.0)  RGB:(255, 0, 152)  HSI:(324.2352941176471, 1.0, 0.5320208235294118) HEX:#ff0098
HSV:(1.0, 1.0, 1.0)  RGB:(255, 0, 0)  HSI:(0.0, 1.0, 0.33333) HEX:#ff0000


# AND- you can access the other color systems with
# color.rgbw, color.hsi, color.hsl /// All of
# which change as the RGB or HSV properties are changed 
# (you can even change them in different spaces on the same object)

我可能在這里有點偏離,但作為一名電子工程師,我看到 LED 輸出光,然后編碼 XD。 無論如何,輸出白色可以通過“點亮”所有 rgb 或白色通道來完成,但保持相同的強度(從我的角度來看 LED 的健康)所有 4 個通道可以混合以輸出與過驅動白色相同的白色頻道。 我懷疑這在這一點上是否有任何問題,但稍后可能會有所幫助。

我在研究我自己的 RGB 到 RGBW 轉換時遇到了這個話題(這還不能工作:-),似乎我錯過了這個轉換中的一個重要因素,那就是白光 LED 的色溫。 “RGB-白色”的色溫可能是 6500(至少我遇到的所有 RGB LED 似乎都是如此),而且您似乎將白色 LED 通道視為它也具有該色溫,但是 4000 或 5000 對於 RGBW LED 來說更為常見......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM