README.md

    Colors (RGB, HSL, objects & converters)

    Color models

    Interface

    The ColorInterface interface is a common abstraction for all color models of the package. It contains only one public constant MAXIMALS, which is overridden in descendant classes and contains an associative array whose keys are the names of the model’s property fields, and whose values ​​are the maximum permissible value for this property (the minimum is always 0).

    Extends the Stringable interface - required to obtain a unique color model name.

    /!\ When adding your own color models, you MUST implement this interface.

    Abstraction

    The AbstractColor class is abstract, implements the ColorInterface interface, contains methods common to all color models of the package, for which it is a common parent class.

    /!\ When adding your own color models, you CAN inherit from this abstraction.

    Built-in color models

    All classes contain:

    1. An overridden MAXIMALS constant with names and values ​​for the given model.
    2. A constructor with the names of the property fields specified (all are real numbers, public and read-only); the last optional property is an alpha channel in the range from 0 to 1 (default is 1).
    3. A call to the built-in checkProperties() method in the constructor to check if the properties are in the range from 0 to MAXIMALS.
    Class Mandatory properties (name(maximal)) Model
    ColorCMYK cyan(100), magenta(100), yellow(100), black(100) CMYK
    ColorHSB hue(360), saturation(1), brightness(1) HSV (HSB)
    ColorHSL hue(360), saturation(1), lightness(1) HSL
    not realized not realized LAB
    ColorRGB red(255), green(255), blue(255) RGB

    Casting instances of these classes to a string yields the actual names of the classes themselves.

    Color models converters

    Interface

    The Converter\ConverterInterface interface is a common abstraction for all converters in the package. It contains only two public constants SOURCE and TARGET, which are overridden in descendant classes, and contains the names of classes implementing the source and target color models. Contains only one public method process(ColorInterface $color): ColorInterface to convert a color from one model to another.

    Also contains a public static method getName(string $source, string $target): string to get a unique converter name based on color model names.

    Extends the Stringable interface - required to obtain a unique converter name.

    /!\ When adding your own converters, you MUST implement this interface.

    Abstraction

    The Converter\AbstractConverter class is abstract, implements the Converter\ConverterInterface interface, contains methods common to all color models of the package, for which it is a common parent class. Implements the process(ColorInterface $color): ColorInterface method as a template method:

    1. Checks that the input color is supported by this converter.
    2. Calls the abstract execute(ColorInterface $color): ColorInterface method, which must be defined in descendant classes.

    /!\ When adding your own converters, you CAN inherit from this abstraction.

    Built-in converters

    Conversion Class
    CMYK->RGB Converter\CmykToRgbConverter
    HSL->HSV Converter\HslToHsbConverter
    HSL->RGB Converter\HslToRgbConverter
    HSV->HSL Converter\HsbToHslConverter
    HSV->RGB Converter\HsbToRgbConverter
    RGB->CMYK Converter\RgbToCmykConverter
    RGB->HSL Converter\RgbToHslConverter
    RGB->HSV Converter\RgbToHsbConverter

    Casting instances of these classes to a string produces a formatted string containing the names of the source and destination color models.

    Registry of converters

    Class Converter\ConvertersRegistry contains all possible color scheme converters: built-in ones are added automatically, custom ones must be registered via the add(Converter\ConverterInterface $converter): self method.

    You can get the required converter either by its name (getByName(string $name): Converter\ConverterInterface) or by the names of the source and target color models (getByPath(string $source, string $target): Converter\ConverterInterface).

    The registry is an iterable object, meaning it behaves like an array.

    Facade for converters

    Class Converter is a facade for all conversions and related actions:

    1. Actually performs the conversion: process(ColorInterface $color, string $target): ColorInterface.
    2. Registers new converters: register(Converter\ConverterInterface $converter): self.
    3. Builds a directed graph of conversions where nodes are color models and edges are converters: graph(): \Fhaculty\Graph\Graph.
    4. Searches for a chain of conversions if there is no direct converter; this action is performed using Dijkstra’s algorithm: chain(class-string<ColorInterface> $source, class-string<ColorInterface> $target): array<Converter\ConverterInterface>.

    The last two actions are implemented using the graphp/algorithms plug-in library.

    graphviz

    Sample

    Code:

    use Jzucen\Colors;
    
    require_once(__DIR__ . '/vendor/autoload.php');
    
    $converter = new Colors\Converter();
    $origin = new Colors\ColorHSB(90, 0.12, 0.34);
    $result = $converter->process($origin, Colors\ColorCMYK::class);
    
    echo var_export($origin, true), PHP_EOL,
        var_export($result, true), PHP_EOL;
    

    Result:

    \Jzucen\Colors\ColorHSB::__set_state(array(
       'hue' => 90,
       'saturation' => 0.12,
       'brightness' => 0.34,
       'alpha' => 1,
    ))
    \Jzucen\Colors\ColorCMYK::__set_state(array(
       'cyan' => 6.000000000000007,
       'magenta' => 0.0,
       'yellow' => 12.0,
       'black' => 65.99999999999999,
       'alpha' => 1,
    ))
    

    Color converters online

    Конвейеры
    0 успешных
    0 с ошибкой