|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +namespace BaconQrCodeTest\Renderer\Color; |
| 5 | + |
| 6 | +use BaconQrCode\Renderer\Color\Cmyk; |
| 7 | +use BaconQrCode\Renderer\Color\Gray; |
| 8 | +use BaconQrCode\Renderer\Color\Rgb; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +final class RgbTest extends TestCase |
| 12 | +{ |
| 13 | + public function testToRgb() : void |
| 14 | + { |
| 15 | + $rgb = new Rgb(10, 20, 30); |
| 16 | + $this->assertSame($rgb, $rgb->toRgb(), 'toRgb should return $this'); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Tests RGB to CMYK conversion, focusing on: |
| 21 | + * 1. Handling the special case RGB(0, 0, 0) -> CMYK(0, 0, 0, 100). |
| 22 | + * 2. Correct application of rounding. |
| 23 | + */ |
| 24 | + public function testToCmyk() : void |
| 25 | + { |
| 26 | + // Special Case: Black (0, 0, 0) -> C:0, M:0, Y:0, K:100 |
| 27 | + $rgbBlack = new Rgb(0, 0, 0); |
| 28 | + $this->assertEquals(new Cmyk(0, 0, 0, 100), $rgbBlack->toCmyk(), 'RGB Black to CMYK (special case)'); |
| 29 | + |
| 30 | + // White (255, 255, 255) -> C:0, M:0, Y:0, K:0 |
| 31 | + $rgbWhite = new Rgb(255, 255, 255); |
| 32 | + $this->assertEquals(new Cmyk(0, 0, 0, 0), $rgbWhite->toCmyk(), 'RGB White to CMYK'); |
| 33 | + |
| 34 | + // Pure Red (255, 0, 0) -> C:0, M:100, Y:100, K:0 |
| 35 | + $rgbRed = new Rgb(255, 0, 0); |
| 36 | + $this->assertEquals(new Cmyk(0, 100, 100, 0), $rgbRed->toCmyk(), 'RGB Red to CMYK'); |
| 37 | + |
| 38 | + // Complex Color checking rounding: RGB(100, 150, 200) |
| 39 | + // K=22 (round(100 * 0.2156)) |
| 40 | + // C=50 (round(100 * 0.3922 / 0.7844)) |
| 41 | + // M=25 (round(100 * 0.1961 / 0.7844)) |
| 42 | + // Y=0 |
| 43 | + $rgbCustom = new Rgb(100, 150, 200); |
| 44 | + $this->assertEquals(new Cmyk(50, 25, 0, 22), $rgbCustom->toCmyk(), 'RGB Custom to CMYK (rounding check)'); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Tests RGB to Gray conversion, focusing on: |
| 49 | + * 1. Correct luminance coefficients (2126, 7152, 722). |
| 50 | + * 2. Integer-based calculation (division by 25500). |
| 51 | + * 3. Correct application of rounding. |
| 52 | + */ |
| 53 | + public function testToGray() : void |
| 54 | + { |
| 55 | + // Black (0, 0, 0) -> Gray(0) |
| 56 | + $rgbBlack = new Rgb(0, 0, 0); |
| 57 | + $this->assertEquals(new Gray(0), $rgbBlack->toGray(), 'RGB Black to Gray'); |
| 58 | + |
| 59 | + // White (255, 255, 255) -> Gray(100) |
| 60 | + $rgbWhite = new Rgb(255, 255, 255); |
| 61 | + $this->assertEquals(new Gray(100), $rgbWhite->toGray(), 'RGB White to Gray'); |
| 62 | + |
| 63 | + // Pure Red (255, 0, 0) |
| 64 | + // round((255 * 2126) / 25500) = round(21.26) = 21 |
| 65 | + $rgbRed = new Rgb(255, 0, 0); |
| 66 | + $this->assertEquals(new Gray(21), $rgbRed->toGray(), 'RGB Red to Gray'); |
| 67 | + |
| 68 | + // Pure Green (0, 255, 0) |
| 69 | + // round((255 * 7152) / 25500) = round(71.52) = 72 |
| 70 | + $rgbGreen = new Rgb(0, 255, 0); |
| 71 | + $this->assertEquals(new Gray(72), $rgbGreen->toGray(), 'RGB Green to Gray'); |
| 72 | + |
| 73 | + // Complex Color checking rounding: RGB(100, 150, 200) |
| 74 | + // round((100*2126 + 150*7152 + 200*722) / 25500) = round(56.07...) = 56 |
| 75 | + $rgbCustom = new Rgb(100, 150, 200); |
| 76 | + $this->assertEquals(new Gray(56), $rgbCustom->toGray(), 'RGB Custom to Gray (rounding check)'); |
| 77 | + } |
| 78 | +} |
0 commit comments