Open In App

PHP | ImagickPixel setHSL() function

Improve
Improve
Like Article
Like
Save
Share
Report

The ImagickPixel::setHSL() function is an inbuilt function in PHP which is used to set the color described by the ImagickPixel object using normalized values for hue, saturation and luminosity.

Syntax:

bool ImagickPixel::setHSL( float $hue, float $saturation, float $luminosity )

Parameters:This function accepts three parameters as mentioned above and described below:

  • $hue: It specifies the normalized value for hue.
  • $saturation: It specifies the normalized value for saturation.
  • $luminosity: It specifies the normalized value for luminosity.

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the ImagickPixel::setHSL() function in PHP:

Program 1:




<?php
// Create a new imagickPixel object
$imagickPixel = new ImagickPixel();
  
// Set the HSL for pixel
$imagickPixel->setHSL(0.4, 0.4, 0.4);
  
// Get the HSL of pixel
$HSL = $imagickPixel->getHSL();
print("<pre>".print_r($HSL, true)."</pre>");
?>


Output:

Array
(
    [hue] => 0.40000158942082
    [saturation] => 0.4000152590219
    [luminosity] => 0.4
)

Program 2:




<?php
// Create a new imagick object
$imagick = new Imagick(
  
// Get the pixel iterator to iterate through each pixel
$imageIterator = $imagick->getPixelIterator();
  
// Loop through pixel rows
foreach ($imageIterator as $row => $pixels) {
    // Loop through the pixels in the row
    foreach ($pixels as $column => $pixel) {
  
        // Get the current HSL
        $HSL = $pixel->getHSL();
  
        // Set the HSL and change only hue
        $pixel->setHSL(0.6, $HSL['saturation'], $HSL['luminosity']);
  
    }
  
    // Sync the iterator after each iteration
    $imageIterator->syncIterator();
}
  
header("Content-Type: image/jpg");
echo $imagick;
?>


Output:

Reference: https://www.php.net/manual/en/imagickpixel.sethsl.php



Last Updated : 02 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads