Skip to content

Commit

Permalink
Using 0-255 grayscale scale
Browse files Browse the repository at this point in the history
Instead 0-100, which was introduced (changed) in 2.2.0
  • Loading branch information
kazsaj committed Jan 8, 2019
1 parent 4503a52 commit 4c484e9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
8 changes: 7 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## [v2.2.2]

### Fixed
- Using 0-255 grayscale scale, instead 0-100, which was changed since 2.2.0

## [v2.2.1]

### Fixed
Expand Down Expand Up @@ -45,7 +50,8 @@ All notable changes to this project will be documented in this file.
### Fixes
- "U" style did not actually underline the text.

[Unreleased]: https://github.com/DocnetUK/tfpdf/compare/v2.2.1...HEAD
[Unreleased]: https://github.com/DocnetUK/tfpdf/compare/v2.2.2...HEAD
[v2.2.2]: https://github.com/DocnetUK/tfpdf/compare/v2.2.1...v2.2.2
[v2.2.1]: https://github.com/DocnetUK/tfpdf/compare/v2.2.0...v2.2.1
[v2.2.0]: https://github.com/DocnetUK/tfpdf/compare/v2.1.1...v2.2.0
[v2.1.1]: https://github.com/DocnetUK/tfpdf/compare/v2.1.0...v2.1.1
Expand Down
69 changes: 23 additions & 46 deletions src/tFPDF/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,30 +851,37 @@ public function PageNo()
}

/**
* @param int $int_red
* @param int|null $int_green
* @param int|null $int_blue
* @param array $args
* @return string
*/
public function SetDrawColor()
protected function getColourString($args)
{
// Set color for all stroking operations
$args = func_get_args();
if (count($args) and is_array($args[0])) {
if (count($args) && is_array($args[0])) {
$args = $args[0];
}
switch (count($args)) {
case 1:
$this->str_draw_color = sprintf('%.3f G', $args[0] / 100);
break;
return sprintf('%.3f g', $args[0] / 255);
case 3:
$this->str_draw_color = sprintf('%.3f %.3f %.3f RG', $args[0] / 255, $args[1] / 255, $args[2] / 255);
break;
return sprintf('%.3f %.3f %.3f rg', $args[0] / 255, $args[1] / 255, $args[2] / 255);
case 4:
$this->str_draw_color = sprintf('%.3f %.3f %.3f %.3f K', $args[0] / 100, $args[1] / 100, $args[2] / 100, $args[3] / 100);
break;
return sprintf('%.3f %.3f %.3f %.3f k', $args[0] / 100, $args[1] / 100, $args[2] / 100, $args[3] / 100);
default:
$this->str_draw_color = '0 G';
return '0 g';
}
}

/**
* @param int $int_red
* @param int|null $int_green
* @param int|null $int_blue
*/
public function SetDrawColor()
{
// Set color for all stroking operations
$args = func_get_args();
// Setting draw colour (unlike any other colours) requires it to be uppercase
$this->str_draw_color = strtoupper($this->getColourString($args));
if ($this->int_page > 0) {
$this->Out($this->str_draw_color);
}
Expand All @@ -889,22 +896,7 @@ public function SetFillColor()
{
// Set color for all filling operations
$args = func_get_args();
if (count($args) and is_array($args[0])) {
$args = $args[0];
}
switch (count($args)) {
case 1:
$this->str_fill_color = sprintf('%.3f g', $args[0] / 100);
break;
case 3:
$this->str_fill_color = sprintf('%.3f %.3f %.3f rg', $args[0] / 255, $args[1] / 255, $args[2] / 255);
break;
case 4:
$this->str_fill_color = sprintf('%.3f %.3f %.3f %.3f k', $args[0] / 100, $args[1] / 100, $args[2] / 100, $args[3] / 100);
break;
default:
$this->str_fill_color = '0 g';
}
$this->str_fill_color = $this->getColourString($args);
$this->bol_fill_text_differ = ($this->str_fill_color != $this->str_text_color);
if ($this->int_page > 0) {
$this->Out($this->str_fill_color);
Expand All @@ -920,22 +912,7 @@ public function SetTextColor()
{
// Set color for text
$args = func_get_args();
if (count($args) and is_array($args[0])) {
$args = $args[0];
}
switch (count($args)) {
case 1:
$this->str_text_color = sprintf('%.3f g', $args[0] / 100);
break;
case 3:
$this->str_text_color = sprintf('%.3f %.3f %.3f rg', $args[0] / 255, $args[1] / 255, $args[2] / 255);
break;
case 4:
$this->str_text_color = sprintf('%.3f %.3f %.3f %.3f k', $args[0] / 100, $args[1] / 100, $args[2] / 100, $args[3] / 100);
break;
default:
$this->str_text_color = '0 g';
}
$this->str_text_color = $this->getColourString($args);
$this->bol_fill_text_differ = ($this->str_fill_color != $this->str_text_color);
}

Expand Down
18 changes: 17 additions & 1 deletion tests/PDFGeneratedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testFileIsGenerated()
$pdfLibrary->SetTextColor(255, 0, 0);
$pdfLibrary->Write(5, "Hello Red Courier World");
$pdfLibrary->Ln(10);
$pdfLibrary->SetTextColor(50);
$pdfLibrary->SetTextColor(122.5);
$pdfLibrary->Write(5, "Hello Gray Courier World");
$pdfLibrary->SetFont('Courier', 'U', 14);
$pdfLibrary->Ln(10);
Expand All @@ -36,6 +36,22 @@ public function testFileIsGenerated()

$pdfLibrary->Ln(10);

// Set draw color example
$pdfLibrary->SetLineWidth(2);
$pdfLibrary->SetDrawColor(122.5);
$pdfLibrary->Line(20, $pdfLibrary->GetY(), 200, $pdfLibrary->GetY());
$pdfLibrary->Ln(10);

// Set fill color example
$pdfLibrary->SetFillColor(122.5);
$pdfLibrary->Rect(20, $pdfLibrary->GetY(), 180, 20, 'F');
$pdfLibrary->Ln(30);

// Set text color example
$pdfLibrary->SetFont('Courier', '', 14);
$pdfLibrary->SetTextColor(122.5);
$pdfLibrary->Text(20, $pdfLibrary->GetY(), 'Test text');

$file = $pdfLibrary->output();

if (empty($file)) {
Expand Down

0 comments on commit 4c484e9

Please sign in to comment.