PHP code example of konradmichalik / php-color

1. Go to this page and download the library: Download konradmichalik/php-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/ */

    

konradmichalik / php-color example snippets


use KonradMichalik\Color\Color;

$color = Color::fromHex('#3366cc');

$color->toRgb();          // Rgb(51, 102, 204)
$color->toHsl();          // Hsl(220.0, 60.0, 50.0)
$color->toHex();          // "#3366cc"

Color::fromRgb(51, 102, 204)->toHex();      // "#3366cc"
Color::fromHsl(220, 60, 50)->toHex();       // "#3366cc"
$color->withLightness(85)->toHex();         // a lighter variant (HSL space)
$color->scaleRgb(0.5)->toHex();             // halve each RGB channel → "#1a3366"
$color->darken(0.5)->toHex();               // alias for scaleRgb()

use KonradMichalik\Color\ColorHasher;

ColorHasher::crc32()->hash($string)->scaleRgb(0.5)->toHex(); // darkened avatar color

Color::fromString('#ff0000');          // red
Color::fromString('f00');              // red
Color::fromString('rgb(255, 0, 0)');   // red
Color::fromString('hsl(0, 100%, 50%)'); // red
Color::fromString('HSL(0,100,50)');    // red

Color::tryFromString($userInput) ?? Color::fromHex('#cccccc');

$rgb = Color::fromHex('#ff0000')->toRgb();

$rgb->toCssString();        // "rgb(255, 0, 0)"
$rgb->toCssString(0.8);     // "rgba(255, 0, 0, 0.8)"

Color::fromHex('#ff0000')->toRgbaString(0.8); // "rgba(255, 0, 0, 0.8)"

$background = Color::fromHex('#222222');

$background->relativeLuminance();                  // 0.0185…
$background->contrastRatio(Color::fromHex('#fff')); // 15.9…
$background->isDark();                              // true
$background->optimalTextColor()->toHex();          // "#ffffff"

use KonradMichalik\Color\ColorHasher;

$hasher = ColorHasher::hsl();              // balanced SHA-256 → HSL (recommended)
$hasher->hash('[email protected]');       // stable Color

$hasher = ColorHasher::hsl(saturation: 70, lightness: 45);
$hasher = ColorHasher::crc32();            // lightweight CRC32 → hex
bash
composer