farzai/color-palette

A robust PHP library for extracting, analyzing, and managing color palettes from images

1.0.0 2024-11-28 05:14 UTC

README

Latest Version on Packagist Tests Total Downloads

A powerful PHP library for extracting color palettes from images and generating color themes. This package supports multiple image processing backends (GD and Imagick) and provides a rich set of color manipulation features.

Color Palette Example

Documentation

📚 View Full Documentation

Features

  • 🎨 Extract dominant colors from images using advanced color quantization
  • 🖼️ Support for multiple image formats (JPEG, PNG, GIF, etc.)
  • 🔄 Multiple image processing backends (GD and Imagick)
  • 🎯 Generate color themes with surface, background, and accent colors
  • 🌈 Color manipulation with RGB, HSL, and Hex support
  • 📏 Color contrast ratio calculations
  • 🎭 Automatic text color suggestions for optimal readability
  • 🔍 Smart surface color recommendations based on color brightness

Requirements

  • PHP 8.1 or higher
  • GD extension or ImageMagick extension
  • Composer

Installation

You can install the package via composer:

composer require farzai/color-palette

Basic Usage

use Farzai\ColorPalette\ImageFactory;
use Farzai\ColorPalette\ColorExtractorFactory;
use Farzai\ColorPalette\ColorPalette;

// Create an image instance
$imageFactory = new ImageFactory();
$image = $imageFactory->createFromPath('path/to/image.jpg');

// Create a color extractor
$extractorFactory = new ColorExtractorFactory();
$extractor = $extractorFactory->create('gd'); // or 'imagick'

// Extract colors to create a palette
$colors = $extractor->extract($image);
$palette = new ColorPalette($colors);

// Get all colors
$colors = $palette->getColors();

// Get suggested text color for a background
$backgroundColor = $colors[0];
$textColor = $palette->getSuggestedTextColor($backgroundColor);

// Get suggested surface colors
$surfaceColors = $palette->getSuggestedSurfaceColors();
// Available keys: 'surface', 'background', 'accent', 'surface_variant'

Documentation

For detailed documentation, please visit our Documentation Site.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.