Color.php 559 B

12345678910111213141516171819
  1. <?php
  2. namespace App\Util\Blurhash;
  3. final class Color {
  4. public static function toLinear(int $value): float {
  5. $value = $value / 255;
  6. return ($value <= 0.04045)
  7. ? $value / 12.92
  8. : pow(($value + 0.055) / 1.055, 2.4);
  9. }
  10. public static function tosRGB(float $value): int {
  11. $normalized = max(0, min(1, $value));
  12. return ($normalized <= 0.0031308)
  13. ? (int) round($normalized * 12.92 * 255 + 0.5)
  14. : (int) round((1.055 * pow($normalized, 1 / 2.4) - 0.055) * 255 + 0.5);
  15. }
  16. }