HSV To RGB

This algorithm converts HSV color model to RGB color model.



									class RGB
{
	public $R;
	public $G;
	public $B;
}

class HSV
{
	public $H;
	public $S;
	public $V;
}

function HSVToRGB($hsv) {
	$r = 0;
	$g = 0;
	$b = 0;

	if ($hsv->S == 0)
	{
		$r = $hsv->V;
		$g = $hsv->V;
		$b = $hsv->V;
	}
	else
	{
		if ($hsv->H == 360)
			$hsv->H = 0;
		else
			$hsv->H = $hsv->H / 60;

		$i = (int)floor($hsv->H);
		$f = $hsv->H - $i;

		$p = $hsv->V * (1.0 - $hsv->S);
		$q = $hsv->V * (1.0 - ($hsv->S * $f));
		$t = $hsv->V * (1.0 - ($hsv->S * (1.0 - $f)));

		switch ($i)
		{
		case 0:
			$r = $hsv->V;
			$g = $t;
			$b = $p;
			break;

		case 1:
			$r = $q;
			$g = $hsv->V;
			$b = $p;
			break;

		case 2:
			$r = $p;
			$g = $hsv->V;
			$b = $t;
			break;

		case 3:
			$r = $p;
			$g = $q;
			$b = $hsv->V;
			break;

		case 4:
			$r = $t;
			$g = $p;
			$b = $hsv->V;
			break;

		default:
			$r = $hsv->V;
			$g = $p;
			$b = $q;
			break;
		}

	}

	$rgb = new RGB();
	$rgb->R = floor($r * 255);
	$rgb->G = floor($g * 255);
	$rgb->B = floor($b * 255);

	return $rgb;
}
								


Example

									$data = new HSV();
$data->H = 154;
$data->S = 0.43;
$data->V = 0.60;

$value = HSVToRGB($data);
								


Output

									R: 87
G: 153
B: 124