mam tabele z kolorami kolorami w RGB (kolumna R, kolumna G, kolumna

<?php // sortuje kolory wg podobienstwa $colors = generateColors( 100 ); ); $sortedColors = sortColors( $colors ); printColors( $sortedColors ); function generateColors( $n ) { $min = 0; $max = 255; for ( $i = 0; $i < $n; $i++ ) { $colors[] = $color; } return $colors; } function sortColors( $colors ) { { // last color // find closest one $nextIndex = findClosest( $colors, $last ); $next = $colors[ $nextIndex ]; // remove it from colors // add to sorted colors $sorted[] = $next; } return $sorted; } function findClosest( $colors, $c ) { // first as best $bestDifference = calculateDifference( $c, $best ); // search better foreach ( $colors as $i => $v ) { $newDiff = calculateDifference( $c, $v ); if ( $newDiff < $bestDifference ) { $best = $v; $bestIndex = $i; $bestDifference = $newDiff; } } return $bestIndex; } function calculateDifference( $from, $to ) { $sum = 0; $pow = 10; $sum += pow( $from[0] - $to[0], $pow ); $sum += pow( $from[1] - $to[1], $pow ); $sum += pow( $from[2] - $to[2], $pow ); return $sum; } function printColors( $colors ) { foreach ( $colors as $color ) { printf( '<div style="background-color: rgb( %d, %d, %d ); width: 200px; height: 5px;"></div>', $color[0], $color[1], $color[2] ); } } ?>