Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RGB to HSV color in javascript? [closed]

Tags:

Does anyone know a function in javascript that converts RGB color to HSV color format?

(or jQuery)

like image 870
Alex Avatar asked Nov 05 '11 19:11

Alex


People also ask

How do you convert an RGB color to his HSV space?

Convert RGB Image to HSV Image Convert the image to the HSV color space. HSV = rgb2hsv(RGB); Process the HSV image. This example increases the saturation of the image by multiplying the S channel by a scale factor.

Is HSL the same as HSV?

The difference between HSL and HSV is that a color with maximum lightness in HSL is pure white, but a color with maximum value/brightness in HSV is analogous to shining a white light on a colored object (e.g. shining a bright white light on a red object causes the object to still appear red, just brighter and more ...

Which is better RGB or HSV?

The proposed algorithms using both RGB and HSV color space are able to detect the 3 standard types of colored images namely Red, Yellow and Blue. The experiment shows that the HSV color algorithm achieved better detection accuracy compared to RGB color space.


1 Answers

Here is a standalone function:

function rgb2hsv (r, g, b) {     let rabs, gabs, babs, rr, gg, bb, h, s, v, diff, diffc, percentRoundFn;     rabs = r / 255;     gabs = g / 255;     babs = b / 255;     v = Math.max(rabs, gabs, babs),     diff = v - Math.min(rabs, gabs, babs);     diffc = c => (v - c) / 6 / diff + 1 / 2;     percentRoundFn = num => Math.round(num * 100) / 100;     if (diff == 0) {         h = s = 0;     } else {         s = diff / v;         rr = diffc(rabs);         gg = diffc(gabs);         bb = diffc(babs);          if (rabs === v) {             h = bb - gg;         } else if (gabs === v) {             h = (1 / 3) + rr - bb;         } else if (babs === v) {             h = (2 / 3) + gg - rr;         }         if (h < 0) {             h += 1;         }else if (h > 1) {             h -= 1;         }     }     return {         h: Math.round(h * 360),         s: percentRoundFn(s * 100),         v: percentRoundFn(v * 100)     }; } 

And how to use it:

console.log( rgb2hsv(60, 120, 180) ); // {h: 210, s: 66.67, v: 70.59} 
like image 145
Mic Avatar answered Oct 11 '22 18:10

Mic