C481 - B581 Computer Graphics

Homework 1 - Solution

Ex. 1. Pseudocode function to convert a color from RGB notation to HSV.

RGB_to_HSV(r, g, b, &hue, &saturation, &value)
{
  dominant = max(r, g, b);
  least = min (r, g, b);
 
  if (dominant == 0)
    hue = saturation = value = 0;
  else {
    if (dominant == r) {
      hue = 0 + 60 * (g - b) / (dominant - least);
      if (hue < 0)
        hue += 360;
    }
    else if (dominant == g)
      hue = 120 + 60 * (b - r) / (dominant - least);
    else
      hue = 240 + 60 * (r - g) / (dominant - least);
 
    saturation = 1 - least / dominant;
    value = dominmant;
  }
}