Novis\Image\Image::withContrast
Makes the difference between light and dark parts larger or smaller.
withContrast moves the red, green and blue value of every pixel away from middle gray (128) by $amount. 1.0 changes nothing. A value above 1.0 makes dark parts darker and light parts lighter. A value below 1.0 moves every colour closer to gray. A value stays between 0 and 255. Transparency does not change.
withContrast returns a new Image and does not change the one you called it on. The work happens when encode, encodeVariants or raw runs.
Parameters
$amountfloat
Return value
Returns Image.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each Novis\Image\Image method that returns a new image is named for it, for example resized(), blurred() and withText(). keepingMetadata() replaces metadata({ keep: true }), and encodeVariants() replaces variants(). Image::compare returns a Comparison with the property differenceImage, which was Diff with diff. |
Examples
More contrast
<?nvsuse Novis\Image\Color;// Shows how more contrast makes a dark colour darker and a light colour lighter.use Novis\Image\Image;
// 100 is 28 below middle gray, and 200 is 72 above it.Image $flat = Image::create(10, 10, Color::rgba(100, 200, 128));
// With 1.5, each distance from 128 grows by half. This prints 86 236 128.var $raw = $flat->withContrast(1.5)->raw();array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($raw->pixels, 0, 4), 'C4');echo $pixel[0], ' ', $pixel[1], ' ', $pixel[2], "\n";86 236 128Less contrast
<?nvsuse Novis\Image\Color;// Shows how less contrast moves every colour closer to gray, for a faded background.use Novis\Image\Image;
Image $photo = Image::create(10, 10, Color::rgba(0, 255, 128));
// With 0.5, each distance from 128 is cut in half. This prints 64 192 128.var $raw = $photo->withContrast(0.5)->raw();array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($raw->pixels, 0, 4), 'C4');echo $pixel[0], ' ', $pixel[1], ' ', $pixel[2], "\n";64 192 128With brightness
<?nvsuse Novis\Image\Color;// Shows how to combine `withContrast` and `withBrightness`. The steps run in the order you write them.use Novis\Image\Image;
Image $photo = Image::create(10, 10, Color::rgba(100, 100, 100));
// Contrast and then brightness gives 108.// Brightness and then contrast gives 172.array<Image> $orders = [$photo->withContrast(2.0)->withBrightness(1.5), $photo->withBrightness(1.5)->withContrast(2.0)];foreach ($orders as Image $edited) { array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($edited->raw()->pixels, 0, 4), 'C4'); echo $pixel[0], "\n";}108172