Skip to content

Commit 2e46629

Browse files
committed
test(color): add unit tests for color mode conversions
1 parent 12b4879 commit 2e46629

3 files changed

Lines changed: 201 additions & 0 deletions

File tree

test/Renderer/Color/CmykTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 CmykTest extends TestCase
12+
{
13+
/**
14+
* Tests CMYK to RGB conversion, focusing on correct application of rounding.
15+
*/
16+
public function testToRgb() : void
17+
{
18+
// Pure Black (C:0, M:0, Y:0, K:100) -> RGB(0, 0, 0)
19+
$cmykBlack = new Cmyk(0, 0, 0, 100);
20+
$this->assertEquals(new Rgb(0, 0, 0), $cmykBlack->toRgb(), 'CMYK Black to RGB');
21+
22+
// Pure White (C:0, M:0, Y:0, K:0) -> RGB(255, 255, 255)
23+
$cmykWhite = new Cmyk(0, 0, 0, 0);
24+
$this->assertEquals(new Rgb(255, 255, 255), $cmykWhite->toRgb(), 'CMYK White to RGB');
25+
26+
// Mid Gray (C:0, M:0, Y:0, K:50) -> RGB(128, 128, 128)
27+
// Check for rounding: 255 * (1 - 0) * (1 - 0.5) = 127.5 -> round(127.5) = 128
28+
$cmykGray = new Cmyk(0, 0, 0, 50);
29+
$this->assertEquals(new Rgb(128, 128, 128), $cmykGray->toRgb(), 'CMYK Gray to RGB (rounding check)');
30+
31+
// Complex Color (Dark Red): C:10, M:80, Y:70, K:30
32+
// R: round(255 * 0.9 * 0.7) = round(160.65) = 161
33+
// G: round(255 * 0.2 * 0.7) = round(35.7) = 36
34+
// B: round(255 * 0.3 * 0.7) = round(53.55) = 54
35+
$cmykColor = new Cmyk(10, 80, 70, 30);
36+
$this->assertEquals(new Rgb(161, 36, 54), $cmykColor->toRgb(), 'CMYK Complex Color to RGB');
37+
}
38+
39+
public function testToCmyk() : void
40+
{
41+
$cmyk = new Cmyk(10, 20, 30, 40);
42+
$this->assertSame($cmyk, $cmyk->toCmyk(), 'toCmyk should return $this');
43+
}
44+
45+
/**
46+
* Tests CMYK to Gray conversion via RGB.
47+
*/
48+
public function testToGray() : void
49+
{
50+
// White (K:0) -> Gray(100)
51+
$cmykWhite = new Cmyk(0, 0, 0, 0);
52+
$this->assertEquals(new Gray(100), $cmykWhite->toGray(), 'CMYK White to Gray');
53+
54+
// Black (K:100) -> Gray(0)
55+
$cmykBlack = new Cmyk(0, 0, 0, 100);
56+
$this->assertEquals(new Gray(0), $cmykBlack->toGray(), 'CMYK Black to Gray');
57+
58+
// Pure Gray (K:50) -> Should result in Gray(50)
59+
$cmykGray = new Cmyk(0, 0, 0, 50);
60+
$this->assertEquals(new Gray(50), $cmykGray->toGray(), 'CMYK Gray to Gray');
61+
}
62+
}

test/Renderer/Color/GrayTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 GrayTest extends TestCase
12+
{
13+
/**
14+
* Tests Gray to RGB conversion, focusing on using 255/100 and correct rounding.
15+
*/
16+
public function testToRgb() : void
17+
{
18+
// Black (0) -> RGB(0, 0, 0)
19+
$grayBlack = new Gray(0);
20+
$this->assertEquals(new Rgb(0, 0, 0), $grayBlack->toRgb(), 'Gray Black to RGB');
21+
22+
// White (100) -> RGB(255, 255, 255)
23+
// 100 * 255 / 100 = 255
24+
$grayWhite = new Gray(100);
25+
$this->assertEquals(new Rgb(255, 255, 255), $grayWhite->toRgb(), 'Gray White to RGB');
26+
27+
// Midpoint (50) -> RGB(128, 128, 128)
28+
// Check for rounding: 50 * 255 / 100 = 127.5 -> round(127.5) = 128
29+
$grayMiddle = new Gray(50);
30+
$this->assertEquals(new Rgb(128, 128, 128), $grayMiddle->toRgb(), 'Gray 50 to RGB (rounding check)');
31+
32+
// Custom value checking rounding
33+
// 33 * 255 / 100 = 84.15 -> round(84.15) = 84
34+
$grayCustom = new Gray(33);
35+
$this->assertEquals(new Rgb(84, 84, 84), $grayCustom->toRgb(), 'Gray Custom to RGB');
36+
}
37+
38+
/**
39+
* Tests Gray to CMYK conversion (K=100-Gray).
40+
*/
41+
public function testToCmyk() : void
42+
{
43+
// Black (0) -> K:100
44+
$grayBlack = new Gray(0);
45+
$this->assertEquals(new Cmyk(0, 0, 0, 100), $grayBlack->toCmyk(), 'Gray Black to CMYK');
46+
47+
// White (100) -> K:0
48+
$grayWhite = new Gray(100);
49+
$this->assertEquals(new Cmyk(0, 0, 0, 0), $grayWhite->toCmyk(), 'Gray White to CMYK');
50+
51+
// Middle (50) -> K:50
52+
$grayMiddle = new Gray(50);
53+
$this->assertEquals(new Cmyk(0, 0, 0, 50), $grayMiddle->toCmyk(), 'Gray Middle to CMYK');
54+
}
55+
56+
public function testToGray() : void
57+
{
58+
$gray = new Gray(75);
59+
$this->assertSame($gray, $gray->toGray(), 'toGray should return $this');
60+
}
61+
}

test/Renderer/Color/RgbTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)