Skip to content

Commit 9ce8213

Browse files
committed
Also support colors that have three channels in the toHavePixelWithColor method.
1 parent 3e2ad93 commit 9ce8213

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

tests/custom-matcher.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export const CustomMatchers = {
4949
toHavePixelWithColor: ((image: IMagickImage, x: number, y: number, colorOrString: IMagickColor | string) => {
5050
const actualColor = pixelColor(image, x, y);
5151
let expectedColor = colorOrString.toString();
52+
if (expectedColor.length === 4)
53+
{
54+
expectedColor = '#' + expectedColor[1] + expectedColor[1] + expectedColor[2] + expectedColor[2] + expectedColor[3] + expectedColor[3];
55+
}
5256
if (expectedColor.length === 7)
5357
expectedColor += 'ff';
5458

@@ -86,16 +90,22 @@ function pixelColor(image: IMagickImage, x: number, y: number): string {
8690

8791
switch (channelCount) {
8892
case 1:
89-
result += toHex(pixel[0]);
90-
result += toHex(Quantum.max);
91-
break;
92-
case 2:
93-
result += toHex(pixel[image._channelOffset(PixelChannel.Red)]);
94-
if (image.hasAlpha)
95-
result += toHex(pixel[image._channelOffset(PixelChannel.Alpha)]);
96-
else
93+
{
94+
const value = toHex(pixel[0]);
95+
result += value + value + value;
9796
result += toHex(Quantum.max);
98-
break;
97+
break;
98+
}
99+
case 2:
100+
{
101+
const value = toHex(pixel[image._channelOffset(PixelChannel.Red)]);
102+
result += value + value + value;
103+
if (image.hasAlpha)
104+
result += toHex(pixel[image._channelOffset(PixelChannel.Alpha)]);
105+
else
106+
result += toHex(Quantum.max);
107+
break;
108+
}
99109
case 3:
100110
result += toHex(pixel[image._channelOffset(PixelChannel.Red)]);
101111
result += toHex(pixel[image._channelOffset(PixelChannel.Green)]);

tests/magick-image/set-write-mask.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ describe('MagickImage#setWriteMask', () => {
1717
mask = expectToNotBeNull(mask);
1818
expect(mask.width).toBe(640);
1919
expect(mask.height).toBe(480);
20-
expect(mask).toHavePixelWithColor(9, 14, '#ffff');
21-
expect(mask).toHavePixelWithColor(10, 15, '#00ff');
20+
expect(mask).toHavePixelWithColor(9, 14, '#fff');
21+
expect(mask).toHavePixelWithColor(10, 15, '#000');
2222
});
2323
});
2424
});

0 commit comments

Comments
 (0)