Skip to content

Commit c057279

Browse files
committed
Attempts a more portable rgba extraction
1 parent a55b900 commit c057279

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

src/mako/pixel/image/inspectors/imagemagick/ColorAt.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ public function inspect(object &$imageResource): mixed
4747

4848
$pixel = $imageResource->getImagePixelColor($this->pixel->x, $this->pixel->y);
4949

50-
$rgba = $pixel->getColor(2); // 2 = RGBA normalized to 0-255
50+
$color = $pixel->getColor(1);
5151

52-
return new Color($rgba['r'], $rgba['g'], $rgba['b'], $rgba['a']);
52+
$r = (int) round($color['r'] * 255);
53+
$g = (int) round($color['g'] * 255);
54+
$b = (int) round($color['b'] * 255);
55+
$a = (int) round($color['a'] * 255);
56+
57+
return new Color($r, $g, $b, $a);
5358
}
5459
}

src/mako/pixel/image/inspectors/imagemagick/TopColors.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,18 @@ public function inspect(object &$imageResource): mixed
5353
break;
5454
}
5555

56-
$rgba = $pixel->getColor(2); // 2 = RGBA normalized to 0-255
56+
$color = $pixel->getColor(1);
5757

58-
if ($hasAlphaChannel && $this->ignoreTransparent && $rgba['a'] === 0) {
58+
$r = (int) round($color['r'] * 255);
59+
$g = (int) round($color['g'] * 255);
60+
$b = (int) round($color['b'] * 255);
61+
$a = (int) round($color['a'] * 255);
62+
63+
if ($hasAlphaChannel && $this->ignoreTransparent && $a === 0) {
5964
continue;
6065
}
6166

62-
$colors[] = new Color($rgba['r'], $rgba['g'], $rgba['b'], $rgba['a']);
67+
$colors[] = new Color($r, $g, $b, $a);
6368
}
6469

6570
return $colors;

tests/unit/pixel/image/operations/imagemagick/ReplaceColorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ public function testReplaceColors(): void
3636

3737
$image->apply(new ReplaceColor(
3838
Color::fromHex('#0376BB'),
39-
new Color(0, 0, 0)
39+
new Color(0, 0, 0, 127)
4040
));
4141

4242
$colors = $image->inspect(new TopColors);
4343

4444
$this->assertCount(3, $colors);
4545

4646
$this->assertSame('#000000', $colors[0]->toHexString());
47+
$this->assertSame('#0000007F', $colors[0]->toHexaString());
4748
$this->assertSame('#B51700', $colors[1]->toHexString());
4849
$this->assertSame('#047101', $colors[2]->toHexString());
4950
}

0 commit comments

Comments
 (0)