當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP imagecolorclosesthwb()用法及代碼示例

imagecolorclosesthwb()函數是PHP中的一個內置函數,用於獲取給定圖像中顏色,白色和黑色的索引。此函數返回帶有顏色索引的整數值,該顏色索引包含最接近給定顏色的色相,白色和黑色。

用法:

int imagecolorclosesthwb ( $image, $red, $green, $blue )

參數:該函數接受上述和以下所述的四個參數:


  • $image:它由圖像創建函數之一(例如imagecreatetruecolor())返回。它用於創建圖像的尺寸。
  • $red:此參數用於設置紅色分量的值。
  • $green:此參數用於設置綠色分量的值。
  • $blue:此參數用於設置藍色分量的值。

返回值:此函數返回整數值,其色相,白色和黑色度的索引最接近圖像中的給定顏色。

以下示例程序旨在說明PHP中的imagecolorclosesthwb()函數。

程序1:

<?php 
  
// Create new image from given URL 
$image = imagecreatefromgif( 
'https://media.geeksforgeeks.org/wp-content/uploads/animateImages.gif'); 
  
// Display the index of color 
echo 'Hue White and Blackness closest color index: ' 
    . imagecolorclosesthwb($image, 5, 165, 10); 
  
imagedestroy($image); 
?>

輸出:

Hue White and Blackness closest color index: 42

程序2:

<?php 
  
// Create new image from given URL 
$image = imagecreatefromgif( 
'https://media.geeksforgeeks.org/wp-content/uploads/animateImages.gif'); 
  
// Array contains rgb color value 
$color = array( 
    array(7, 150, 0), 
    array(53, 190, 255), 
    array(255, 165, 54) 
); 
  
// Loop to find closest HWB color 
foreach($color as $id => $rgb) 
{ 
    echo 'Hue White and Blackness closest color index: '
    . imagecolorclosesthwb($image, $rgb[0], $rgb[1], $rgb[2]) . "<br>"; 
} 
  
imagedestroy($image); 
?>

輸出:

Hue White and Blackness closest color index: 42
Hue White and Blackness closest color index: 101
Hue White and Blackness closest color index: 186

相關文章:

參考: http://php.net/manual/en/function.imagecolorclosesthwb.php



相關用法


注:本文由純淨天空篩選整理自Mahadev99大神的英文原創作品 PHP | imagecolorclosesthwb() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。