PHP code example of delights / color

1. Go to this page and download the library: Download delights/color library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

delights / color example snippets


use Delight\Color\Generator;

Generator::one();
Generator::many(n: 10)
Generator::manyLazily(n: 10_000)

use Delight\Color\Generator;

Generator::withDefaults(
    hue: [100, 200],
    saturation: 50,
    lightness: [40, 60]
);

Generator::withDefaults(
    hue: [120, 140] // just restrict the hue but keep the saturation and lightness settings
);

$avatarColor = Generator::one(seed: $email); // will always return the same color for the given seed.

use Delight\Color\Generator;

$avatarColor = Generator::one(
    hue: [100, 200],
    lightness: [40, 80]
);
$avatarColor = Generator::many(
    hue: null, // use global defaults
    saturation: [100, 100]
    lightness: [40, 80]
);
$avatarColor = Generator::manyLazily(
    lightness: [50, 60]
)

use Delight\Color\Generator;

$avatarColor = Generator::one(hue: [0, 360], lightness: 50, saturation: 100)

\Delight\Color\Hsl::fromRGB(255, 0, 0);

\Delight\Color\Hsl::fromHex("#FF0000")
\Delight\Color\Hsl::fromHex("FF0000")

\Delight\Color\Hsl::fromString("rgb(255, 0,0)")
\Delight\Color\Hsl::fromString("rgba(255, 0,0, .5)") // silently ignores the transparency
\Delight\Color\Hsl::fromString("hsl(144, 100%, 14.4)")

use Delight\Color\Hsl;

$color = new Hsl(100, 20, 20);
$color = new Hsl(100, .2, .2); // automatically normalized to 0-100

Hsl::boundedRandom([0, 360], [0,100], [0,100], $seed)

Hsl::random($seed);

$color->toHex(); // #000000
$color->toRgb(); // rgb(0, 0, 0)
$color->toHsl(); // hsl(0, 0, 0)

$color->hue; # between 0-360
$color->saturation; # between 0-100
$color->lightness; # between 0-100

$color->setHue(...)->setSaturation(...)->setLightness(...); // modifies the color
$color->withHue(...) // returns a new instance
$color->withSaturation(...); // returns a new instance
$color->withLightness(...); // returns a new instance

// If you chain more than one with...(), use clone() + set...() instead:
$color->clone() 
     ->setHue(...)
     ->setSaturation(...)
     ->setLightness().


$color->colorChannels(); // returns [r, g, b]
$color->red(); // 0-255
$color->green(); // 0-255
$color->blue(); // 0-255

$color->isDark();
$color->isBright();

$color->isDark(threshold: 5);

// Returns a new instance of the color
$color->darken($percentage = 15);
$color->lighten($percentage = 15);

$color->luminance(); // 0.0 - 1.0

$color->contrast($otherColor); // 1 - 21