imagecolorclosesthwb

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

imagecolorclosesthwb β€” ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ индСкс Ρ†Π²Π΅Ρ‚Π° с Π·Π°Π΄Π°Π½Π½Ρ‹ΠΌΠΈ Ρ‚ΠΎΠ½ΠΎΠΌ, Π±Π΅Π»ΠΈΠ·Π½ΠΎΠΉ ΠΈ Π·Π°Ρ‚Π΅ΠΌΠ½Π΅Π½ΠΈΠ΅ΠΌ

ОписаниС

function imagecolorclosesthwb(
Β Β Β Β GdImage $image,
Β Β Β Β int $red,
Β Β Β Β int $green,
Β Β Β Β int $blue
): int

Ѐункция ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ индСкс Ρ†Π²Π΅Ρ‚Π° с Π½Π°ΠΈΠ±ΠΎΠ»Π΅Π΅ Π±Π»ΠΈΠ·ΠΊΠΈΠΌΠΈ ΠΊ Π·Π°Π΄Π°Π½Π½ΠΎΠΌΡƒ Ρ†Π²Π΅Ρ‚Ρƒ значСниями Ρ‚ΠΎΠ½Π°, Π±Π΅Π»ΠΈΠ·Π½Ρ‹ ΠΈ затСмнСния.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

image
ΠžΠ±ΡŠΠ΅ΠΊΡ‚ GdImage, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Π΅Ρ€Π½ΡƒΠ»Π° функция imagecreatetruecolor() ΠΈΠ»ΠΈ другая функция Π³Π΅Π½Π΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ.
red

Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ красного ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Π° Ρ†Π²Π΅Ρ‚Π°.

green

Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π·Π΅Π»Ρ‘Π½ΠΎΠ³ΠΎ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Π° Ρ†Π²Π΅Ρ‚Π°.

blue

Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ синСго ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Π° Ρ†Π²Π΅Ρ‚Π°.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Π€ΡƒΠ½ΠΊΡ†ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ цСлочислСнный индСкс Ρ†Π²Π΅Ρ‚Π° с Π½Π°ΠΈΠ±ΠΎΠ»Π΅Π΅ Π±Π»ΠΈΠ·ΠΊΠΈΠΌΠΈ ΠΊ Π·Π°Π΄Π°Π½Π½ΠΎΠΌΡƒ Ρ†Π²Π΅Ρ‚Ρƒ значСниями Ρ‚ΠΎΠ½Π°, Π±Π΅Π»ΠΈΠ·Π½Ρ‹ ΠΈ затСмнСния.

Бписок измСнСний

ВСрсия ОписаниС
8.0.0 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ image Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ GdImage; Ρ€Π°Π½ΡŒΡˆΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π» ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Ρ‹ΠΉ gd-рСсурс (resource).

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ получСния индСкса Ρ†Π²Π΅Ρ‚Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagecolorclosesthwb()

<?php

$im
= imagecreatefromgif('php.gif');

echo
'HWB: ' . imagecolorclosesthwb($im, 116, 115, 152);

?>

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΡ…ΠΎΠΆ Π½Π°:

HWB: 33

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • imagecolorclosest() - ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ блиТайший индСкс Ρ†Π²Π΅Ρ‚Π°
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 1 note

up
7
Xong ΒΆ
16 years ago
I have written a function which uses the deltaE-function of the CMC.
It returns much better results than imagecolorclosestHWB() or imageColorClosest().

A test-script is online on http://www.fuhrpark-software.de/spielwiese/color.php?color=ffff00

<?php
/*
 * Returns the index of the palette-color which is most similar
 * to $givenColor.
 * 
 * $givenColor and the colors in $palette should be either
 * formatted as (#)rrggbb
 * (e. g. "ff0000", "4da4f3" or "#b5d7f3")
 * or arrays with values for red, green and blue
 * (e. g. $givenColor = array( 0xff, 0x00, 0x00 ) )
 *
 * References:
 * function rgb2lab
 *   - http://www.f4.fhtw-berlin.de/~barthel/ImageJ/ImageJ.htm
 * 
 * function rgb2lab & function deltaE
 *   - http://www.brucelindbloom.com
 */
