Skip to content

Commit 7e85330

Browse files
committed
* 'trunk' of https://github.com/WordPress/performance: Add test case for when RGB values are too high Ensure RGB variable is int fix: avoid needless array allocation in rgb to hex conversion
2 parents c58f0c7 + b63b25a commit 7e85330

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

plugins/dominant-color-images/class-dominant-color-image-editor-gd.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public function get_dominant_color() {
3535
imagecopyresampled( $shorted_image, $this->image, 0, 0, 0, 0, 1, 1, imagesx( $this->image ), imagesy( $this->image ) );
3636

3737
$rgb = imagecolorat( $shorted_image, 0, 0 );
38+
if ( false === $rgb ) {
39+
return new WP_Error( 'image_editor_dominant_color_error', __( 'Dominant color detection failed.', 'dominant-color-images' ) );
40+
}
3841
$r = ( $rgb >> 16 ) & 0xFF;
3942
$g = ( $rgb >> 8 ) & 0xFF;
4043
$b = $rgb & 0xFF;

plugins/dominant-color-images/helper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,11 @@ function dominant_color_has_transparency( int $attachment_id ): ?bool {
183183
* @return string|null Hex color or null if error.
184184
*/
185185
function dominant_color_rgb_to_hex( int $red, int $green, int $blue ): ?string {
186-
$range = range( 0, 255 );
187-
if ( ! in_array( $red, $range, true ) || ! in_array( $green, $range, true ) || ! in_array( $blue, $range, true ) ) {
186+
if ( ! (
187+
$red >= 0 && $red <= 255
188+
&& $green >= 0 && $green <= 255
189+
&& $blue >= 0 && $blue <= 255
190+
) ) {
188191
return null;
189192
}
190193

tests/plugins/dominant-color-images/dominant-color-test.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,48 +350,54 @@ public function test_dominant_color_rgb_to_hex( int $red, int $green, int $blue,
350350
/** @return array<string, mixed> */
351351
public function provider_get_hex_color(): array {
352352
return array(
353-
'black' => array(
353+
'black' => array(
354354
'red' => 0,
355355
'green' => 0,
356356
'blue' => 0,
357357
'hex' => '000000',
358358
),
359-
'white' => array(
359+
'white' => array(
360360
'red' => 255,
361361
'green' => 255,
362362
'blue' => 255,
363363
'hex' => 'ffffff',
364364
),
365-
'blue' => array(
365+
'blue' => array(
366366
'red' => 255,
367367
'green' => 0,
368368
'blue' => 0,
369369
'hex' => 'ff0000',
370370
),
371-
'teal' => array(
371+
'teal' => array(
372372
'red' => 255,
373373
'green' => 255,
374374
'blue' => 0,
375375
'hex' => 'ffff00',
376376
),
377-
'pink' => array(
377+
'pink' => array(
378378
'red' => 255,
379379
'green' => 0,
380380
'blue' => 255,
381381
'hex' => 'ff00ff',
382382
),
383-
'purple' => array(
383+
'purple' => array(
384384
'red' => 88,
385385
'green' => 42,
386386
'blue' => 158,
387387
'hex' => '582a9e',
388388
),
389-
'invalid' => array(
389+
'invalid1' => array(
390390
'red' => -1,
391391
'green' => -1,
392392
'blue' => -1,
393393
'hex' => null,
394394
),
395+
'invalid2' => array(
396+
'red' => 256,
397+
'green' => 256,
398+
'blue' => 256,
399+
'hex' => null,
400+
),
395401
);
396402
}
397403

0 commit comments

Comments
 (0)