<A HREF="COL_.htm"><img align=center src="contents.gif" ALT="Contents"></A> Up Previous Next


HSV: Hue Saturation Value.

RGB to HSV (Travis)

  Given RGB values, find the max and min.

  V = max
  S = (max-min) / max

  If S = 0, H is undefined
  else
    R1 = (max-R) / (max-min)
    G1 = (max-G) / (max-min)
    G2 = (max-B) / (max-min)

    if 		R = max and G = min, 	H = 5 + B1
    else if   	R = max and G not= min, H = 1 - G1
    else if   	G = max and B = min,	H = R1 + 1
    else if   	G = max and B not=main,	H = 3 - B1
    else if   	R = max,		H = 3 + G1
    else				H = 5 - R1

    H = H*60 (converts to degrees so S and V lie between 0
    and 1, H between 0 and 360)
HSV to RGB (Travis)
  Convert H degrees to a hexagon section
    hex = H / 360

  main_colour = int(hex)
  sub_colour  = hex - main_colour
  var1 = (1-S)*V
  var2 = (1 -(S * sub_colour)) * V
  var3 = (1 -(S * (1 - sub_colour))) * V

  then

    if main_colour = 0, R = V, G = var3, B = var1
       main_colour = 1, R = var2, G = V, B = var1
       main_colour = 2, R = var1, G = V, B = var3
       main_colour = 3, R = var1, G = var2, B = V 
       main_colour = 4, R = var3, G = var1, B = V
       main_colour = 5, R = V, G = var1, B = var2

where int(x) converts x to an integer value.

(N.B. I haven't implemented this transform.)

RGB to HSV (Foley and VanDam)

  max = maximum of RGB
  min = minimum of RGB

  V = max
  S = (max - min) / max

  if S = 0, H is undefined, else
    delta = max-min

    if R = max, H = (G-b)/delta
    if G = max, H = 2 + (B-R)/delta
    if B = max, H = 4 + (R-G)/delta

    H = H*60
    if H < 0, H = H + 360
HSV to RGB (Foley and VanDam)
  if S = 0 and H = undefined, R = G = B = V

  if H = 360, H = 0
  H = H / 60
  i = floor(H)
  f = H - i
  p = V*(1-S)
  q = V*(1-(S*f))
  t = V*(1 - (S * (1-f)))

  if i = 0, R = v, G = t, B = p
  if i = 1, R = q, G = v, B = p
  if i = 2, R = p, G = v, B = t
  if i = 3, R = p, G = q, B = v
  if i = 4, R = t, G = p, B = v
  if i = 5, R = v, G = p, B = q

where floor is the C floor function.