Permalink
Browse files

version 0.2.0

  • Loading branch information...
1 parent ab500c5 commit 838b828aa939db788025aa97ce2276e6aa7108c8 @MatTheCat MatTheCat committed Jan 12, 2016
View
@@ -2,3 +2,4 @@ vendor
composer.lock
.DS_Store
.idea
+.php_cs.cache
View
@@ -0,0 +1,12 @@
+<?php
+
+$finder = \Symfony\Component\Finder\Finder::create()
+ ->files()
+ ->name('*.php')
+ ->in(array('src', 'tests'));
+
+$config = Symfony\CS\Config\Config::create()
+ ->finder($finder)
+ ->setUsingCache(true);
+
+return $config;
View
@@ -12,86 +12,42 @@ Extract colors from an image like a human would do.
Via Composer
``` bash
-$ composer require league/color-extractor:0.1.*
+$ composer require league/color-extractor:0.2.*
```
## Usage
```php
require 'vendor/autoload.php';
-use League\ColorExtractor\Client as ColorExtractor;
+use League\ColorExtractor\Color;
+use League\ColorExtractor\ColorExtractor;
+use League\ColorExtractor\Palette;
-$client = new ColorExtractor;
+$palette = Palette::fromFilename('./some/image.png');
-$image = $client->loadPng('./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";
+}
-// Get the most used color hex code
-$palette = $image->extract();
+// it offers some helpers too
+$topFive = $palette->getMostUsedColors(5);
-// Get three most used color hex code
-$palette = $image->extract(3);
+$colorCount = count($palette);
-// Change the Minimum Color Ratio (0 - 1)
-// Default: 0
-$image->setMinColorRatio(1);
-$palette = $image->extract();
+$blackCount = $palette->getColorCount(Color::fromHexToInt('#000000'));
-```
-
-## Service Providers
-
-Integration with most frameworks would require a bridge package, but for Silex and Laravel 4 a
-simple service provider will suffice.
-
-### Silex
-
-First register `ColorExtractorServiceProvider` in your application:
-```php
-use League\ColorExtractor\Silex\ColorExtractorServiceProvider;
-
-// ... create $app
-$app->register(new ColorExtractorServiceProvider);
-```
-
-Then you can use like this:
-
-```php
-$image = $app['color-extractor']->loadPng('./some/image.png');
-...
-$palette = $image->extract();
-```
-
-### Laravel 4
-
-Find the `providers` key in `app/config/app.php` and register the `ColorExtractorServiceProvider`:
-```php
-'providers' => array(
- // ...
- 'League\ColorExtractor\Laravel\ColorExtractorServiceProvider',
-)
-```
+// an extractor is built from a palette
+$extractor = new ColorExtractor($palette);
-Then you can use it exactly the same way as the Silex service provider.
-If you prefer to use Facades, find the `aliases` key in `app/config/app.php` and register the `ColorExtractorFacade`:
+// it defines an extract method which return the most “representative” colors
+$colors = $extractor->extract(5);
-```php
-'aliases' => array(
- // ...
- 'ColorExtractor' => 'League\ColorExtractor\Laravel\ColorExtractorFacade',
-)
```
-Example:
-
-```php
-$image = ColorExtractor::loadPng('./some/image.png');
-...
-$palette = $image->extract();
-```
-
-
## Contributing
Please see [CONTRIBUTING](https://github.com/thephpleague/color-extractor/blob/master/CONTRIBUTING.md) for details.
View
@@ -21,14 +21,12 @@
"ext-gd": "*"
},
"require-dev": {
- "laravel/framework": "~4.1",
- "orchestra/testbench": "~2.0",
- "silex/silex": "~1.1",
- "squizlabs/php_codesniffer": "~1.5"
+ "fabpot/php-cs-fixer": "*",
+ "phpunit/phpunit": "~5"
},
"autoload": {
- "psr-0": {
- "League\\ColorExtractor": ["src", "tests"]
+ "psr-4": {
+ "": "src"
}
}
}
View
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
-<phpunit colors="true" bootstrap="vendor/autoload.php">
+<phpunit colors="true" bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="ColorExtractor Test Suite">
<directory>tests/League/ColorExtractor</directory>
@@ -1,64 +0,0 @@
-<?php
-
-namespace League\ColorExtractor;
-
-/**
- * Class Client
- * @package League\ColorExtractor
- */
-class Client
-{
- /**
- * @param resource $resource
- *
- * @return Image
- */
- public function loadResource($resource)
- {
- if (!is_resource($resource) || get_resource_type($resource) != 'gd') {
- throw new \InvalidArgumentException('Image must be a gd resource');
- }
-
- return new Image($resource);
- }
-
- /**
- * @param string $imagePath
- *
- * @return Image
- */
- public function loadFile($imagePath)
- {
- return new Image(imagecreatefromstring(file_get_contents($imagePath)));
- }
-
- /**
- * @param string $imagePath
- *
- * @return Image
- */
- public function loadJpeg($imagePath)
- {
- return new Image(imagecreatefromjpeg($imagePath));
- }
-
- /**
- * @param string $imagePath
- *
- * @return Image
- */
- public function loadPng($imagePath)
- {
- return new Image(imagecreatefrompng($imagePath));
- }
-
- /**
- * @param string $imagePath
- *
- * @return Image
- */
- public function loadGif($imagePath)
- {
- return new Image(imagecreatefromgif($imagePath));
- }
-}
@@ -0,0 +1,27 @@
+<?php
+
+namespace League\ColorExtractor;
+
+class Color
+{
+ /**
+ * @param int $color
+ * @param bool $prependHash = true
+ *
+ * @return string
+ */
+ public static function fromIntToHex($color, $prependHash = true)
+ {
+ return ($prependHash ? '#' : '').sprintf('%02X%02X%02X', ($color >> 16) & 0xFF, ($color >> 8) & 0xFF, $color & 0xFF);
+ }
+
+ /**
+ * @param string $color
+ *
+ * @return int
+ */
+ public static function fromHexToInt($color)
+ {
+ return hexdec(ltrim($color, '#'));
+ }
+}
Oops, something went wrong.

0 comments on commit 838b828

Please sign in to comment.