Ad
Code
Diff
  • function rgbToHsv(rgb) {
      let r = rgb[0] / 255, g = rgb[1] / 255, b = rgb[2] / 255;
      const v = Math.max(r, g, b);
      const min = Math.min(r, g, b);
      const delta = v - min;
    
      const s = v === 0 ? 0 : delta / v;
    
      let h = 0;
      if (delta !== 0) {
        if (v === r) {
          h = (g - b) / delta + (g < b ? 6 : 0);
        } else if (v === g) {
          h = (b - r) / delta + 2;
        } else {
          h = (r - g) / delta + 4;
        }
        h /= 6;
        h *= 360;
      }
      return [Math.round(h), Math.round(s * 100), Math.round(v * 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 r = rgb[0] / 255, g = rgb[1] / 255, b = rgb[2] / 255;
    • const v = Math.max(r, g, b);
    • const min = Math.min(r, g, b);
    • const delta = v - min;
    • const s = v === 0 ? 0 : delta / v;
    • 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));
    • if (delta !== 0) {
    • if (v === r) {
    • h = (g - b) / delta + (g < b ? 6 : 0);
    • } else if (v === g) {
    • h = (b - r) / delta + 2;
    • } else {
    • h = (r - g) / delta + 4;
    • }
    • h /= 6;
    • h *= 360;
    • }
    • return [Math.round(h), Math.round(s * 100), Math.round(v * 100)];
    • }