Last active
November 4, 2022 16:39
-
-
Save thekid/795c35bec603c0f217cb5445cc901ded to your computer and use it in GitHub Desktop.
Create examples for https://github.com/thekid/dialog/pull/36
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use de\thekid\dialog\color\Colors; | |
use img\Image; | |
use img\io\StreamReader; | |
use io\{File, Path, Folder}; | |
use util\cmd\Console; | |
use util\profiling\Timer; | |
class Glob implements IteratorAggregate { | |
public function __construct(private Path $base, private string $pattern) { } | |
public function getIterator(): Traversable { | |
foreach (glob($this->base.$this->pattern, GLOB_NOSORT | GLOB_BRACE) as $file) { | |
yield new Path($file); | |
} | |
} | |
} | |
$out= (new File('out.html'))->out(); | |
$out->write('<!DOCTYPE html><html lang="en"><head>'); | |
$out->write('<style type="text/css">body { margin: 0; padding: 0; } * { box-sizing: border-box; }</style>'); | |
$out->write('</head><body>'); | |
$out->write('<div style="width: 100%; padding: 1rem">'); | |
$colors= new Colors(10); | |
$out->write('<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem">'); | |
$t= (new Timer())->start(); | |
foreach (array_slice($argv, 1) as $input) { | |
$p= new Path($input); | |
foreach ($p->isFile() ? [$p] : new Glob($p, 'thumb*.webp') as $entry) { | |
Console::writeLine($entry); | |
$palette= $colors->palette(Image::loadFrom(new StreamReader($entry->asFile())), 5); | |
$boxes= ''; | |
foreach ($palette as $color) { | |
$boxes.= '<div style="background: '.$color->toHex().'; width: 2.5rem; height: 2.5rem"></div>'; | |
} | |
$ambient= "{$palette[0]->red} {$palette[0]->green} {$palette[0]->blue}"; | |
$out->write('<div> | |
<div> | |
<img alt="Fixture" width="100%" style="aspect-ratio: 16 / 10; object-fit: cover; border: .5rem solid rgb('.$ambient.' / .4)" src="'.$entry->toString('/').'"> | |
</div> | |
<div style="display: flex; gap: 1rem; margin-top: .5rem"> | |
'.$boxes.' | |
</div> | |
</div>'); | |
} | |
} | |
$t->stop(); | |
$out->write('</div>'); | |
$out->write('</div></body>'); | |
$out->close(); | |
Console::writeLinef('Quality %d -> %.3f seconds', $colors->quality, $t->elapsedTime()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compare to https://lokeshdhakar.com/projects/color-thief/#examples:
The first color is what Colorthief.js regards as the dominant color.