mirror of https://github.com/pixelfed/pixelfed
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
871 B
PHP
30 lines
871 B
PHP
<?php
|
|
|
|
namespace App\Util\Blurhash;
|
|
|
|
final class Color
|
|
{
|
|
public static function toLinear(int $value): float
|
|
{
|
|
$value = $value / 255;
|
|
|
|
return ($value <= 0.04045)
|
|
? $value / 12.92
|
|
: pow(($value + 0.055) / 1.055, 2.4);
|
|
}
|
|
|
|
public static function tosRGB(float $value): int
|
|
{
|
|
$normalized = max(0, min(1, $value));
|
|
|
|
$result = ($normalized <= 0.0031308)
|
|
? (int) round($normalized * 12.92 * 255 + 0.5)
|
|
: (int) round((1.055 * pow($normalized, 1 / 2.4) - 0.055) * 255 + 0.5);
|
|
|
|
// The + 0.5 rounds a fully saturated channel up to 256, which does not fit
|
|
// in the 8 bits DC::encode() packs it into and carries into the next channel
|
|
// (pure white came out as R=257 G=1 B=0). Clamp like upstream php-blurhash.
|
|
return max(0, min($result, 255));
|
|
}
|
|
}
|