HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelDevelopmentTutorial Source codeReport an issue
Category
Example

set HSL

← setColorValueQuantum  
Sets the color described by the ImagickPixel object using normalized values for hue, saturation and luminosity.
This example creates a red color, rotates the hue by 180 degrees and sets the new color.

Original color

r : 230
g : 26
b : 26
a : 1

Rotated color

r : 25
g : 230
b : 230
a : 1

Example

        $output = "This example creates a red color, rotates the hue by 180 degrees and sets the new color.<br/>";

        //Create an almost pure red color
        $color = new \ImagickPixel('rgb(90%, 10%, 10%)');
        $originalColor = clone $color;

        //Get it's HSL values
        $colorInfo = $color->getHSL();

        //Rotate the hue by 180 degrees
        $newHue = $colorInfo['hue'] + 0.5;
        if ($newHue > 1) {
            $newHue = $newHue - 1;
        }

        //Set the ImagickPixel to the new color
        $color->setHSL($newHue, $colorInfo['saturation'], $colorInfo['luminosity']);

        $output .= "<h3>Original color</h3>";
        $colorInfo = $originalColor->getcolor();
        foreach ($colorInfo as $key => $value) {
            $output .= "$key : $value <br/>";
        }

        $output .= "<h3>Rotated color</h3>";
        //Check that the new color is blue/green
        $colorInfo = $color->getcolor();
        foreach ($colorInfo as $key => $value) {
            $output .= "$key : $value <br/>";
        }

        return $output;