Название основного цвета значения RGB
Я хочу найти название цвета, учитывая его значение RGB.
Пример значения RGB: (237, 65, 83)
Предопределенные значения
массив (11, 'Красный', '# FF0000', '255,0,0'),
массив (3, «Коричневый», «# A52A2A», «165,42,42»)
Если я использую этот методрасчет расстояния
я получаю цвет как коричневый.
Но фактический цвет красный, если мы проверим это значение RGBВот
Отредактировано 1
<?php
$colors = array(
array(1, 'Black', '#000000', '0,0,0'),
array(2, 'Blue', '#0000FF', '0,0,255'),
array(3, 'Brown', '#A52A2A', '165,42,42'),
array(4, 'Cream', '#FFFFCC', '255,255,204'),
array(5, 'Green', '#008000', '0,128,0'),
array(6, 'Grey', '#808080', '128,128,128'),
array(7, 'Yellow', '#FFFF00', '255,255,0'),
array(8, 'Orange', '#FFA500', '255,165,0'),
array(9, 'Pink', '#FFC0CB', '255,192,203'),
array(11, 'Red', '#FF0000', '255,0,0'),
array(10, 'Purple', '#800080', '128,0,128'),
array(12, 'Tan', '#d2b48c', '210,180,140'),
array(13, 'Turquoise', '#40E0D0', '64,224,208'),
array(14, 'White', '#FFFFFF', '255,255,255')
);
$miDist = 99999999999999999 ;
$loc = 0 ;
$findColor = RGBtoHSV(72, 70, 68);
for( $i = 0 ; $i < 14 ; $i++){
$string = $colors[$i][3];
$pieces = explode(',' , $string);
$r0 = $pieces[0];
$g0 = $pieces[1];
$b0 = $pieces[2];
$storedColor = RGBtoHSV($r0,$g0,$b0);
echo $storedColor[0] ."-" . $storedColor[1] ;
// distance between colors (regardless of intensity)
$d = sqrt( ($storedColor[0]-$findColor[0])
*($storedColor[0]-$findColor[0])
+
($storedColor[1]-$findColor[1])
*($storedColor[1]-$findColor[1])
);
echo $colors[$i][1] ."=" .$d;
//echo $d ;
if( $miDist >= $d )
{
$miDist = $d;
$loc = $i ;
}
echo "<br>" ;
}
echo $colors[$loc][1];
function RGBtoHSV($R, $G, $B) // RGB values: 0-255, 0-255, 0-255
{ // HSV values: 0-360, 0-100, 0-100
// Convert the RGB byte-values to percentages
$R = ($R / 255);
$G = ($G / 255);
$B = ($B / 255);
// Calculate a few basic values, the maximum value of R,G,B, the
// minimum value, and the difference of the two (chroma).
$maxRGB = max($R, $G, $B);
$minRGB = min($R, $G, $B);
$chroma = $maxRGB - $minRGB;
// Value (also called Brightness) is the easiest component to calculate,
// and is simply the highest value among the R,G,B components.
// We multiply by 100 to turn the decimal into a readable percent value.
$computedV = 100 * $maxRGB;
// Special case if hueless (equal parts RGB make black, white, or grays)
// Note that Hue is technically undefined when chroma is zero, as
// attempting to calculate it would cause division by zero (see
// below), so most applications simply substitute a Hue of zero.
// Saturation will always be zero in this case, see below for details.
if ($chroma == 0)
return array(0, 0, $computedV);
// Saturation is also simple to compute, and is simply the chroma
// over the Value (or Brightness)
// Again, multiplied by 100 to get a percentage.
$computedS = 100 * ($chroma / $maxRGB);
// Calculate Hue component
// Hue is calculated on the "chromacity plane", which is represented
// as a 2D hexagon, divided into six 60-degree sectors. We calculate
// the bisecting angle as a value 0 <= x < 6, that represents which
// portion of which sector the line falls on.
if ($R == $minRGB)
$h = 3 - (($G - $B) / $chroma);
elseif ($B == $minRGB)
$h = 1 - (($R - $G) / $chroma);
else // $G == $minRGB
$h = 5 - (($B - $R) / $chroma);
// After we have the sector position, we multiply it by the size of
// each sector's arc (60 degrees) to obtain the angle in degrees.
$computedH = 60 * $h;
return array($computedH, $computedS, $computedV);
}
?>