ColorPalette Class

The ColorPalette class manages collections of colors and provides tools for color analysis and theme generation.

Overview

namespace Farzai\ColorPalette;

class ColorPalette implements ArrayAccess, ColorPaletteInterface, Countable
{
    // ...
}

The ColorPalette class provides functionality for:

Creating Color Palettes

Constructor

public function __construct(array $colors)
Creates a new ColorPalette instance from an array of colors.

Parameters

Name Type Description
$colors array<string|int, ColorInterface> Array of colors with optional keys

Color Collection Methods

getColors

public function getColors(): array
Gets all colors in the palette.

Returns

Array of ColorInterface instances

count

public function count(): int
Gets the number of colors in the palette.

Returns

Number of colors

Array Access Methods

offsetExists

public function offsetExists(mixed $offset): bool
Checks if a color exists at the specified offset.

Parameters

Name Type Description
$offset mixed Array offset or key

offsetGet

public function offsetGet(mixed $offset): ?ColorInterface
Gets a color at the specified offset.

Parameters

Name Type Description
$offset mixed Array offset or key

Returns

Color instance or null if not found

offsetSet

public function offsetSet(mixed $offset, mixed $value): void
Sets a color at the specified offset.

Parameters

Name Type Description
$offset mixed Array offset or key
$value ColorInterface Color instance to set

offsetUnset

public function offsetUnset(mixed $offset): void
Removes a color at the specified offset.

Parameters

Name Type Description
$offset mixed Array offset or key

Color Analysis Methods

getSuggestedTextColor

public function getSuggestedTextColor(ColorInterface $backgroundColor): ColorInterface
Gets a suggested text color for the given background color.

Parameters

Name Type Description
$backgroundColor ColorInterface Background color to analyze

Returns

Suggested text color (black or white) for optimal contrast

getSuggestedSurfaceColors

public function getSuggestedSurfaceColors(): array
Gets suggested surface colors based on the palette.

Returns

Array of suggested colors for different surface types:

  • 'surface' - Main surface color
  • 'background' - Background color
  • 'accent' - Accent color
  • 'surface_variant' - Alternative surface color

Examples

Creating and Using a Color Palette

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

// Create colors
$blue = new Color(37, 99, 235);
$red = new Color(239, 68, 68);
$green = new Color(34, 197, 94);

// Create palette
$palette = new ColorPalette([$blue, $red, $green]);

// Access colors
$firstColor = $palette[0];
$count = count($palette);

// Add a new color
$palette[] = new Color(168, 85, 247); // Purple

Color Analysis

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

// Get surface colors
$surfaceColors = $palette->getSuggestedSurfaceColors();
$mainSurface = $surfaceColors['surface'];
$background = $surfaceColors['background'];
$accent = $surfaceColors['accent'];

Array Access

// Check if color exists
if (isset($palette[0])) {
    // Get color
    $color = $palette[0];
    
    // Update color
    $palette[0] = new Color(59, 130, 246);
    
    // Remove color
    unset($palette[0]);
}

See Also