marijnvdwerf/material-palette

PHP的突出图像颜色提取

1.3.0 2019-10-26 15:33 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:11:13 UTC


README

Logo

调色板是同一名称的Android库的移植,用于从图像中提取调色板。然后可以使用这些结果来调整界面以匹配图像。

安装

$ composer require 'marijnvdwerf/material-palette:~1.0'

使用

调色板利用Intervention图像库来加载图像,并支持Imagick和GD驱动程序。Imagick驱动程序的性能略好,但差异相当小。

$manager = new ImageManager(array('driver' => 'imagick'));
$image = $manager->make('path/to/image.png');

$palette = Palette::generate($image);
echo $palette->getVibrantSwatch()->getColor();

对比度

您可以通过调用AbstractColor::calculateContrast($background, $foreground)来获取非半透明背景上颜色的对比度。有关推荐对比度比例的信息,请参阅W3C推荐

$white = new RGBColor(1, 1, 1);
$black = new RGBColor(0, 0, 0);
$background = $palette->getVibrantSwatch()->getColor();

echo '<div style="background: ' . $background . '">';
if(AbstractColor::calculateContrast($background, $white) >= 3) {
    echo '<h1 style="color: white;">Palette</h1>';
} else {
    echo '<h1 style="color: black;">Palette</h1>';
}
echo '</div>';