php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #71074 Wrong Hue value
Submitted: 2015-12-09 11:50 UTC Modified: 2015-12-29 21:14 UTC
From: vadimx at gmail dot com Assigned: danack (profile)
Status: Assigned Package: imagick (PECL)
PHP Version: 5.5.30 OS: Windows 7
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-12-09 11:50 UTC] vadimx at gmail dot com
Description:
------------
ImagickPixel:getHSL, has wrong Hue value


Test script:
---------------
$p = new ImagickPixel();
$p->setColor('rgb(158,146,130)');
print_r($p->getHSL());


Expected result:
----------------
Array
(
    [hue] => 0.34285714285714
    [saturation] => 0.12612612612613
    [luminosity] => 0.56470588235294
)

Actual result:
--------------
Array
(
    [hue] => 0.095238095238095
    [saturation] => 0.12612612612613
    [luminosity] => 0.56470588235294
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-09 15:11 UTC] danack@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: danack
 [2015-12-09 15:11 UTC] danack@php.net
I was going to ask you how you were calculating that the values were 'wrong'. However I realised that doing the conversion RGB -> HSL -> RGB gives different RGB color than the starting one.....e.g. the code below outputs:

InputColor: rgb(158, 146, 130)
FinalColor: rgb(158.00, 141.00, 130.00)


This is very likely going to be an issue with the underlying ImageMagick library, which I will need to investigate and report upstream.


$inputColor = "rgb(158, 146, 130)";

$fromRGB = new ImagickPixel($inputColor);
$hsl = $fromRGB->getHSL();

$hslString = sprintf(
	"hsl(%.4f, %.4f, %.4f)", 
	$hsl['hue'] * 255,
	$hsl['saturation'] * 255,
	$hsl['luminosity'] * 255
);

$fromHSL = new ImagickPixel($hslString);
$outputColorArray = $fromHSL->getColor(false);
$finalColor = sprintf(
    "rgb(%.2f, %.2f, %.2f)", 
    $outputColorArray['r'],
    $outputColorArray['g'],
    $outputColorArray['b']
);

echo "hslString is $hslString\n";

echo "InputColor: ".$inputColor."\n";
echo "FinalColor: ".$finalColor."\n";
 [2015-12-09 15:35 UTC] danack@php.net
Reported upstream as: https://github.com/ImageMagick/ImageMagick/issues/64
 [2015-12-29 21:14 UTC] danack@php.net
-Type: Bug +Type: Documentation Problem
 [2015-12-29 21:14 UTC] danack@php.net
So, it's not a bug.

The values are all normalized 0 - 1.0. Most HSL converters don't normalize the hue to that range, and instead have 0-360 or 0-3.6. 

To get your expected result you need to multiply by 3.6
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC