File: GrayTest.php

package info (click to toggle)
baconqrcode 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 744 kB
  • sloc: php: 7,075; xml: 63; makefile: 8
file content (63 lines) | stat: -rw-r--r-- 2,221 bytes parent folder | download
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
<?php
declare(strict_types = 1);

namespace BaconQrCodeTest\Renderer\Color;

use BaconQrCode\Renderer\Color\Cmyk;
use BaconQrCode\Renderer\Color\Gray;
use BaconQrCode\Renderer\Color\Rgb;
use PHPUnit\Framework\TestCase;

final class GrayTest extends TestCase
{
    /**
     * Tests Gray to RGB conversion, focusing on:
     * 1. Using 255/100 instead of 2.55 to avoid floating-point precision loss.
     * 2. Correct application of rounding.
     */
    public function testToRgb() : void
    {
        // Black (0) -> RGB(0, 0, 0)
        $grayBlack = new Gray(0);
        $this->assertEquals(new Rgb(0, 0, 0), $grayBlack->toRgb(), 'Gray Black to RGB');

        // White (100) -> RGB(255, 255, 255)
        // 100 * 255 / 100 = 255
        $grayWhite = new Gray(100);
        $this->assertEquals(new Rgb(255, 255, 255), $grayWhite->toRgb(), 'Gray White to RGB');

        // Midpoint (50) -> RGB(128, 128, 128)
        // Check for precision and rounding: 50 * 255 / 100 = 127.5 -> round(127.5) = 128
        $grayMiddle = new Gray(50);
        $this->assertEquals(new Rgb(128, 128, 128), $grayMiddle->toRgb(), 'Gray 50 to RGB (rounding check)');

        // Custom value (90) -> RGB(230, 230, 230)
        // Check for precision and rounding: 90 * 255 / 100 = 229.5 -> round(229.5) = 230
        $grayCustom = new Gray(90);
        $this->assertEquals(new Rgb(230, 230, 230), $grayCustom->toRgb(), 'Gray Custom to RGB (rounding check)');
    }

    /**
     * Tests Gray to CMYK conversion (K=100-Gray).
     */
    public function testToCmyk() : void
    {
        // Black (0) -> K:100
        $grayBlack = new Gray(0);
        $this->assertEquals(new Cmyk(0, 0, 0, 100), $grayBlack->toCmyk(), 'Gray Black to CMYK');

        // White (100) -> K:0
        $grayWhite = new Gray(100);
        $this->assertEquals(new Cmyk(0, 0, 0, 0), $grayWhite->toCmyk(), 'Gray White to CMYK');

        // Middle (50) -> K:50
        $grayMiddle = new Gray(50);
        $this->assertEquals(new Cmyk(0, 0, 0, 50), $grayMiddle->toCmyk(), 'Gray Middle to CMYK');
    }

    public function testToGray() : void
    {
        $gray = new Gray(75);
        $this->assertSame($gray, $gray->toGray(), 'toGray should return $this');
    }
}