-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorParser.php
More file actions
30 lines (23 loc) · 826 Bytes
/
ColorParser.php
File metadata and controls
30 lines (23 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
// SPDX-FileCopyrightText: 2026 LibreSign
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreSign\XObjectTemplate\Pdf;
final class ColorParser
{
public function toPdfRgb(string $hexColor): string
{
$hex = ltrim(trim($hexColor), '#');
if (strlen($hex) === 3) {
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
}
if (!preg_match('/^[0-9a-f]{6}$/i', $hex)) {
return '0 0 0 rg';
}
$channels = str_split($hex, 2);
$redChannel = round(hexdec($channels[0]) / 255, 4);
$greenChannel = round(hexdec($channels[1]) / 255, 4);
$blueChannel = round(hexdec($channels[2]) / 255, 4);
return sprintf('%s %s %s rg', $redChannel, $greenChannel, $blueChannel);
}
}