我正在寻找快速算法(通常仅使用整数)将RGB数据转换为HSL数据,然后转换回RGB数据。我为这些转换找到了算法(Wikipedia),但是它们在要求浮动操作时并不快。
有人知道仅使用整数的有效算法吗?
将近5年,没有答案?好吧,这是50%:HSL-> RGB
uint32_t hsl2rgb(uint8_t h, uint8_t s, uint8_t l) {
uint8_t r, g, b, lo, c, x, m;
uint16_t h1, l1, H;
l1 = l + 1;
if (l < 128) {
c = ((l1<<1) * s) >> 8;
}
else {
c = (512 - (l1<<1)) * s >> 8;
}
H = h*6; // 0 to 1535 (actually 1530)
lo = H & 255; // Low byte = primary/secondary color mix
h1 = lo + 1;
if ((H & 256) == 0) { // even sextant, like red to yellow
x = h1 * c >> 8;
}
else { // odd sextant, like yellow to green
x = (256 - h1) * c >> 8;
}
m = l - (c >> 1);
switch(H >> 8) { // High byte = sextant of colorwheel
case 0 : r = c ; g = x ; b = 0 ; break; // R to Y
case 1 : r = x ; g = c ; b = 0 ; break; // Y to G
case 2 : r = 0 ; g = c ; b = x ; break; // G to C
case 3 : r = 0 ; g = x ; b = c ; break; // C to B
case 4 : r = x ; g = 0 ; b = c ; break; // B to M
default: r = c ; g = 0 ; b = x ; break; // M to R
}
return r+m<<16 | g+m<<8 | b+m;
}