RGB to HSV Color Conversion Algorithm

Solution 1:

$CMax$ is the largest of $R,G,$ and $B$. $CMin$ is the smallest.

$(\mod 6)$ is the remainder after dividing by $6$. (% operator in C-ish languages)

The commas look to be conditional statements. (e.g. $H = 60 ^\circ ({{G' -B' \over \Delta} \mod 6)} $ if $CMax = R'$)

$<>$ means 'not equal to'.

Solution 2:

Well, I have been searching for the same algorithm for months! I actually did not get my algorithm from Wikipedia, I got it from GitHub$^1$. Anyways, here is my formula:
$$r,g,b = \frac{r'}{255},\frac{g'}{255},\frac{b'}{255}$$ $$M = \max(r,g,b)$$ $$m = \min(r,g,b)$$ $$c = M - m$$ $$s = (\frac{c}{M})100$$ $$R, G, B = \frac{M-r}{c},\frac{M-g}{c},\frac{M-b}{c}$$
$$h' = $$ $0$: if M = m
$0+B-G$: if M = r
$2+R-B$: if M = g
$4+G-R$: if M = b

$$h = (\frac{h'}{6}\mod{1})360$$ $$v = M100$$



$^1$: https://github.com/python/cpython/blob/3.9/Lib/colorsys.py



This is my code version (python):

def rgb_to_hsv(r, g, b):
  r /= 255
  g /= 255
  b /= 255
  maxc = max(r, g, b)
  minc = min(r, g, b)
  v = maxc
  if minc == maxc:
      return 0.0, 0.0, v
  s = (maxc-minc) / maxc
  rc = (maxc-r) / (maxc-minc)
  gc = (maxc-g) / (maxc-minc)
  bc = (maxc-b) / (maxc-minc)
  if r == maxc:
      h = 0.0+bc-gc
  elif g == maxc:
      h = 2.0+rc-bc
  else:
      h = 4.0+gc-rc
  h = (h/6.0) % 1.0
  return h * 360, s * 100, v * 100