<?php

declare(strict_types=1);

$path = __DIR__ . '/../assets/img/karvi-logo.png.png';
$img = imagecreatefrompng($path);
$w = imagesx($img);
$h = imagesy($img);

$samples = [];
for ($y = (int) ($h * 0.2); $y < (int) ($h * 0.8); $y += 8) {
    for ($x = 10; $x < (int) ($w * 0.35); $x += 8) {
        $rgb = imagecolorat($img, $x, $y);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        if ($r < 40 && $g < 40 && $b < 40) {
            continue;
        }
        if ($r > 230 && $g > 230 && $b > 230) {
            continue;
        }
        if ($r > $g && $g > $b && $r > 120) {
            $samples[] = [$r, $g, $b];
        }
    }
}

$avg = [0, 0, 0];
foreach ($samples as $s) {
    $avg[0] += $s[0];
    $avg[1] += $s[1];
    $avg[2] += $s[2];
}
$n = max(1, count($samples));
$avg = [ (int) round($avg[0] / $n), (int) round($avg[1] / $n), (int) round($avg[2] / $n) ];

echo 'samples=' . count($samples) . PHP_EOL;
echo 'rgb=' . implode(',', $avg) . PHP_EOL;
echo 'hex=#' . sprintf('%02X%02X%02X', $avg[0], $avg[1], $avg[2]) . PHP_EOL;
