Program to Change RGB color model to HSV color model | javascript program change RGB Color Model to HSV Color Model ; R , G , B values are divided by 255 to change the range from 0. .255 to 0. .1 ; h , s , v = hue , saturation , value var cmax = Math . max ( r , Math . max ( g , b ) ) ; maximum of r , g , b var cmin = Math . min ( r , Math . min ( g , b ) ) ; minimum of r , g , b var diff = cmax - cmin ; diff of cmax and cmin . ; if cmax and cmax are equal then h = 0 ; if cmax equal r then compute h ; if cmax equal g then compute h ; if cmax equal b then compute h ; if cmax equal zero ; compute v ; rgb_to_hsv ( 45 , 215 , 0 ) ; rgb_to_hsv ( 31 , 52 , 29 ) ;
function rgb_to_hsv ( r , g , b ) { r = r / 255.0 ; g = g / 255.0 ; b = b / 255.0 ; var h = - 1 , s = - 1 ; if ( cmax == cmin ) h = 0 ; else if ( cmax == r ) h = ( 60 * ( ( g - b ) / diff ) + 360 ) % 360 ; else if ( cmax == g ) h = ( 60 * ( ( b - r ) / diff ) + 120 ) % 360 ; else if ( cmax == b ) h = ( 60 * ( ( r - g ) / diff ) + 240 ) % 360 ; if ( cmax == 0 ) s = 0 ; else s = ( diff / cmax ) * 100 ; var v = cmax * 100 ; document . write ( " " + h . toFixed ( 1 ) + " " + s + " " + v + " " ) ; } rgb_to_hsv ( 129 , 88 , 47 ) ;