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
- ImageLoader - Image loading and processing
- ColorExtractor - Color extraction from images
π§ Color Operations
- Color Manipulation - Adjusting colors
- Color Spaces - Working with different color spaces
- Color Schemes - Generating color combinations
π― Advanced Features
- Palette Generation - Creating color palettes
- Utilities - Helper functions and tools
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
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
- Color Creation
- Use the most appropriate constructor for your use case
- Validate color values before creation
- Use named constructors for clarity
- Color Extraction
- Cache extracted palettes for frequently used images
- Use appropriate sample sizes for performance
- Handle extraction errors gracefully
- Theme Generation
- Start with a carefully chosen base color
- Test themes across different contexts
- Consider accessibility requirements
- Performance
- Use the GD backend for better performance
- Implement caching where appropriate
- Batch color operations when possible