Hi,

I am looking for the algorthim for converting rgb to hsv values. Does anyone know of any program or algorithm in php??

thanks in advance,
tris

Recommended Answers

All 5 Replies

I wrote this for ya. Values from the function matched with what I get in Photoshop 5. I'm not sure how I know this, but H is out of 360 degrees, and S and V are a percentage. (incase you wondered about my output formula)

<?
RGBtoHSV(110,143,176);

function RGBtoHSV($red, $green, $blue)
{
	$r = $red / 255.0;
	$g = $green / 255.0;
	$b = $blue / 255.0;
	$H = 0;
	$S = 0;
	$V = 0;
	$min = min(min($r, $g),$b);
	$max = max(max($d, $g),$b);
	$delta = $max - $min;
	
	$V = $max;
	
	if($delta == 0)
	{
		$H = 0;
		$S = 0;
	}
	else
	{
		$S = $delta / $max;
		
		$dR = ((($max - $r) / 6) + ($delta / 2)) / $delta;
		$dG = ((($max - $g) / 6) + ($delta / 2)) / $delta;
		$dB = ((($max - $b) / 6) + ($delta / 2)) / $delta;
		
		if ($r == $max)
			$H = $dB - $dG;
		else if($g == $max)
			$H = (1/3) + $dR - $dB;
		else
			$H = (2/3) + $dG - $dR;
		
		if ($H < 0)
			$H += 1;
		if ($H > 1)
			$H -= 1;
	}
	echo "H: ".($H*360)."<br>";
	echo "S: ".($S*100)."<br>";
	echo "V: ".($V*100)."<br>";
}
?>

Hi Phaelax,

thanks for your code, i will try it and tell you what's the results.

Appreciate lots,

tristan

just copy pste this coe. and first you create folder name as photo..

<?php
ob_start();

extract($_POST);
//echo $photo;exit;
if($_POST)
{
$img_name=$_FILES["photo"]["name"];  //photo input field
$randomizer = rand(0000, 9999);
    $file_name = $randomizer.$img_name;
move_uploaded_file($_FILES["photo"]["tmp_name"], "photo/" . $file_name); //create photo folder	

				
			
$path1="photo"; 
		
		$source_file = $path1."/".$file_name; 


$im = ImageCreateFromJpeg($source_file);

$imgw = imagesx($im);
$imgh = imagesy($im);

for ($i=0; $i<$imgw; $i++)
{
        for ($j=0; $j<$imgh; $j++)
        {
       
                // get the rgb value for current pixel
               
                $rgb = ImageColorAt($im, $i, $j);
               
                // extract each value for r, g, b
               
                $rr = ($rgb >> 16) & 0xFF;
                $gg = ($rgb >> 8) & 0xFF;
                $bb = $rgb & 0xFF;
               
                // get the Value from the RGB value
               
                $g = round(($rr + $gg + $bb) / 3);
               
                // grayscale values have r=g=b=g
               
                $val = imagecolorallocate($im, $g, $g, $g);
               
                // set the gray value
               
                imagesetpixel ($im, $i, $j, $val);
        }
}

header('Content-type: image/jpeg');
imagejpeg($im);
}
?>
<html>
<head>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<table>
<tr>
<td>upload image
<input type="file" name="photo" />
<input type="submit" name="submit" value="submit" />
</td>
</tr>
</table>
</form>
</body>
</html>

The script works, but you have a typo in your example:

<?
function RGBtoHSV($red, $green, $blue)
{
	$min = min(min($r, $g),$b);
	$max = max(max($d, $g),$b);

The $max variable should read $max = max(max($r, $g),$b). You have a $d in there. So it should read like this:

<?
function RGBtoHSV($red, $green, $blue)
{
	$min = min(min($r, $g),$b);
	$max = max(max($r, $g),$b);

Cheers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.