jonaskohl / color-extractor
以人的方式从图片中提取颜色。
0.4.0
2021-05-28 12:19 UTC
Requires
- php: >=5.4.0
- ext-gd: *
Requires (Dev)
Replaces
- league/color-extractor: *
- matthecat/colorextractor: *
README
以人的方式从图片中提取颜色。
安装
通过 Composer
$ composer require jonaskohl/color-extractor:0.3.*
使用方法
require 'vendor/autoload.php'; use JonasKohl\ColorExtractor\Color; use JonasKohl\ColorExtractor\ColorExtractor; use JonasKohl\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'));
贡献
请参阅 CONTRIBUTING 以获取详细信息。
致谢
许可协议
MIT 许可协议 (MIT)。请参阅 许可文件 以获取更多信息。