Code Monkey home page Code Monkey logo

imagepalette's Introduction

ImagePalette

Build Status Total Downloads Latest Stable Version Scrutinizer Code Quality

ImagePalette is used to extract a color palette from a given image. Aside from being a native PHP implementation, ImagePalette differs from many palette extractors as it works off a white list color palette. Below is the default palette:

The main advantage of working from a color palette is closer matching, as each pixel simply has to calculate the color-distance within the palette and chose the best match. This is useful for working with color taxonomies as the taxonomy should have a finite amount of colors.

See an example of this in action here: http://alpha.wallhaven.cc/wallpaper/21852

Requirements

PHP >= 5.4 php5-gd

Installation

Simply add the following to your composer.json file:

"require": {
    "brianmcdo/image-palette": "dev-master"
}

Usage

// initiate with image
$palette = new \BrianMcdo\ImagePalette\ImagePalette( 'https://www.google.co.uk/images/srpr/logo3w.png' );

// get the prominent colors
$colors = $palette->colors; // array of Color objects

// to string as json
echo $palette; // '["#ffffdd", ... ]'

// implements IteratorAggregate
foreach ($palette as $color) {
  // Color provides several getters/properties
  echo $color;             // '#ffffdd'
  echo $color->rgbString;  // 'rgb(255,255,221)'
  echo $color->rgbaString; // 'rgba(255,255,221,0.25)'
  echo $color->int;        // 0xffffdd
  echo $color->rgb;        // array(255,255,221)
  echo $color->rgba;       // array(255,255,221,0.25)
  // ...
}

And there we go!

Laravel 4

Find the providers key in app/config/app.php and register the ImagePaletteServiceProvider:

'providers' => array(
    // ...
    'BrianMcdo\ImagePalette\Laravel\ImagePaletteServiceProvider',
)

Then, find the aliases key in app/config/app.php and register the ImagePaletteFacade:

'aliases' => array(
    // ...
    'ImagePalette' => 'BrianMcdo\ImagePalette\Laravel\ImagePaletteFacade',
)

Example:

$fileOrUrl = 'https://www.google.com/images/srpr/logo11w.png';

ImagePalette::getColors($fileOrUrl);

Options

Precision

By default, ImagePalette will process every 10th pixel. This is for performance reasons, you can change this like below. The precision is a performance-to-time decision.

$palette = new \BrianMcdo\ImagePalette\ImagePalette( $src, 5 /* precision */ );

Color Count

To control the amount colors returned set the third parameter. You can also provide the getter with a custom length.

$palette = new \BrianMcdo\ImagePalette\ImagePalette( $src, 5, 3 /* number of colors to return */ );
$colors = $palette->getColors(7 /* number of colors to return */);

Contribution guidelines

See: https://github.com/brianmcdo/ImagePalette/blob/master/CONTRIBUTING.md

imagepalette's People

Contributors

brianmcdo avatar gandalfx avatar joehoyle avatar richardswain avatar bitdeli-chef avatar scrutinizer-auto-fixer avatar bhavyanshu avatar

Watchers

James Cloos avatar

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.