Skip to content

Commit

Permalink
Updated PDF colors.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Feb 4, 2025
1 parent e2ebd43 commit 97c5d47
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 51 deletions.
6 changes: 2 additions & 4 deletions src/Interfaces/ComparableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ interface ComparableInterface
*
* @param ComparableInterface $other the other instance to compare with
*
* @return int 0 if this instance is equal to the other instance, -1 if this instance is less than the other
* instance and 1 if this instance is greater than the other instance
* @return int<-1, 1> 0 if this instance is equal to the other instance, -1 if this instance is less than the other
* instance and 1 if this instance is greater than the other instance
*
* @psalm-param TComparable $other
*
* @psalm-return int<-1, 1>
*/
public function compare(self $other): int;
}
2 changes: 0 additions & 2 deletions src/Pdf/Interfaces/PdfColorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public function applyTextColor(PdfDocument $doc): void;
* Gets this value converted to RGB.
*
* @return array{0: int, 1: int, 2: int}
*
* @see PdfColorInterface::asInt()
*/
public function asRGB(): array;

Expand Down
21 changes: 10 additions & 11 deletions src/Pdf/Traits/PdfColorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,14 @@ public function applyTextColor(PdfDocument $doc): void
}

/**
* @return array{0: int, 1: int, 2: int}
*
* @psalm-return array{0: int<0, 255>, 1: int<0, 255>, 2: int<0, 255>}
* @return array{0: int<0, 255>, 1: int<0, 255>, 2: int<0, 255>}
*/
public function asRGB(): array
{
$hex = $this->getPhpOfficeColor();

/** @psalm-var array{0: int<0, 255>, 1: int<0, 255>, 2: int<0, 255>} */
return [
$this->hex2int(\substr($hex, 0, 2)),
$this->hex2int(\substr($hex, 2, 2)),
$this->hex2int(\substr($hex, 4, 2)),
$this->hexdec($this->value, 1),
$this->hexdec($this->value, 3),
$this->hexdec($this->value, 5),
];
}

Expand Down Expand Up @@ -94,8 +89,12 @@ public function getTextColor(): PdfTextColor
return $color;
}

private function hex2int(string $hex): int
/**
* @return int<0, 255>
*/
private function hexdec(string $value, int $offset): int
{
return (int) \hexdec($hex);
/** @var int<0, 255> */
return (int) \hexdec(\substr($value, $offset, 2));
}
}
54 changes: 26 additions & 28 deletions src/Report/HtmlColorsReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
use App\Pdf\Html\HtmlBootstrapColor;
use App\Pdf\Html\HtmlColorName;
use App\Pdf\Html\HtmlGrayedColor;
use App\Pdf\Interfaces\PdfColorInterface;
use App\Pdf\PdfCell;
use App\Pdf\PdfColumn;
use App\Pdf\PdfGroupTable;
use App\Pdf\PdfStyle;
use App\Utils\FormatUtils;
use fpdf\Enums\PdfFontName;

/**
* Report outputting HTML color names.
*
* @psalm-type ColorType = HtmlColorName|HtmlBootstrapColor|HtmlGrayedColor
*/
class HtmlColorsReport extends AbstractReport
{
Expand All @@ -37,72 +39,68 @@ public function render(): bool
$this->outputTable('Html by Name', $colors);

$this->addPage();
$this->sortColors($colors);
\usort($colors, $this->compareColors(...));
$this->outputTable('Html by Value', $colors);

$this->addPage();
$colors = HtmlBootstrapColor::cases();
$this->outputTable('Bootstrap by Name', $colors);

$this->sortColors($colors);
$this->moveY(self::LINE_HEIGHT);
\usort($colors, $this->compareColors(...));
$this->outputTable('Bootstrap by Value', $colors, true);

$this->addPage();
$this->moveY(self::LINE_HEIGHT);
$colors = HtmlGrayedColor::cases();
$this->outputTable('Grayed by Name', $colors);

$this->sortColors($colors);
$this->moveY(self::LINE_HEIGHT);
\usort($colors, $this->compareColors(...));
$this->outputTable('Grayed by Value', $colors, true);

return true;
}

private function compareColors(PdfColorInterface $color1, PdfColorInterface $color2): int
{
return \hexdec($color1->getPhpOfficeColor()) <=> \hexdec($color2->getPhpOfficeColor());
}

/**
* @param array<HtmlColorName|HtmlBootstrapColor|HtmlGrayedColor> $colors
* @param array<ColorType> $colors
*/
private function outputTable(string $title, array $colors, bool $currentY = false): void
{
$this->addBookmark(text: $title, currentY: $currentY);
$table = PdfGroupTable::instance($this)
->addColumns(
PdfColumn::left('Name', 40),
PdfColumn::center('Hexadecimal', 35, true),
PdfColumn::center('RGB', 35, true),
PdfColumn::center('Color', 35, true),
)->outputHeaders()
PdfColumn::center('Hexadecimal', 30, true),
PdfColumn::center('Red', 18, true),
PdfColumn::center('Green', 18, true),
PdfColumn::center('Blue', 18, true),
PdfColumn::center('Color', 30, true),
)
->outputHeaders()
->setGroupKey($title);

$colorStyle = PdfStyle::getCellStyle();
$colorCell = new PdfCell(style: $colorStyle);
$valueStyle = PdfStyle::getCellStyle()
->setFontName(PdfFontName::COURIER);

foreach ($colors as $color) {
$rgb = $color->asRGB();
$colorStyle->setFillColor($color->getFillColor());
$table->addRow(
$color->name,
new PdfCell(text: $color->value, style: $valueStyle),
\implode(', ', $color->asRGB()),
$color->value,
(string) $rgb[0],
(string) $rgb[1],
(string) $rgb[2],
$colorCell,
);
}

$text = \sprintf('%d colors', FormatUtils::formatInt($colors));
$table->singleLine($text, PdfStyle::getHeaderStyle());
}

/**
* @param array<HtmlColorName|HtmlBootstrapColor|HtmlGrayedColor> $colors
*/
private function sortColors(array &$colors): void
{
\usort(
$colors,
fn (
HtmlColorName|HtmlBootstrapColor|HtmlGrayedColor $color1,
HtmlColorName|HtmlBootstrapColor|HtmlGrayedColor $color2
): int => \hexdec($color1->value) <=> \hexdec($color2->value)
);
}
}
2 changes: 1 addition & 1 deletion tests/Report/HtmlColorsReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HtmlColorsReportTest extends TestCase
/**
* @throws Exception
*/
public function testEmpty(): void
public function testReport(): void
{
$controller = $this->createMock(AbstractController::class);
$report = new HtmlColorsReport($controller);
Expand Down
10 changes: 5 additions & 5 deletions vendor-bin/psalm/composer.lock

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

0 comments on commit 97c5d47

Please sign in to comment.