Skip to content

Commit

Permalink
Added named colors (#87)
Browse files Browse the repository at this point in the history
* Added named colors

* Added named colors
  • Loading branch information
vanodevium authored Sep 20, 2024
1 parent 2a3bb4e commit 8cb01a9
Show file tree
Hide file tree
Showing 9 changed files with 863 additions and 5 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/color.svg?style=flat-square)](https://packagist.org/packages/spatie/color)
![Tests](https://github.com/spatie/color/workflows/Tests/badge.svg)

A little library to handle color conversions and comparisons. Currently supports rgb, rgba, hex, hsl, hsla, CIELab, and xyz color formats as well as CIE76, CIE94, and CIEDE2000 color comparison algorithms.
A little library to handle color conversions and comparisons. Currently, supports CSS names, rgb, rgba, hex, hsl, hsla, CIELab, and xyz color formats as well as CIE76, CIE94, and CIEDE2000 color comparison algorithms.

```php
$named = Named::fromString('peru'); // case-insensitive

echo $named->red(); // 205
echo $named->green(); // 133
echo $named->blue(); // 63

echo $named->toHex(); // #cd853f

$rgb = Rgb::fromString('rgb(55,155,255)');

echo $rgb->red(); // 55
Expand Down Expand Up @@ -102,6 +110,7 @@ There are seven classes which implement the `Color` interface:
Parses a color string and returns a `Color` implementation, depending on the format of the input string.

```php
Named::fromString('blue');
Hex::fromString('#000000');
Rgba::fromString('rgba(255, 255, 255, 1)');
Hsla::fromString('hsla(360, 100%, 100%, 1)');
Expand Down Expand Up @@ -250,6 +259,7 @@ With the `Factory` class, you can create a color instance from any string (it do

```php
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
Factory::fromString('Hello world!'); // `InvalidColorValue` exception
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/InvalidColorValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,9 @@ public static function xyzValueNotInRange(float $value, string $name, float $min
{
return new static("Xyz value `{$name}` must be a number between $min and $max");
}

public static function malformedNamedColorString(string $string): self
{
return new static("Color string `{$string}` doesn't match any of the available colors.");
}
}
1 change: 1 addition & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function fromString(string $string): Color
protected static function getColorClasses(): array
{
return [
Named::class,
CIELab::class,
Cmyk::class,
Hex::class,
Expand Down
8 changes: 4 additions & 4 deletions src/Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class Hex implements Color

public function __construct(string $red, string $green, string $blue, string $alpha = 'ff')
{
Validate::hexChannelValue($red, 'red');
Validate::hexChannelValue($green, 'green');
Validate::hexChannelValue($blue, 'blue');
Validate::hexChannelValue($alpha, 'alpha');
Validate::hexChannelValue($red);
Validate::hexChannelValue($green);
Validate::hexChannelValue($blue);
Validate::hexChannelValue($alpha);

$this->red = strtolower($red);
$this->green = strtolower($green);
Expand Down
38 changes: 38 additions & 0 deletions src/Named.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Spatie\Color;

use Spatie\Color\Exceptions\InvalidColorValue;

class Named extends Rgb
{
use Names;

protected $name;

public function __construct(string $name)
{

$this->name = strtolower($name);

if (! array_key_exists($this->name, $this->names)) {
throw InvalidColorValue::malformedNamedColorString($name);
}

$color = $this->names[$this->name];

parent::__construct($color[0], $color[1], $color[2]);
}

public static function fromString(string $string)
{
Validate::namedColorString($string);

return new static($string);
}

public function __toString(): string
{
return $this->name;
}
}
Loading

0 comments on commit 8cb01a9

Please sign in to comment.