php画图取色,Color Extractor:一个从图像中提取颜色的PHP库

ColorExtractor是一个PHP库,它能像人类一样从图像中提取颜色。通过Composer安装后,可以创建色彩调色板,按像素计数排序颜色,并获取最常用的颜色。它还支持处理透明度,允许指定背景色来考虑透明像素。此外,项目欢迎贡献,并遵循MIT许可证。
摘要由CSDN通过智能技术生成

ColorExtractor

8e15851b1e0ccaf1f877355066f1b273.png68747470733a2f2f706f7365722e707567782e6f72672f6c65616775652f636f6c6f722d657874726163746f722f646f776e6c6f6164732e706e6768747470733a2f2f706f7365722e707567782e6f72672f6c65616775652f636f6c6f722d657874726163746f722f762f737461626c652e706e67

Extract colors from an image like a human would do.

Install

Via Composer

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

Usage

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

Handling transparency

By default any pixel with alpha value greater than zero will be discarded. This is because transparent colors are not perceived as is. For example fully transparent black would be seen white on a white background. So if you want to take transparency into account when building a palette you have to specify this background color. You can do this with the second argument of Palette constructors. Its default value is null, meaning a color won't be added to the palette if its alpha component exists and is greater than zero.

You can set it as an integer representing the color, then transparent colors will be blended before addition to the palette.

// 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

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值