Skip to content

Commit

Permalink
Updated PdfRgbColor class.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jan 30, 2025
1 parent 89b9c03 commit 1f23003
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Updated `PdfRgbColor` class.
- Updated Symfony to version 7.2.3.
- Updated Rector to version 2.0.
- Updated cache configurations.
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 24 additions & 31 deletions src/Color/PdfRgbColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public function __toString(): string
);
}

/**
* Gets the hexadecimal representation of these values.
*
* @param string $prefix the optional prefix to prepend
*
* @return string the hexadecimal value as six lower case characters (like <code>'ff8040'</code>)
*/
public function asHex(string $prefix = ''): string
{
return \sprintf('%s%02x%02x%02x', $prefix, $this->red, $this->green, $this->blue);
}

/**
* Gets the black color.
*
Expand All @@ -61,24 +73,28 @@ public static function blue(): self
}

/**
* Try to create an RGB color from the given value.
* Try to create an RGB color from the given string.
*
* Note: This function will ignore any non-hexadecimal characters it encounters.
*
* @param int|string|null $value the value to parse. An integer value or a hexadecimal string
* like <code>'FF8040'</code> or <code>'FFF'</code>
* @param ?string $value the value to parse. A hexadecimal string
* like <code>'FF8040'</code> or <code>'FFF'</code>
*
* @return PdfRgbColor|null the RGB color, if applicable, null otherwise
*/
public static function create(int|string|null $value): ?self
public static function create(?string $value): ?self
{
if (null === $value || '' === $value) {
return null;
}

if (\is_int($value)) {
return self::createFromInt($value);
}
$value = (string) \preg_replace('/[^0-9A-F]/i', '', $value);

return self::createFromString($value);
return match (\strlen($value)) {
3 => self::createFrom3Chars($value),
6 => self::createFrom6Chars($value),
default => null,
};
}

/**
Expand Down Expand Up @@ -231,29 +247,6 @@ private static function createFrom6Chars(string $value): self
return self::instance($red, $green, $blue);
}

private static function createFromInt(int $value): self
{
/** @var int<0, 255> $red */
$red = 0xFF & ($value >> 0x10);
/** @var int<0, 255> $green */
$green = 0xFF & ($value >> 0x08);
/** @var int<0, 255> $blue */
$blue = 0xFF & $value;

return self::instance($red, $green, $blue);
}

private static function createFromString(string $value): ?self
{
$value = (string) \preg_replace('/[^0-9A-F]/i', '', $value);

return match (\strlen($value)) {
3 => self::createFrom3Chars($value),
6 => self::createFrom6Chars($value),
default => null,
};
}

/**
* @return int<0, 255>
*/
Expand Down
65 changes: 56 additions & 9 deletions tests/Color/PdfRgbColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,51 @@
use fpdf\Color\PdfCmykColor;
use fpdf\Color\PdfGrayColor;
use fpdf\Color\PdfRgbColor;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class PdfRgbColorTest extends TestCase
{
public static function getHexColors(): \Iterator
{
$rgb = [0x00, 0x00, 0x00];
yield [$rgb[0], $rgb[1], $rgb[2], '000000'];
yield [$rgb[0], $rgb[1], $rgb[2], '0x000000', '0x'];

$rgb = [0xFF, 0xFF, 0xFF];
yield [$rgb[0], $rgb[1], $rgb[2], 'ffffff'];
yield [$rgb[0], $rgb[1], $rgb[2], '0xffffff', '0x'];

$rgb = [0x32, 0x64, 0x96];
yield [$rgb[0], $rgb[1], $rgb[2], '326496'];
yield [$rgb[0], $rgb[1], $rgb[2], '0x326496', '0x'];

$rgb = [0x00, 0x64, 0x96];
yield [$rgb[0], $rgb[1], $rgb[2], '006496'];
yield [$rgb[0], $rgb[1], $rgb[2], '0x006496', '0x'];

$rgb = [0x00, 0xFF, 0x00];
yield [$rgb[0], $rgb[1], $rgb[2], '00ff00'];
yield [$rgb[0], $rgb[1], $rgb[2], '0x00ff00', '0x'];

$rgb = [0x00, 0x00, 0xFF];
yield [$rgb[0], $rgb[1], $rgb[2], '0000ff'];
yield [$rgb[0], $rgb[1], $rgb[2], '0x0000ff', '0x'];
}

/**
* @param int<0, 255> $red
* @param int<0, 255> $green
* @param int<0, 255> $blue
*/
#[DataProvider('getHexColors')]
public function testAsHex(int $red, int $green, int $blue, string $expected, string $prefix = ''): void
{
$color = PdfRgbColor::instance($red, $green, $blue);
$actual = $color->asHex($prefix);
self::assertSame($expected, $actual);
}

public function testBlack(): void
{
$actual = PdfRgbColor::black();
Expand Down Expand Up @@ -53,23 +94,29 @@ public function testCreate(): void
$actual = PdfRgbColor::create('FA');
self::assertNull($actual);

$actual = PdfRgbColor::create(0);
$actual = PdfRgbColor::create('FAB');
self::assertInstanceOf(PdfRgbColor::class, $actual);
self::assertSameColor($actual, 0, 0, 0);

// 3296100
$value = 50 << 0x10 | 75 << 0x08 | 100;
$actual = PdfRgbColor::create($value);
self::assertInstanceOf(PdfRgbColor::class, $actual);
self::assertSameColor($actual, 50, 75, 100);
self::assertSameColor($actual, 255, 170, 187);

$actual = PdfRgbColor::create('#FAB');
self::assertInstanceOf(PdfRgbColor::class, $actual);
self::assertSameColor($actual, 255, 170, 187);

$actual = PdfRgbColor::create('#FFAABB');
$actual = PdfRgbColor::create('#FAB#');
self::assertInstanceOf(PdfRgbColor::class, $actual);
self::assertSameColor($actual, 255, 170, 187);

$actual = PdfRgbColor::create('FF00BB');
self::assertInstanceOf(PdfRgbColor::class, $actual);
self::assertSameColor($actual, 255, 0, 187);

$actual = PdfRgbColor::create('#FF00BB');
self::assertInstanceOf(PdfRgbColor::class, $actual);
self::assertSameColor($actual, 255, 0, 187);

$actual = PdfRgbColor::create('#FF00BB#');
self::assertInstanceOf(PdfRgbColor::class, $actual);
self::assertSameColor($actual, 255, 0, 187);
}

public function testDarkGray(): void
Expand Down
12 changes: 6 additions & 6 deletions vendor-bin/php-cs-fixer/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f23003

Please sign in to comment.