File tree Expand file tree Collapse file tree 3 files changed +36
-0
lines changed
Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -46,6 +46,14 @@ public function nativeToColor(mixed $native): ColorInterface
4646 throw new ColorException ('Vips driver can only decode colors in array format. ' );
4747 }
4848
49+ // Handle grayscale images (single-band) when creating RgbColor
50+ // VIPS returns single value for grayscale images, but RgbColor constructor needs 3 values
51+ // Only grayscale images have single-band data; HSV/HSL would have 3 bands
52+ if ($ this ->colorspace ::class !== CmykColorspace::class && count ($ native ) === 1 ) {
53+ // Expand grayscale luminance to RGB triplet
54+ $ native = [$ native [0 ], $ native [0 ], $ native [0 ]];
55+ }
56+
4957 return match ($ this ->colorspace ::class) {
5058 CmykColorspace::class => new CmykColor (...$ native ),
5159 default => new RgbColor (...$ native ),
Original file line number Diff line number Diff line change @@ -42,4 +42,20 @@ public function testAnalyzeFloatFormat(): void
4242 $ this ->assertEquals ('1eccff ' , $ result ->toHex (), 'Failed for format: ' . $ format );
4343 }
4444 }
45+
46+ public function testAnalyzeGrayscale (): void
47+ {
48+ $ image = $ this ->readTestImage ('grayscale.png ' );
49+ $ analyzer = new PixelColorAnalyzer (0 , 0 );
50+ $ analyzer ->setDriver (new Driver ());
51+ $ result = $ analyzer ->analyze ($ image );
52+ $ this ->assertInstanceOf (ColorInterface::class, $ result );
53+ // Grayscale images should return a color where R=G=B
54+ $ hex = $ result ->toHex ();
55+ $ r = substr ($ hex , 0 , 2 );
56+ $ g = substr ($ hex , 2 , 2 );
57+ $ b = substr ($ hex , 4 , 2 );
58+ $ this ->assertEquals ($ r , $ g , 'Red and green channels should be equal for grayscale ' );
59+ $ this ->assertEquals ($ g , $ b , 'Green and blue channels should be equal for grayscale ' );
60+ }
4561}
Original file line number Diff line number Diff line change @@ -40,6 +40,18 @@ public function testNativeToColor(): void
4040 $ this ->assertEquals (255 , $ color ->channel (Alpha::class)->value ());
4141 }
4242
43+ public function testNativeToColorGrayscale (): void
44+ {
45+ // Test grayscale image handling - single value should be expanded to RGB
46+ $ processor = new ColorProcessor (new Colorspace ());
47+ $ color = $ processor ->nativeToColor ([128 ]); // Single grayscale value
48+ $ this ->assertInstanceOf (Color::class, $ color );
49+ $ this ->assertEquals (128 , $ color ->channel (Red::class)->value ());
50+ $ this ->assertEquals (128 , $ color ->channel (Green::class)->value ());
51+ $ this ->assertEquals (128 , $ color ->channel (Blue::class)->value ());
52+ $ this ->assertEquals (255 , $ color ->channel (Alpha::class)->value ()); // Default alpha
53+ }
54+
4355 #[DataProvider('interpretationToColorspaceProvider ' )]
4456 public function testInterpretationToColorspace (string $ interpretation , string $ colorspaceClassname ): void
4557 {
You can’t perform that action at this time.
0 commit comments