Question

I am working with imagick extension from PHP, and i want get to the HEX Code from a ImagickPixel object, if i use getColorAsString the result is a srgb format color.

echo $pixel->getColorAsString();
//return srgb(62.128633554589150378%,78.681620508125433844%,74.308384832532240694%)

Any built-in function or a custom one, for cast this format to HEX Code?

Was it helpful?

Solution

Finally i made my own function:

The input is the result of getColorAsString methon from ImagickPixel. I cannot make this directly because the output of the method is in a 3rd party library

function iMagickColorToHEX($string)
{
    $pixel = new ImagickPixel($string);
    $color = $pixel->getColor();

    return sprintf('#%s%s%s', 
        dechex($color['r']), 
        dechex($color['g']),
        dechex($color['b'])
    );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top