-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfpdf_namedcolors.php
123 lines (100 loc) · 3.91 KB
/
fpdf_namedcolors.php
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
require_once('fpdf.php');
/*
Author: Philip van Heemstra - https://github.com/vHeemstra
License: Same as FPDF - Free for all! (Would appreciate the mention however)
The extension to the free FPDF class (www.fpdf.org) adds:
1) Named color support
2) Hex color support
NAME COLORS
-----------
To use named colors, first define them by calling the NameColor method providing a name and a color definition, like so:
$pdf->NameColor('name_of_color', [255,0,0]);
- The name will be used as an array key, so some restrictions apply. Also don't start it with a '#', this is reserved for hex values.
- The color definition can be one of these:
Single integer --> Grayscale color [0-255]
Array of 3 integers --> RGB color values [0-255]
Hex string --> Hex color value '#' + (6, 3 or 1 character(s)) [0-F]
USE NAMED COLORS AND BROADER DEFINITION SUPPORT
-----------------------------------------------
After defining the color, you can use the name as $r in the original Set[..]Color methods.
Additionally, the above described ways of defining colors can also be used as the $r value in the Set[..]Color methods, like so:
$pdf->SetDrawColor('name_of_color');
$pdf->SetDrawColor(255);
$pdf->SetDrawColor(255, 255, 255);
$pdf->SetDrawColor([255, 255, 255]);
$pdf->SetDrawColor('#f');
$pdf->SetDrawColor('#fff');
$pdf->SetDrawColor('#ffffff');
*/
class FPDF_NamedColors extends FPDF
{
/**
* Array to store the defined colors
*/
protected $namedColors = [];
/**
* Translate named colors, hex values, rgb strings and arrays to RGB array
*
* @param mixed $color Integer --> Value for red (or greyscale) channel [0-255]
* Hex string --> Hex color value of length 6, 3 or 1
* String --> Assumed defined color name
* @param [int] $g Value for green channel [0-255]
* @param [int] $b Value for blue channel [0-255]
*
* @return array [$red, $green, $blue] Returns corrected array with RGB values [0-255].
*/
function CorrectColor($color=0, $g=null, $b=null)
{
$rgb = [0, 0, 0];
if (is_integer($color) && is_integer($g) && is_integer($b)) {
$rgb = [$color, $g, $b];
} else if (is_integer($color)) {
$rgb = [$color, $color, $color];
} else if (is_array($color) && count($color)==3) {
$rgb = [$color[0], $color[1], $color[2]];
} else if (is_string($color) && 0 === strpos($color, '#')) {
$color = substr($color, 1);
if (6==count($color)) {
$rgb = [hexdec(substr($color,0,2)), hexdec(substr($color,2,2)), hexdec(substr($color,4,2))];
} else if (3==count($color)) {
$rgb = [hexdec(substr($color,0,1).substr($color,0,1)), hexdec(substr($color,1,1).substr($color,1,1)), hexdec(substr($color,2,1).substr($color,2,1))];
} else if (1==count($color)) {
$rgb = [hexdec(substr($color,0,1).substr($color,0,1)), hexdec(substr($color,0,1).substr($color,0,1)), hexdec(substr($color,0,1).substr($color,0,1))];
}
} else if (is_string($color) && 0 !== strpos($color, '#') && isset($this->namedColors[ $color ])) {
$rgb = $this->namedColors[ $color ];
}
return [
max(0, min(255, $rgb[0])),
max(0, min(255, $rgb[1])),
max(0, min(255, $rgb[2])),
];
}
/**
* Define/name a color
*
* @param string $name Name of the color, NOT starting with '#', used as array key
* @param mixed $color The color in either a single integer (grayscale), hex value or array of 3 integers (rgb).
*/
function NameColor($name, $color=0)
{
$this->namedColors[ $name ] = $this->CorrectColor($color);
}
function SetDrawColor($r, $g=null, $b=null)
{
$rgb = $this->CorrectColor($r, $g, $b);
parent::SetDrawColor($rgb[0], $rgb[1], $rgb[2]);
}
function SetFillColor($r, $g=null, $b=null)
{
$rgb = $this->CorrectColor($r, $g, $b);
parent::SetFillColor($rgb[0], $rgb[1], $rgb[2]);
}
function SetTextColor($r, $g=null, $b=null)
{
$rgb = $this->CorrectColor($r, $g, $b);
parent::SetTextColor($rgb[0], $rgb[1], $rgb[2]);
}
}
?>