Ad

function rgbToHsv(rgb) {

let red = rgb[0], green = rgb[1], blue = rgb[2];
const hsvValue = Math.max(red, green, blue) / 255;

if (hsvValue == 0) return Array.of(0, 0, 0);
red /= hsvValue;
green /= hsvValue;
blue /= hsvValue;
const saturation = 1 - Math.min(red, green, blue) / 255;
if (saturation == 0) return Array.of(0, 0, hsvValue * 100);
red = 255 - (255 - r) / saturation;
green = 255 - (255 - g) / saturation;
blue = 255 - (255 - b) / saturation;
const peak = Math.max(red, green, blue);
let hue = 0;
if (red == peak) hue = green < blue ? 360 - blue * 60 / 255 : green * 60 / 255;
else if (green == peak) hue = red > blue ? 120 - red * 60 / 255 : 120 + blue * 60 / 255;
else hue = red > green ? 240 + red * 60 / 255 : 240 - green * 60 / 255;
return Array.of(Math.round(hue), Math.round(saturation * 100), Math.round(hsvValue * 100));
}

Code
Diff
  • function rgbToHsv(rgb) {
    
      let red = rgb[0], green = rgb[1], blue = rgb[2];
      const hsvValue = Math.max(red, green, blue) / 255;
      
      if (hsvValue == 0) return Array.of(0, 0, 0);
    
      red /= hsvValue;
      green /= hsvValue;
      blue /= hsvValue;
    
      const saturation = 1 - Math.min(red, green, blue) / 255;
    
      if (saturation == 0) return Array.of(0, 0, hsvValue * 100);
    
      red = 255 - (255 - red) / saturation;
      green = 255 - (255 - green) / saturation;
      blue = 255 - (255 - blue) / saturation;
      const peak = Math.max(red, green, blue);
      let hue = 0;
      
      if (red == peak) hue = green < blue ? 360 - blue * 60 / 255 : green * 60 / 255;
      else if (green == peak) hue = red > blue ? 120 - red * 60 / 255 : 120 + blue * 60 / 255;
      else hue = red > green ? 240 + red * 60 / 255 : 240 - green * 60 / 255;
      return Array.of(Math.round(hue), Math.round(saturation * 100), Math.round(hsvValue *  100));
    }
    • function rgbToHsv(rgb) {
    • let r = rgb[0], g = rgb[1], b = rgb[2];
    • const v = Math.max(r, g, b) / 255;
    • if (v == 0) return Array.of(0, 0, 0);
    • r /= v;
    • g /= v;
    • b /= v;
    • const s = 1 - Math.min(r, g, b) / 255;
    • if (s == 0) return Array.of(0, 0, v * 100);
    • r = 255 - (255 - r) / s;
    • g = 255 - (255 - g) / s;
    • b = 255 - (255 - b) / s;
    • const peak = Math.max(r, g, b);
    • let h = 0;
    • if (r == peak) h = g < b ? 360 - b * 60 / 255 : g * 60 / 255;
    • else if (g == peak) h = r > b ? 120 - r * 60 / 255 : 120 + b * 60 / 255;
    • else h = r > g ? 240 + r * 60 / 255 : 240 - g * 60 / 255;
    • return Array.of(Math.round(h), Math.round(s * 100), Math.round(v * 100));
    • let red = rgb[0], green = rgb[1], blue = rgb[2];
    • const hsvValue = Math.max(red, green, blue) / 255;
    • if (hsvValue == 0) return Array.of(0, 0, 0);
    • red /= hsvValue;
    • green /= hsvValue;
    • blue /= hsvValue;
    • const saturation = 1 - Math.min(red, green, blue) / 255;
    • if (saturation == 0) return Array.of(0, 0, hsvValue * 100);
    • red = 255 - (255 - red) / saturation;
    • green = 255 - (255 - green) / saturation;
    • blue = 255 - (255 - blue) / saturation;
    • const peak = Math.max(red, green, blue);
    • let hue = 0;
    • if (red == peak) hue = green < blue ? 360 - blue * 60 / 255 : green * 60 / 255;
    • else if (green == peak) hue = red > blue ? 120 - red * 60 / 255 : 120 + blue * 60 / 255;
    • else hue = red > green ? 240 + red * 60 / 255 : 240 - green * 60 / 255;
    • return Array.of(Math.round(hue), Math.round(saturation * 100), Math.round(hsvValue * 100));
    • }