diggipacks-co/laravel-color-extractor

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

dev-master 2021-10-31 16:20 UTC

This package is not auto-updated.

Last update: 2024-09-17 04:00:12 UTC


README

Build Status Total Downloads Latest Stable Version

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

安装

通过 Composer

composer require diggipacks-co/laravel-color-extractor

使用方法

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);

// get hex color from $colors
foreach($colors as $color) {
    echo dechex($color);
}

处理透明度

默认情况下,任何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'));

贡献

请参阅CONTRIBUTING以获取详细信息。

致谢

许可协议

MIT 许可协议(MIT)。有关更多信息,请参阅许可文件