league/color-extractor

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

0.4.0 2022-09-24 15:57 UTC

This package is auto-updated.

Last update: 2024-08-25 15:43:16 UTC


README

Build Status Total Downloads Latest Stable Version

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

安装

通过 Composer

$ composer require league/color-extractor

使用方法

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)。有关更多信息,请参阅许可证文件