Ad
Code
Diff
  • function rgbToHsv(rgb) {
      let [r, g, b] = rgb;
      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));
    }
    • function rgbToHsv(rgb) {
    • let r = rgb[0], g = rgb[1], b = rgb[2];
    • let [r, g, b] = rgb;
    • const v = Math.max(r, g, b) / 255;
    • if (v == 0) return Array.of(0, 0, 0);
    • 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);
    • 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;
    • 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));
    • }