Skip to content

Commit

Permalink
Merge pull request spatie#67 from AstroCorp/fix/rgba-opacity
Browse files Browse the repository at this point in the history
Fix rgba opacity
  • Loading branch information
freekmurze authored Apr 12, 2022
2 parents 92e9bea + 9441351 commit 351f82e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ echo $ratio; // 15.0

$cie76_distance = Distance::CIE76($rgb, $hex2);
$cie76_distance = Distance::CIE76('rgba(55,155,255,1)', '#2d78c8'); // Outputs the same thing, Factory is built-in to all comparison functions
echo $cie76_distance; // 55.894680426674
echo $cie76_distance; // 55.89468042667388

$cie94_distance = Distance::CIE94($rgb, $hex2);
echo $cie94_distance; // 13.490919427908
echo $cie94_distance; // 13.49091942790753

$cie94_textiles_distance = Distance::CIE94($rgb, $hex2, 1); // Third parameter optionally sets the application type (0 = Graphic Arts [Default], 1 = Textiles)
echo $cie94_textiles_distance; // 7.0926538068477

$ciede2000_distance = Distance::CIEDE2000($rgb, $hex2);
echo $ciede2000_distance; // 12.711957696301
echo $ciede2000_distance; // 12.711957696300898
```

## Support us
Expand Down
2 changes: 1 addition & 1 deletion src/Rgba.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function fromString(string $string)
Validate::rgbaColorString($string);

$matches = null;
preg_match('/rgba\( *(\d{1,3} *, *\d{1,3} *, *\d{1,3} *, *[0-1](\.\d{1,2})?) *\)/i', $string, $matches);
preg_match('/rgba\( *(\d{1,3} *, *\d{1,3} *, *\d{1,3} *, *[0-1](\.\d{1,})?) *\)/i', $string, $matches);

$channels = explode(',', $matches[1]);
[$red, $green, $blue, $alpha] = array_map('trim', $channels);
Expand Down
2 changes: 1 addition & 1 deletion src/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static function rgbColorString($string): void

public static function rgbaColorString($string): void
{
if (! preg_match('/^ *rgba\( *\d{1,3} *, *\d{1,3} *, *\d{1,3} *, *[0-1](\.\d{1,2})? *\) *$/i', $string)) {
if (! preg_match('/^ *rgba\( *\d{1,3} *, *\d{1,3} *, *\d{1,3} *, *[0-1](\.\d{1,})? *\) *$/i', $string)) {
throw InvalidColorValue::malformedRgbaColorString($string);
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/DistanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public function it_can_compare_distance_using_CIE76()
$color2 = Hex::fromString('#2d78c8');
$distance = Distance::CIE76($color1, $color2);

$this->assertSame(55.894680426674, $distance);
$this->assertSame(55.89468042667388, $distance);
}

/** @test */
public function it_can_compare_distance_using_CIE76_and_string_colors()
{
$distance = Distance::CIE76('rgb(55,155,255)', '#2d78c8');

$this->assertSame(55.894680426674, $distance);
$this->assertSame(55.89468042667388, $distance);
}

/** @test */
Expand All @@ -34,15 +34,15 @@ public function it_can_compare_distance_using_CIE94()
$color2 = Hex::fromString('#2d78c8');
$distance = Distance::CIE94($color1, $color2);

$this->assertSame(13.490919427908, $distance);
$this->assertSame(13.49091942790753, $distance);
}

/** @test */
public function it_can_compare_distance_using_CIE94_and_string_colors()
{
$distance = Distance::CIE94('rgb(55,155,255)', '#2d78c8');

$this->assertSame(13.490919427908, $distance);
$this->assertSame(13.49091942790753, $distance);
}

/** @test */
Expand All @@ -52,14 +52,14 @@ public function it_can_compare_distance_using_CIEDE2000()
$color2 = Hex::fromString('#2d78c8');
$distance = Distance::CIEDE2000($color1, $color2);

$this->assertSame(12.711957696301, $distance);
$this->assertSame(12.711957696300898, $distance);
}

/** @test */
public function it_can_compare_distance_using_CIEDE2000_and_string_colors()
{
$distance = Distance::CIEDE2000('rgb(55,155,255)', '#2d78c8');

$this->assertSame(12.711957696301, $distance);
$this->assertSame(12.711957696300898, $distance);
}
}
12 changes: 12 additions & 0 deletions tests/RgbaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ public function it_can_be_created_from_a_string()
$this->assertSame(0.5, $rgba->alpha());
}

/** @test */
public function it_can_be_created_from_a_string_with_3_decimals_in_opacity()
{
$rgba = Rgba::fromString('rgba(55,155,255,0.555)');

$this->assertInstanceOf(Rgba::class, $rgba);
$this->assertSame(55, $rgba->red());
$this->assertSame(155, $rgba->green());
$this->assertSame(255, $rgba->blue());
$this->assertSame(0.555, $rgba->alpha());
}

/** @test */
public function it_can_be_created_from_a_string_with_spaces()
{
Expand Down

0 comments on commit 351f82e

Please sign in to comment.