function getNearestColor(
  $givenColor,
  $palette = array( 'blue' => '43aafd','red' => 'fe6256','green' => '64b949','yellow' => 'fcf357',
    'black' => '656565','white' => 'fdfdfd','orange' => 'fea800','purple' => '9773fe')
) {
  if(!function_exists('rgb2lab')) {
    function rgb2lab($rgb) {
      $eps = 216/24389; $k = 24389/27;
      // reference white D50
      $xr = 0.964221; $yr = 1.0; $zr = 0.825211;

      $rgb[0] = $rgb[0]/255; //R 0..1
      $rgb[1] = $rgb[1]/255; //G 0..1
      $rgb[2] = $rgb[2]/255; //B 0..1

      // assuming sRGB (D65)
      $rgb[0] = ($rgb[0] <= 0.04045)?($rgb[0]/12.92):pow(($rgb[0]+0.055)/1.055,2.4);
      $rgb[1] = ($rgb[1] <= 0.04045)?($rgb[1]/12.92):pow(($rgb[1]+0.055)/1.055,2.4);
      $rgb[2] = ($rgb[2] <= 0.04045)?($rgb[2]/12.92):pow(($rgb[2]+0.055)/1.055,2.4);

      // sRGB D50
      $x =  0.4360747*$rgb[0] + 0.3850649*$rgb[1] + 0.1430804 *$rgb[2];
      $y =  0.2225045*$rgb[0] + 0.7168786*$rgb[1] + 0.0606169 *$rgb[2];
      $z =  0.0139322*$rgb[0] + 0.0971045*$rgb[1] + 0.7141733 *$rgb[2];

      $xr = $x/$xr; $yr = $y/$yr; $zr = $z/$zr;

      $fx = ($xr > $eps)?pow($xr, 1/3):($fx = ($k * $xr + 16) / 116);
      $fy = ($yr > $eps)?pow($yr, 1/3):($fy = ($k * $yr + 16) / 116);
      $fz = ($zr > $eps)?pow($zr, 1/3):($fz = ($k * $zr + 16) / 116);

      $lab = array();
      $lab[] = round(( 116 * $fy ) - 16);
      $lab[] = round(500*($fx-$fy));
      $lab[] = round(200*($fy-$fz));      
      return $lab;
    }
  }
  
  if(!function_exists('deltaE')) {
    function deltaE($lab1, $lab2) {
      // CMC 1:1
      $l = 1; $c = 1;
      
      $c1 = sqrt($lab1[1]*$lab1[1]+$lab1[2]*$lab1[2]);
      $c2 = sqrt($lab2[1]*$lab2[1]+$lab2[2]*$lab2[2]);
      
      $h1 = (((180000000/M_PI) * atan2($lab1[1],$lab1[2]) + 360000000) % 360000000)/1000000;

      $t = (164 <= $h1 AND $h1 <= 345)?(0.56 + abs(0.2 * cos($h1+168))):(0.36 + abs(0.4 * cos($h1+35)));
      $f = sqrt(pow($c1,4)/(pow($c1,4) + 1900));
      
      $sl = ($lab1[0] < 16)?(0.511):((0.040975*$lab1[0])/(1 + 0.01765*$lab1[0]));
      $sc = (0.0638 * $c1)/(1 + 0.0131 * $c1) + 0.638;
      $sh = $sc * ($f * $t + 1 -$f);
      
      return sqrt(
        pow(($lab1[0]-$lab2[0])/($l * $sl),2) + 
        pow(($c1-$c2)/($c * $sc),2) + 
        pow(sqrt(
              ($lab1[1]-$lab2[1])*($lab1[1]-$lab2[1]) + 
              ($lab1[2]-$lab2[2])*($lab1[2]-$lab2[2]) + 
              ($c1-$c2)*($c1-$c2)
            )/$sh,2)
      );
    }
  }
  
  if(!function_exists('str2rgb')) {
    function str2rgb($str)
    {
      $str = preg_replace('~[^0-9a-f]~','',$str);
      $rgb = str_split($str,2);
      for($i=0;$i<3;$i++)
        $rgb[$i] = intval($rgb[$i],16);

      return $rgb;
    }
  }
  
  $givenColorRGB = is_array($givenColor?$givenColor:str2rgb($givenColor);

  $min = 0xffff;
  $return = NULL;
  
  foreach($palette as $key => $color) {
    $color = is_array($color)?$color:str2rgb($color);
    if($min >= ($deltaE = deltaE(rgb2lab($color),rgb2lab($givenColorRGB))))
    {
      $min = $deltaE;
      $return = $key;
    }
  }
  
  return $return;
}
?>