spatie/color

repository·main·Indexed 18 days ago

https://github.com/spatie/color

A PHP 8.2+ library for handling color conversions between formats including RGB, Hex, HSL, CMYK, CIELab, XYZ, Argb, and Hsb. It provides a Factory for automatic format detection, utilities for calculating contrast ratios, and perceptual color distance algorithms (CIE76, CIE94, CIEDE2000).

Tokens
8.4K
Snippets
38
Records
41
Agent score
59%

What's inside spatie/color

  1. How color formats and the Color interface work

    main

    The library uses a separate class for each color format, all of which implement the Spatie\Color\Color interface. This allows you to work with different color models (like RGB, Hex, or HSL) using a consistent set of methods.

    Supported color classes:

    • Argb
    • CIELab
    • Cmyk
    • Hex
    • Hsb
    • Hsl
    • Hsla
    • Named
    • Rgb
    • Rgba
    • Xyz
  2. Compare color contrast and distance

    main

    The library provides utilities to calculate color contrast ratios and perceptual color distances.

    Contrast Ratio: Use Contrast::ratio(Color $color1, Color $color2) to get the contrast ratio between two colors.

    Color Distance: Use the Distance class to calculate how different two colors appear to the human eye using various algorithms:

    • Distance::CIE76($color1, $color2)
    • Distance::CIE94($color1, $color2, int $applicationType = 0): Use 0 for Graphic Arts (default) or 1 for Textiles.
    • Distance::CIEDE2000($color1, $color2)

    Note: Comparison functions accept both Color instances and color strings (they use the Factory internally).

    $rgb = Rgb::fromString('rgb(55,155,255)');
    $hex = Hex::fromString('#2d78c8');
    
    // Contrast
    $ratio = Contrast::ratio(Hex::fromString('#f0fff0'), Hex::fromString('#191970')); 
    
    // Distance
    $cie76 = Distance::CIE76($rgb, $hex);
    $cie94_textiles = Distance::CIE94($rgb, $hex, 1);
    $ciede2000 = Distance::CIEDE2000($rgb, $hex);
  3. Create color instances from strings

    main

    You can create specific color instances using their respective fromString() methods, or use the Factory class to automatically detect the format from a string.

    Specific formats:

    Named::fromString('blue');
    Hex::fromString('#000000');
    Rgba::fromString('rgba(255, 255, 255, 1)');
    Argb::fromString('argb(1, 255, 255, 255)');
    Hsla::fromString('hsla(360, 100%, 100%, 1)');

    Automatic detection with Factory:

    Factory::fromString('rgb(0, 0, 255)'); // Returns Rgb instance
    Factory::fromString('blue');           // Returns Named instance
    Factory::fromString('#0000ff');        // Returns Hex instance

    Note: Throws InvalidColorValue if the string cannot be parsed.

    Factory::fromString('rgb(0, 0, 255)'); // `Rgb` instance
    Factory::fromString('blue'); // `Named` instance
    Factory::fromString('#0000ff'); // `Hex` instance
    Factory::fromString('hsl(240, 100%, 50%)'); // `Hsl` instance
  4. Access color channels (Red, Green, Blue)

    main

    Use the red(), green(), and blue() methods to retrieve the values of individual color channels. The return type depends on the color format (e.g., int for Rgb or string for Hex).

    Hex::fromString('#ff0000')->red(); // 'ff'
    Rgb::fromString('rgb(255, 0, 0)')->red(); // 255
  5. Convert between color formats

    main

    Every color class provides methods to convert to other formats. If converting from a format that lacks an alpha channel (like Rgb), you can provide an $alpha parameter to add opacity.

    Common conversions:

    • toHex(): Returns a Hex instance.
    • toRgb(): Returns an Rgb instance (omits opacity if source has it).
    • toRgba(float $alpha = 1): Returns a Rgba instance.
    • toHsla(float $alpha = 1): Returns a Hsla instance.
    • toCmyk(): Returns a Cmyk instance.
    • toHsb(): Returns a Hsb instance.
    • toHsl(): Returns a Hsl instance.
    • toArgb(float $alpha = 1): Returns an Argb instance.
    • toCIELab(): Returns a CIELab instance.
    • toXyz(): Returns an Xyz instance.
    // Adding alpha during conversion
    Rgb::fromString('rgb(0, 0, 255)')->toHsla(.5);
    // Converting and omitting existing alpha
    Rgba::fromString('rgba(0, 0, 255, .5)')->toHsl();
  6. Create an Xyz instance from a string

    main

    The Xyz::fromString(string $string): static method allows you to create an Xyz instance from a formatted string. The expected format is xyz(x, y, z), where x, y, and z are numeric values separated by commas.

    Example format: xyz(0.5, 0.5, 0.5)

    use Spatie\Color\Xyz;
    
    $xyz = Xyz::fromString('xyz(0.95, 1.0, 1.08)');
  7. String representation of Hsb

    main

    The Hsb class implements __toString(), which returns a string representation in the format hsb(hue,saturation%,brightness%). The hue, saturation, and brightness values are rounded in the output string.

    $color = new Hsb(120.4, 100.1, 50.9);
    echo (string) $color; // "hsb(120,100%,51%)"
  8. Get HSB components and RGB values from Hsb

    main

    You can retrieve the individual components of an Hsb instance:

    • hue(): float - Returns the hue.
    • saturation(): float - Returns the saturation.
    • brightness(): float - Returns the brightness.
    • red(): int - Returns the red component (0-255).
    • green(): int - Returns the green component (0-255).
    • blue(): int - Returns the blue component (0-255).
    $color = new Hsb(180, 50, 50);
    
    echo $color->hue();        // 180
    echo $color->red();       // RGB red component
  9. Instantiate Rgba colors

    main

    You can create an Rgba instance by passing the red, green, and blue integer values (0-255) and a float alpha value (0.0-1.0) to the constructor. Alternatively, use the fromString() static method to create an instance from a CSS-style rgba() string.

    use Spatie\
    Color\\Rgba;
    
    // Via constructor
    $rgba = new Rgba(255, 0, 0, 0.5);
    
    // Via CSS string
    $rgba = Rgba::fromString('rgba(255, 0, 0, 0.5)');
  10. Get RGBA channel values

    main

    Use the following methods to retrieve the individual color components of an Rgba instance:

    • red(): Returns the red channel as an int.
    • green(): Returns the green channel as an int.
    • blue(): Returns the blue channel as an int.
    • alpha(): Returns the alpha channel as a float.
    $red = $rgba->red();
    $green = $rgba->green();
    $blue = $rgba->blue();
    $alpha = $rgba->alpha();
  11. Convert a Hex color to other color spaces

    main

    The Hex class provides several methods to convert the color into different color models. Most conversions are performed by first converting the hex values to RGB.

    Available conversion methods:

    • toRgb(): Returns an Rgb instance.
    • toRgbA(?float $alpha = null): Returns an Rgba instance.
    • toArgb(?float $alpha = null): Returns an Argb instance.
    • toHsla(?float $alpha = null): Returns an Hsla instance.
    • toHsl(): Returns an Hsl instance.
    • toHsb(): Returns an Hsb instance.
    • toCmyk(): Returns a Cmyk instance.
    • toCIELab(): Returns a CIELab instance.
    • toXyz(): Returns an Xyz instance.
    • toHex(?string $alpha = null): Returns a new Hex instance, optionally allowing you to override the alpha channel.
    use Spatie\Color\Hex;
    
    $hex = Hex::fromString('#3498db');
    
    $rgb = $hex->toRgb();
    $hsl = $hex->toHsl();
    $cmyk = $hex->toCmyk();
    $hexWithNewAlpha = $hex->toHex('00'); // Returns #3498db00