marijnvdwerf / material-palette
Prominent image colour extraction for PHP
Installs: 47 078
Dependents: 0
Suggesters: 0
Security: 0
Stars: 29
Watchers: 3
Forks: 6
Open Issues: 7
Requires
- php: >=5.4
- intervention/image: ~2.2
Requires (Dev)
- devster/ubench: ^1.1
- phpunit/phpunit: ^6.1
- symfony/debug: ~2.6
- symfony/var-dumper: ~2.6
Suggests
- ext-gd: to use the GD implementation
- ext-imagick: to use the Imagick implementation
README
Palette is a port of the Android library of the same name, for extracting a colour palette from an image. The results can then be used for tinting the interface to match the image.
Installation
$ composer require 'marijnvdwerf/material-palette:~1.0'
Usage
Palette makes use of the Intervention image library to load images, and supports both the Imagick and GD drivers. The performance of the Imagick driver is slightly better, but the difference is fairly small.
$manager = new ImageManager(array('driver' => 'imagick')); $image = $manager->make('path/to/image.png'); $palette = Palette::generate($image); echo $palette->getVibrantSwatch()->getColor();
Contrast
You can get the contrast of a colour on a non-translucent background by calling
AbstractColor::calculateContrast($background, $foreground)
. Information on the recommended contrast ratio can be found
at the W3C recommendation.
$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>';