Code Monkey home page Code Monkey logo

material-palette-php's Introduction

Palette for PHP

Logo

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

material-palette-php's People

Contributors

agallou avatar marijnvdwerf avatar on2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

material-palette-php's Issues

asHSLColor() not working as expected

Hello !

thanks for this library, i've tried a lot of color extraction php libs and this one is by far the best.

But there's a problem when converting a color to HSL the hue value seems wrong.

$color = new \marijnvdwerf\palette\Color\RGBColor(0.8, 0.5, 0.2)

(string) $color
// => "rgb(204, 128, 51)"
// this is right

(string) $color->asHSLColor()
// => "hsl(0.000000, 60.000000%, 50.000000%)"
// this is wrong

(string) $color->asHSLColor()->asRGBColor()
// => "rgb(204, 51, 51)"
// this is different from the color we entered in the constructor

Best regards,

License

Could you please define a License for this so I can figure out if I am able to use it?

Consider returning non-zero population in Palette::generateEmptySwatches?

I'm not sure if what I'm about to suggest is a good idea, but I was surprised to see zero-population swatches turn up in my results. This is caused by L123 and L133 in Palette:generateEmptySwatches.

Would it be possible (or worthwhile) to include a non-zero population count for generated swatches? One option might be to take the generated color, and re-analyze the image to determine the population of pixels that approximate that color. Another option might be to use the dark swatch population in its stead.

In my implementation, I'm just looking to extract a single dominant color, so if the vibrant color is missing its population, I ignore it and fall back to dark vibrant.

The reason I'm asking is because I'm intending to use the population together with image size to generate a fill percentage, which will be used as a factor in weighing score when searching by color.

I'm not sure if I'm looking for solutions here, but I'd be interested in a discussion. Hopefully, if someone else is confused by zero populations appearing in their results, this will help them track down the cause.

Ignored swatches cause error

Hello,

With some images I get this error :

max(): Array must contain at least one element
Palette.php","line":81,"function":"max"

I tracked the error down and it's caused by the shouldIgnoreSwatch function, and more precisely to isNearRedILine.

I don't understand the meaning of isNearRedILine but it's causing the swatches array to be empty.
Would be nice if we could customize shouldIgnoreSwatch as proposed here : #12
Or at least if Palette.php could handle an empty array without throwing an error.

Example image:
image

Surely I can work on a PR if you explain the meaning of isNearRedILine

Thanks

Allow to configure shouldIgnoreSwatch() types

We have some issues by using your library. We ran mostly in the isNearRedILine() case.

So my suggestion is to allow user to configure the ignore types.

Would be great if you can adapt this.

Bug. Warning: max(): Array must contain at least one element

An array of colors can be empty when there are no colors in the image.
In the function max and min, you can not pass an empty array, otherwise the function will return false and throw warning.

Stacktrace:

Warning: max(): Array must contain at least one element

Exception trace:
 () at vendor/marijnvdwerf/material-palette/src/ColorCutQuantizer.php:101
 marijnvdwerf\palette\Vbox->getVolume() at vendor/marijnvdwerf/material-palette/src/ColorCutQuantizer.php:39
 marijnvdwerf\palette\ColorCutQuantizer->quantize() at vendor/marijnvdwerf/material-palette/src/PaletteGenerator.php:47
 marijnvdwerf\palette\PaletteGenerator->generate() at vendor/marijnvdwerf/material-palette/src/Palette.php:59

Sample code

$manager = new ImageManager();
$image = $manager->make('filename');
$palette = Palette::generate($image);
$palette->getVibrantSwatch()->getColor();

Problem image

icon

Performance?

First off, really like this tool. Thanks for putting it together.

My question concerns performance.

I am using Intervention Image with GD on a 250x 250 px image and am seeing the Palette::generate($image) method take 3 seconds for this single small image. This is on a recent i7 macbook running PHP 5.6 on MacOS. The quantize($swatches, 16) call within the PaletteGenerator->generate() method is where all the time is spent.

Is this expected?

PHP with GD and without composer

Can you provide instructions in the readme on how to install without composer (as not everyone uses it) and also an example using gd, as I can't install imagemagick on my server.
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.