Reputation: 21
I'm looking for fast algorithms (typically using integers only) for converting RGB data to HSL data and then converting back to RGB data. I've found algorithms (wikipedia) for these conversions but they are not fast as they requiere floating operations.
Does anyone know efficient algorithms that use integers only ?
Upvotes: 2
Views: 1685
Reputation: 1963
Almost 5 years and no answer? Well, here's 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;
}
Upvotes: 3