Skip to content

Novis\Image\Image::Blend

How withOverlay mixes the colours of the image on top with the colours of the image below it.

Image::Blend is declared in the class Novis\Image\Image.

An Image::Blend is how a pixel on top mixes with the pixel under it:

  • Normal puts the top pixel over the lower one. Transparent parts of the top image show the lower image.
  • Multiply makes the result darker. White on top changes nothing.
  • Screen makes the result lighter. Black on top changes nothing.
  • Overlay makes dark parts darker and light parts lighter, so the contrast is higher.
  • Darken keeps the darker of the two values for each of red, green and blue.
  • Lighten keeps the lighter of the two values for each of red, green and blue.

You pass an Image::Blend to withOverlay as blend. The default is Normal. You can also store an Image::Blend and compare it, as the example shows.

Examples

Choose a mode from a setting

<?nvs
// Shows how to choose an `Image::Blend` from the text of a setting.
use Novis\Image\Image;
class Mode
{
public static function from(string $text): Image::Blend
{
return match ($text) {
'multiply' => Image::Blend::Multiply,
'screen' => Image::Blend::Screen,
'overlay' => Image::Blend::Overlay,
'darken' => Image::Blend::Darken,
'lighten' => Image::Blend::Lighten,
default => Image::Blend::Normal,
};
}
public static function darker(Image::Blend $blend): bool
{
return $blend == Image::Blend::Multiply || $blend == Image::Blend::Darken;
}
}
// "multiply" makes the result darker, so the first line prints "darker".
// "unknown" is not a mode and gives `Normal`, so the second line prints "not darker".
array<string> $settings = ['multiply', 'unknown'];
foreach ($settings as string $setting) {
echo Mode::darker(Mode::from($setting)) ? 'darker' : 'not darker', "\n";
}
Output
darker
not darker