Skip to content

Commit

Permalink
Merge branch 'master' of github.com:breadthe/php-contrast
Browse files Browse the repository at this point in the history
  • Loading branch information
breadthe committed Mar 29, 2020
2 parents a6a3fbb + a57a03e commit dcbc8d4
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/ColorPair.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(float $ratio = null, string $color1 = null, string $

/**
* '000' => '#000000'
* '000000' => '#000000'
* '000000' => '#000000'.
*/
// protected function normalize($color)
// {
Expand Down
10 changes: 5 additions & 5 deletions src/HexColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ public static function random(): self

/**
* '#abc' or 'abc' => '#aabbcc'
* '#abcdef' or 'abcdef' => '#abcdef'
* '#abcdef' or 'abcdef' => '#abcdef'.
*/
protected function normalize(string $hexColor)
{
// '#abcdef' or 'abcdef'
if (preg_match('/^\s*#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\s*$/i', $hexColor, $matches)) {
return sprintf("#%s%s%s", $matches[1], $matches[2], $matches[3]);
return sprintf('#%s%s%s', $matches[1], $matches[2], $matches[3]);
}

// '#abc' or 'abc'
if (preg_match('/^\s*#?([0-9a-f])([0-9a-f])([0-9a-f])\s*$/i', $hexColor, $matches)) {
return sprintf("#%s%s%s", str_repeat($matches[1], 2), str_repeat($matches[2], 2),
return sprintf('#%s%s%s', str_repeat($matches[1], 2), str_repeat($matches[2], 2),
str_repeat($matches[3], 2));
}

return null; // TODO throw
// TODO throw
}

/**
* @see https://stackoverflow.com/questions/5614530/generating-a-random-hex-color-code-with-php
*/
protected static function randomColor()
{
return static::randomColorPart() . static::randomColorPart() . static::randomColorPart();
return static::randomColorPart().static::randomColorPart().static::randomColorPart();
}

protected static function randomColorPart()
Expand Down
8 changes: 4 additions & 4 deletions src/HexColorPair.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public function getSibling(string $hexValue): HexColor

protected static function calculateRatio(HexColor $fg = null, HexColor $bg = null)
{
if (!$fg || !$bg) {
return null;
if (! $fg || ! $bg) {
return;
}

$fgLuminance = static::luminance($fg);
Expand Down Expand Up @@ -108,11 +108,11 @@ protected static function luminance(HexColor $color)
$g = ($gSrgb <= 0.03928) ? $gSrgb / 12.92 : pow((($gSrgb + 0.055) / 1.055), 2.4);
$b = ($bSrgb <= 0.03928) ? $bSrgb / 12.92 : pow((($bSrgb + 0.055) / 1.055), 2.4);

return (0.2126 * $r + 0.7152 * $g + 0.0722 * $b);
return 0.2126 * $r + 0.7152 * $g + 0.0722 * $b;
}

/**
* '#abcdef' => ['ab', 'cd', 'ef']
* '#abcdef' => ['ab', 'cd', 'ef'].
*/
protected static function rgbHexChannels(HexColor $hexColor): array
{
Expand Down
8 changes: 4 additions & 4 deletions src/TailwindColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct()
}

/**
* Returns all the default Tailwind colors
* Returns all the default Tailwind colors.
*/
public static function colors(): array
{
Expand All @@ -29,7 +29,7 @@ public function getColors(): array
}

/**
* Returns a random Tailwind color
* Returns a random Tailwind color.
*/
public static function random(): HexColor
{
Expand Down Expand Up @@ -69,7 +69,7 @@ public static function merge(array $customPalette): self
}

/**
* Returns a random pair of accessible (min. contrast 3:1) Tailwind colors
* Returns a random pair of accessible (min. contrast 3:1) Tailwind colors.
*/
public static function randomPair(): HexColorPair
{
Expand Down Expand Up @@ -122,6 +122,6 @@ protected function flatten(array $twColorsArr): Collection

protected function parseDefaultTailwindColors()
{
return json_decode(file_get_contents(__DIR__ . '/../stubs/twcolors.json'), true);
return json_decode(file_get_contents(__DIR__.'/../stubs/twcolors.json'), true);
}
}
10 changes: 5 additions & 5 deletions tests/HexColorPairTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,23 @@ public function provideContrastData()
{
yield 'maximum contrast' => [
21,
['000', 'fff',],
['000', 'fff'],
];
yield 'no contrast - black on black' => [
1,
['000', '000',],
['000', '000'],
];
yield 'no contrast - white on white' => [
1,
['fff', 'fff',],
['fff', 'fff'],
];
yield 'high contrast' => [
18.4,
['300', 'fff',],
['300', 'fff'],
];
yield 'low contrast' => [
1.1,
['300', '000',],
['300', '000'],
];
}
}
1 change: 0 additions & 1 deletion tests/HexColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Breadthe\PhpContrast\Tests;

use Breadthe\PhpContrast\HexColor;
use Breadthe\PhpContrast\HexColorPair;
use PHPUnit\Framework\TestCase;

class HexColorTest extends TestCase
Expand Down
3 changes: 1 addition & 2 deletions tests/TailwindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Breadthe\PhpContrast\HexColorPair;
use Breadthe\PhpContrast\TailwindColor;
use PHPUnit\Framework\TestCase;
use Tightenco\Collect\Support\Collection;

class TailwindTest extends TestCase
{
Expand Down Expand Up @@ -47,7 +46,7 @@ public function it_returns_random_color_pairs_of_minimum_3_to_1_ratio()
/** @test */
public function it_can_merge_default_and_custom_colors()
{
$customPalette = json_decode(file_get_contents(__DIR__ . '/../stubs/custom-palette.json'), true);
$customPalette = json_decode(file_get_contents(__DIR__.'/../stubs/custom-palette.json'), true);

$colors = TailwindColor::merge($customPalette)->getColors();
$customPalette = collect($customPalette)
Expand Down

0 comments on commit dcbc8d4

Please sign in to comment.