爱泰欧/颜色提取器

像人类一样从图片中提取颜色。

0.3.3 2022-03-22 09:52 UTC

This package is auto-updated.

Last update: 2024-09-22 15:01:33 UTC


README

Build Status Total Downloads Latest Stable Version

像人类一样从图片中提取颜色。

安装

通过 Composer

$ composer require league/color-extractor:0.3.*

使用方法

require 'vendor/autoload.php';

use League\ColorExtractor\Color;
use League\ColorExtractor\ColorExtractor;
use League\ColorExtractor\Palette;

$palette = Palette::fromFilename('./some/image.png');

// $palette is an iterator on colors sorted by pixel count
foreach($palette as $color => $count) {
    // colors are represented by integers
    echo Color::fromIntToHex($color), ': ', $count, "\n";
}

// it offers some helpers too
$topFive = $palette->getMostUsedColors(5);

$colorCount = count($palette);

$blackCount = $palette->getColorCount(Color::fromHexToInt('#000000'));


// an extractor is built from a palette
$extractor = new ColorExtractor($palette);

// it defines an extract method which return the most “representative” colors
$colors = $extractor->extract(5);

处理透明度

默认情况下 任何具有大于零的alpha值的像素将被丢弃。这是因为透明颜色不会被感知为真实颜色。例如,在白色背景上,完全透明的黑色看起来是白色的。所以如果您想在构建调色板时考虑透明度,您必须指定此背景颜色。您可以通过 Palette 构造函数的第二个参数来指定它。其默认值为 null,这意味着如果颜色的alpha分量存在且大于零,则颜色不会被添加到调色板中。

您可以将它设置为一个表示颜色的整数,然后透明颜色将在添加到调色板之前进行混合。

// we set a white background so fully transparent colors will be added as white in the palette
// pure red #FF0000 at 50% opacity will be stored as #FF8080 as it would be perceived
$palette = Palette::fromFilename('./some/image.png', Color::fromHexToInt('#FFFFFF'));

贡献

请参阅贡献指南以获取详细信息。

致谢

许可协议

MIT 许可协议 (MIT)。请参阅许可文件以获取更多信息。