myindexlike / color-extractor
像人类一样从图片中提取颜色。
0.3
2021-10-25 22:31 UTC
Requires
- php: >=7.2.0
- ext-gd: *
Requires (Dev)
Replaces
This package is not auto-updated.
Last update: 2024-09-25 10:48:22 UTC
README
像人类一样从图片中提取颜色。
安装
通过 Composer
$ composer require myindexlike/best-color-extractor:0.3.*
使用方法
require 'vendor/autoload.php'; use RuslanPro\ColorExtractor\Color; use RuslanPro\ColorExtractor\ColorExtractor; use RuslanPro\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'));