API Reference

Welcome to the Color Palette PHP API documentation. This comprehensive guide covers all the classes, methods, and features available in the library.

🎨 Core Components

  • Color - Core color representation and manipulation
  • ColorPalette - Collection of colors with analysis tools
  • Theme - Theme generation and management

πŸ–ΌοΈ Image Processing

πŸ”§ Color Operations

🎯 Advanced Features

Quick Start

Here’s a quick overview of the most commonly used features:

Creating Colors

use Farzai\ColorPalette\Color;

// From RGB values
$color = new Color(37, 99, 235);

// From hex string
$color = Color::fromHex('#2563eb');

// From HSL values
$color = Color::fromHsl(220, 84, 53);

Extracting Colors from Images

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

// Load image
$imageFactory = new ImageFactory();
$image = $imageFactory->createFromPath('image.jpg');

// Extract colors
$extractorFactory = new ColorExtractorFactory();
$extractor = $extractorFactory->make('gd');
$palette = $extractor->extract($image, 5);

Generating Themes

use Farzai\ColorPalette\PaletteGenerator;

// Create generator with base color
$generator = new PaletteGenerator($color);

// Generate different schemes
$analogous = $generator->analogous();
$complementary = $generator->complementary();
$websiteTheme = $generator->websiteTheme();

Class Reference

Core Classes

Color

Core class for color representation and manipulation.

View Documentation β†’

ColorPalette

Manages collections of colors with analysis tools.

View Documentation β†’

Theme

Handles theme generation and management.

View Documentation β†’

ColorExtractor

Extracts dominant colors from images.

View Documentation β†’

Interface Reference

Core Interfaces

// ColorInterface - Base interface for color operations
interface ColorInterface {
    public function toHex(): string;
    public function toRgb(): array;
    public function toHsl(): array;
    // ... more methods
}

// ColorPaletteInterface - Interface for palette operations
interface ColorPaletteInterface {
    public function getColors(): array;
    public function getSuggestedTextColor(ColorInterface $backgroundColor): ColorInterface;
    public function getSuggestedSurfaceColors(): array;
}

// ThemeInterface - Interface for theme operations
interface ThemeInterface {
    public function getPrimaryColor(): ColorInterface;
    public function getSecondaryColor(): ColorInterface;
    public function getAccentColor(): ColorInterface;
    // ... more methods
}

Error Handling

The library uses custom exceptions for different types of errors:

use Farzai\ColorPalette\Exceptions\ColorException;
use Farzai\ColorPalette\Exceptions\ImageException;
use Farzai\ColorPalette\Exceptions\InvalidArgumentException;

try {
    // Your code here
} catch (ColorException $e) {
    // Handle color-related errors
} catch (ImageException $e) {
    // Handle image-related errors
} catch (InvalidArgumentException $e) {
    // Handle invalid argument errors
}

Best Practices

  1. Color Creation
    • Use the most appropriate constructor for your use case
    • Validate color values before creation
    • Use named constructors for clarity
  2. Color Extraction
    • Cache extracted palettes for frequently used images
    • Use appropriate sample sizes for performance
    • Handle extraction errors gracefully
  3. Theme Generation
    • Start with a carefully chosen base color
    • Test themes across different contexts
    • Consider accessibility requirements
  4. Performance
    • Use the GD backend for better performance
    • Implement caching where appropriate
    • Batch color operations when possible

See Also