import { MAX_COLOR_RGB } from './consts.js';

function rgb2hsv(r, g, b) {
  var h = NaN;
  var max = Math.max(r, g, b);
  var min = Math.min(r, g, b);
  var delta = max - min;
  if (delta === 0) {
    h = 0;
  } else if (r === max) {
    h = (g - b) / delta % 6;
  } else if (g === max) {
    h = (b - r) / delta + 2;
  } else if (b === max) {
    h = (r - g) / delta + 4;
  }
  h = Math.round(h * 60);
  if (h < 0) {
    h += 360;
  }
  var s = Math.round((max === 0 ? 0 : delta / max) * 100);
  var v = Math.round(max / MAX_COLOR_RGB * 100);
  return { h: h, s: s, v: v };
}

export { rgb2hsv };
