[英]How to convert RGB565 to HSL Color in C
我是轉換圖像類型的新手。 我想確定屏幕上每個像素的顏色。 我可以從幀緩沖區中讀取顏色,但是它們都在RGB565中。 為了跟蹤某種顏色,我想將RGB565轉換為HSV,以便檢查色相。
我已經嘗試通過在線轉換器將其從RGB565轉換為RGB888。 例如RGB565'08F5'到RGB888'081BAA'。 然后從RGB888到HSL'233 91%35%'。 但是,我無法在C代碼中使用它。 顏色為十六進制格式,每2個存儲在一個寄存器中。 我為每種顏色制作了一個由四個組成的字符數組。
int colorcodes = IORD_ALTERA_AVALON_PIO_DATA(0x08000000 + 123204);
char colorcodesInHex[9];
snprintf(colorcodesInHex, 9, "%08x\n", colorcodes);
char firstColor[4];
char secondColor[4];
for(int i = 0; i <= 7; i++)
{
if(i <= 3)
{
firstColor[i] = colorcodesInHex[i];
}
else if (i >= 4 && i <= 7)
{
secondColor[i - 4] = colorcodesInHex[i];
}
}
有人知道如何將RGB565轉換為RGB888,然后再轉換為C中的HSL嗎?
int rgb565 = ...; // 16 bit value with rrrrrggggggbbbbb
double r = ((rgb565 >> 11) & 0x1F) / 31.0; // red 0.0 .. 1.0
double g = ((rgb565 >> 5) & 0x3F) / 63.0; // green 0.0 .. 1.0
double b = (rgb565 & 0x1F) / 31.0; // blue 0.0 .. 1.0
double cmax = max(r, max(g, b));
double cmin = min(r, min(g, b));
double delta = cmax - cmin;
// hue (in °)
double h_degrees = delta == 0.0 ? 0.0
: cmax == r ? 60 * (((g - b) / delta) % 6)
: cmax == g ? 60 * (((b - r) / delta + 2)
: /* cmax == b ? */ 60 * (((r - g) / delta + 4);
// saturation
double s = delta == 0.0 ? 0.0 : delta / (1.0 - abs(cmax + cmin - 1));
// lightness
double l = (cmax + cmin)/2;
RGB565是紅綠藍的16包裝。 上面是RGB565到HSL的轉換。
色相的度數為0°至360°
通過位移>>
提取紅色/綠色/藍色分量,然后縮放到0.0-1.0。
產生的亮度是不完美的平均值,即最小和最大顏色分量值的平均值。
色相是指色環中被三個RGB顏色划分的角度。
飽和度(灰色趨勢)由較小的增量確定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.