function XYZ2RGB($xyz) { $x = $xyz['X']; $y = $xyz['Y']; $z = $xyz['Z']; if ($x > 1) { $x = 1; } if ($x < 0) { $x = 0; } if ($y > 1) { $y = 1; } if ($y < 0) { $y = 0; } if ($z > 1) { $z = 1; } if ($z < 0) { $z = 0; } if ($y >= 1) { $r = $g = $b = 1; } else { $r = 3.240479 * $x - 1.53715 * $y - 0.498535 * $z; $g = -0.969256 * $x + 1.875991 * $y + 0.041556 * $z; $b = 0.055648 * $x - 0.204043 * $y + 1.057311 * $z; } $rgb = array(); $linearRGB = array('R' => $r, 'G' => $g, 'B' => $b); // for ($k=0;$k<count($linearRGB);$k++) { foreach ($linearRGB as $k => $v) { // $v=$linearRGB[$i]; if ($v <= 0.0031308) { $v *= 12.92; } else { $v = pow($v, 1 / 2.4) * (1 + 0.055) - 0.055; } $v *= 255; $rgb[$k] = floor($v); } $rgb['color'] = RGB2ColorCode($rgb['R'], $rgb['G'], $rgb['B']); return $rgb; }
function XYZ2RGB($xyz) { list($x, $y, $z) = $xyz; if ($x > 1) { $x = 1; } if ($x < 0) { $x = 0; } if ($y > 1) { $y = 1; } if ($y < 0) { $y = 0; } if ($z > 1) { $z = 1; } if ($z < 0) { $z = 0; } if ($y >= 1) { $r = $g = $b = 1; } else { $r = 3.240479 * $x - 1.53715 * $y - 0.498535 * $z; $g = -0.969256 * $x + 1.875991 * $y + 0.041556 * $z; $b = 0.055648 * $x - 0.204043 * $y + 1.057311 * $z; } $rgb = array(); $linearRGB = array($r, $g, $b); for ($i = 0; $i < count($linearRGB); $i++) { $c = $linearRGB[$i]; if ($c <= 0.0031308) { $c *= 12.92; } else { $c = pow($c, 1 / 2.4) * (1 + 0.055) - 0.055; } $c *= 255; $rgb[$i] = floor($c); } $rgb['color'] = RGB2ColorCode($rgb[0], $rgb[1], $rgb[2]); return $rgb; }