LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

understanding labview RGB to HSL conversation

Hello,

 

I know that labview uses the RGB and HSV values  in range from 0-255 and not in degrees or percents. On the net I found some code examples  RGB to HSV conversations. Implemented it in Matlab and compared it with the VI IMAQ RGBTocolor 2 function. The Saturation and Hue parameter did match , but not the Lightness parameter which is the easiest one to calculate. This Why is the L different? What does Labview do instead?

 

I used this code for the Matlab implementation, but made some modifications:

 

/**
 * Converts an RGB color value to HSL. Conversion formula
 * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
 * Assumes r, g, and b are contained in the set [0, 255] and
 * returns h, s, and l in the set [0, 1].
 *
 * @param   Number  r       The red color value
 * @param   Number  g       The green color value
 * @param   Number  b       The blue color value
 * @return  Array           The HSL representation
 */
function rgbToHsl(r, g, b){
    r /= 255, g /= 255, b /= 255;
    var max = Math.max(r, g, b), min = Math.min(r, g, b);
    var h, s, l = (max + min) / 2;

    if(max == min){
        h = s = 0; // achromatic
    }else{
        var d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
        switch(max){
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
            case g: h = (b - r) / d + 2; break;
            case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
    }

    return [h, s, l];
}

Here is the screenshoot, lvL means labview L(ightness) value:

Unbenannt.PNG

 

Thanks for the help

0 Kudos
Message 1 of 4
(4,758 Views)

Hello D_1,

 

Based on the following Wikipedia entry your Matlab code calculates the I parameter of HSI representation, not the L one from HSL. If you set the Color Mode input of the VI to HSI you will get the same result as the Matlab code.

 

Best regards,

 

Daniel

Dániel Fülöp
Field Application Engineer (CLA, CTA)
National Instruments
0 Kudos
Message 2 of 4
(4,729 Views)

Hello Daniel_Fulop,

 

thanks for the help. I have changed it and now all the parameter mismatch 🙂 But when I try the RGB to HSL converter on the net the values H, S and L match. strange, like Labview uses their own formula. I have added a screen. 

 

0 Kudos
Message 3 of 4
(4,712 Views)

Hello D_1,

 

the explanation in this case is the same as the previous one. If you use HSI conversion mode in the LabVIEW VI and convert the 0...255 outputs of the VI to degrees and percentages, you will get similar results to the web based converter.

 

Regards,

 

Daniel

Dániel Fülöp
Field Application Engineer (CLA, CTA)
National Instruments
0 Kudos
Message 4 of 4
(4,682 Views)