Skip to content

Novis\Image\Image::withBrightness

$image->withBrightness(float $amount): Image$image->withBrightness(
    float $amount,
): Image

Makes an image lighter or darker.

withBrightness multiplies the red, green and blue value of every pixel by $amount. 1.0 changes nothing. 1.2 makes the image 20% brighter, and 0.5 makes it half as bright. A value can not go above 255, so very bright parts become white. Transparency does not change.

withBrightness 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

VersionChange
0.0.6Each 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

Brighter

<?nvs
use Novis\Image\Color;
// Shows how to make a dark photo 20% brighter.
use Novis\Image\Image;
Image $dark = Image::create(10, 10, Color::rgba(100, 50, 25));
// Each colour value is multiplied by 1.2. This prints 120 60 30.
var $raw = $dark->withBrightness(1.2)->raw();
array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($raw->pixels, 0, 4), 'C4');
echo $pixel[0], ' ', $pixel[1], ' ', $pixel[2], "\n";
Output
120 60 30

Darker

<?nvs
use Novis\Image\Color;
// Shows how to make a background image darker, so white text on it is easier to read.
use Novis\Image\Image;
Image $banner = Image::create(10, 10, Color::rgba(200, 180, 160));
// An amount below 1.0 makes the image darker. This prints 100 90 80.
var $raw = $banner->withBrightness(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";
Output
100 90 80

Values stop at 255

<?nvs
use Novis\Image\Color;
// Shows that a colour value does not go above 255.
use Novis\Image\Image;
Image $light = Image::create(10, 10, Color::rgba(200, 100, 0));
// 200 times 1.5 is 300, so the red value stops at 255.
// 0 times 1.5 is still 0. This prints 255 150 0.
var $raw = $light->withBrightness(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";
Output
255 150 0