Skip to content

Commit

Permalink
Color: ensure values are within range (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
kornrunner committed Jan 18, 2021
1 parent 5c7d339 commit df1edb7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public static function toLinear(int $value): float {

public static function tosRGB(float $value): int {
$normalized = max(0, min(1, $value));
return ($normalized <= 0.0031308)
$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);
return max(0, min($result, 255));
}
}
2 changes: 1 addition & 1 deletion test/DCTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DCTest extends TestCase {

public function testEncode () {
$this->assertSame (65793, DC::encode ([0, 0, 0], 1));
$this->assertSame (16843008, DC::encode ([255, 255, 255], 1));
$this->assertSame (16777215, DC::encode ([255, 255, 255], 1));
$this->assertSame (65793, DC::encode ([-1, -1, -1], 1));
}

Expand Down

0 comments on commit df1edb7

Please sign in to comment.