Theme Class

The Theme class provides functionality for managing named color collections.

Overview

namespace Farzai\ColorPalette;

class Theme implements ThemeInterface
{
    // ...
}

The Theme class provides functionality for:

Creating Themes

Constructor

public function __construct(array $colors = [])

Creates a new Theme instance with named colors.

Parameters:

Static Factory Methods

public static function fromColors(array $colors): self

Creates a new Theme instance from an array of colors.

Parameters:

Color Management

getColor()

public function getColor(string $name): ColorInterface

Gets a color by its name.

Parameters:

Returns:

Throws:

hasColor()

public function hasColor(string $name): bool

Checks if a color exists in the theme.

Parameters:

Returns:

getColors()

public function getColors(): array

Gets all colors in the theme.

Returns:

toArray()

public function toArray(): array

Converts the theme to an array of hex color values.

Returns:

Examples

Basic Usage

use Farzai\ColorPalette\Theme;
use Farzai\ColorPalette\Color;

// Create a theme with named colors
$theme = new Theme([
    'primary' => new Color(37, 99, 235),    // Blue
    'secondary' => new Color(244, 63, 94),   // Red
    'background' => new Color(255, 255, 255) // White
]);

// Access colors by name
$primaryColor = $theme->getColor('primary');
echo $primaryColor->toHex(); // "#2563eb"

// Check if a color exists
if ($theme->hasColor('primary')) {
    // Use the color
}

// Get all colors as hex values
$colors = $theme->toArray();
// [
//     'primary' => '#2563eb',
//     'secondary' => '#f43f5e',
//     'background' => '#ffffff'
// ]

Creating Themes from Colors

// Create a theme using the static factory method
$theme = Theme::fromColors([
    'text' => new Color(0, 0, 0),
    'background' => new Color(255, 255, 255)
]);

// Access colors
$textColor = $theme->getColor('text');
$backgroundColor = $theme->getColor('background');