Skip to content

Novis\Image\Image::tinted

$image->tinted(Color $color): Image$image->tinted(
    Color $color,
): Image

Colours an image with one colour.

tinted multiplies the red, green and blue value of every pixel by the values of $color. White becomes the tint colour, and black stays black. The alpha of $color is how strong the tint is: 1.0 is the full tint, 0.5 is half, and 0.0 changes nothing. The transparency of the image does not change.

tinted returns a new Image and does not change the one you called it on. The work happens when encode, encodeVariants or raw runs.

Good to know: call grayscaled first when the result should have only the tint colour.

Parameters

  • $colorColor

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

Old photo

<?nvs
use Novis\Image\Color;
// Shows how to give a photo the brown colour of an old print.
use Novis\Image\Image;
Image $photo = Image::create(10, 10, Color::hex('#ffffff'));
// White becomes the tint colour. This prints 255 204 153.
var $raw = $photo->grayscaled()->tinted(Color::hex('#ffcc99'))->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 204 153

Half strength

<?nvs
use Novis\Image\Color;
// Shows how the alpha of the tint colour sets how strong the tint is.
use Novis\Image\Image;
Image $photo = Image::create(10, 10, Color::hex('#ffffff'));
// A blue tint with an alpha of 0.5 colours the image half as much. This prints 127 127 255.
var $raw = $photo->tinted(Color::rgba(0, 0, 255, 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
127 127 255

Black stays black

<?nvs
use Novis\Image\Color;
// Shows that `tinted` does not change black, and that an alpha of 0 changes nothing.
use Novis\Image\Image;
// This prints 0 0 0, because black times any colour is black.
var $black = Image::create(4, 4, Color::hex('#000000'))->tinted(Color::hex('#ff0000'))->raw();
array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($black->pixels, 0, 4), 'C4');
echo $pixel[0], ' ', $pixel[1], ' ', $pixel[2], "\n";
// This prints 200 200 200, because the tint has an alpha of 0.
var $gray = Image::create(4, 4, Color::rgba(200, 200, 200))->tinted(Color::rgba(255, 0, 0, 0.0))->raw();
$pixel = Core\Bytes::unpack(Core\Bytes::slice($gray->pixels, 0, 4), 'C4');
echo $pixel[0], ' ', $pixel[1], ' ', $pixel[2], "\n";
Output
0 0 0
200 200 200