Skip to content

πŸŽ₯ Make videos programmatically with React

@remotion/effects: Add contrast() effect

Jonny Burger2cf41ef5575f
diff --git a/packages/docs/docs/effects/contrast.mdx b/packages/docs/docs/effects/contrast.mdx
new file mode 100644
index 0000000000..7302884aa5
--- /dev/null
+++ b/packages/docs/docs/effects/contrast.mdx
@@ -0,0 +1,63 @@
+---
+slug: /effects/contrast
+title: contrast()
+sidebar_label: contrast()
+crumb: '@remotion/effects'
+---
+
+# contrast()<AvailableFrom v="4.0.467" />
+
+_Part of the [`@remotion/effects`](/docs/effects/api) package._
+
+A contrast adjustment effect that can be applied to canvas-based components such as [`<Video>`](/docs/media/video), [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) and [`<Solid>`](/docs/solid).
+
+## Example
+
+```tsx twoslash title="MyComp.tsx"
+import {AbsoluteFill} from 'remotion';
+import {Video} from '@remotion/media';
+import {contrast} from '@remotion/effects/contrast';
+
+export const MyComp: React.FC = () => {
+ return (
+ <AbsoluteFill>
+ <Video src="https://remotion.media/video.mp4" effects={[contrast({amount: 1.25})]} />
+ </AbsoluteFill>
+ );
+};
+```
+
+## API
+
+Pass an object with the following properties. You can also call `contrast()` without arguments.
+
+### `amount?`
+
+Contrast multiplier. Must be `>= 0`.
+
+- `1` leaves the layer unchanged
+- `0` produces a flat mid-gray image
+- values above `1` increase contrast
+- values between `0` and `1` reduce contrast
+
+Defaults to `1`.
+
+### `disabled?`
+
+When `true`, the effect is skipped. Defaults to `false`.
+
+## Effect order
+
+If you stack color-correction effects, a common order is:
+
+`brightness()` β†’ `contrast()` β†’ `saturation()` β†’ `hue()`
+
+`brightness()` and `contrast()` are not commutative – swapping their order changes the result, because contrast scales around the mid-gray point (`128`) of the current layer.
+
+## See also
+
+- [`@remotion/effects`](/docs/effects/api)
+- [`brightness()`](/docs/effects/brightness)
+- [`saturation()`](/docs/effects/saturation)
+- [`hue()`](/docs/effects/hue)
+- [Source code for this effect](https://github.com/remotion-dev/remotion/blob/main/packages/effects/src/contrast.ts)
diff --git a/packages/docs/docs/effects/table-of-contents.tsx b/packages/docs/docs/effects/table-of-contents.tsx
index f5b163e191..5ef3c3b672 100644
--- a/packages/docs/docs/effects/table-of-contents.tsx
+++ b/packages/docs/docs/effects/table-of-contents.tsx
@@ -24,6 +24,13 @@ const categories: {
name: 'brightness()',
description: 'Brightness adjustment effect',
},
+ {
+ link: '/docs/effects/contrast',
+ preview: '/img/effects-contrast-preview.jpg',
+ alt: 'contrast effect preview',
+ name: 'contrast()',
+ description: 'Contrast adjustment effect',
+ },
{
link: '/docs/effects/grayscale',
preview: '/img/effects-grayscale-preview.jpg',
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index d2b8c77df0..176de5adc2 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -271,6 +271,7 @@ const sidebars: SidebarsConfig = {
'effects/barrel-distortion',
'effects/blur',
'effects/brightness',
+ 'effects/contrast',
'effects/grayscale',
'effects/halftone',
'effects/hue',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index d09850544a..4a4c38f915 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -1745,6 +1745,15 @@ export const articles = [
noAi: false,
slug: 'effects/brightness',
},
+ {
+ id: 'effects/contrast',
+ title: 'contrast()',
+ relativePath: 'docs/effects/contrast.mdx',
+ compId: 'articles-docs-effects-contrast',
+ crumb: '@remotion/effects',
+ noAi: false,
+ slug: 'effects/contrast',
+ },
{
id: 'effects/grayscale',
title: 'grayscale()',
diff --git a/packages/effects/bundle.ts b/packages/effects/bundle.ts
index e07270d26e..7a20109307 100644
--- a/packages/effects/bundle.ts
+++ b/packages/effects/bundle.ts
@@ -10,6 +10,7 @@ const effectEntrypoints = [
'src/barrel-distortion.ts',
'src/blur.ts',
'src/brightness.ts',
+ 'src/contrast.ts',
'src/grayscale.ts',
'src/halftone.ts',
'src/hue.ts',
diff --git a/packages/effects/package.json b/packages/effects/package.json
index daacc33db8..8cb6a44704 100644
--- a/packages/effects/package.json
+++ b/packages/effects/package.json
@@ -47,6 +47,11 @@
"module": "./dist/esm/brightness.mjs",
"import": "./dist/esm/brightness.mjs"
},
+ "./contrast": {
+ "types": "./dist/contrast.d.ts",
+ "module": "./dist/esm/contrast.mjs",
+ "import": "./dist/esm/contrast.mjs"
+ },
"./halftone": {
"types": "./dist/halftone.d.ts",
"module": "./dist/esm/halftone.mjs",
@@ -105,6 +110,9 @@
"brightness": [
"dist/brightness.d.ts"
],
+ "contrast": [
+ "dist/contrast.d.ts"
+ ],
"halftone": [
"dist/halftone.d.ts"
],
diff --git a/packages/effects/src/contrast.ts b/packages/effects/src/contrast.ts
new file mode 100644
index 0000000000..b5ac2df44f
--- /dev/null
+++ b/packages/effects/src/contrast.ts
@@ -0,0 +1,81 @@
+import type {SequenceSchema} from 'remotion';
+import {Internals} from 'remotion';
+import {
+ assertOptionalFiniteNumber,
+ clampColorChannel,
+ colorMultiplierSchema,
+ DEFAULT_AMOUNT,
+ validateNonNegative,
+} from './color-utils.js';
+import {assertEffectParamsObject} from './validate-effect-param.js';
+
+const {createEffect} = Internals;
+
+const contrastSchema = {
+ amount: colorMultiplierSchema,
+} as const satisfies SequenceSchema;
+
+export type ContrastParams = {
+ /** Contrast multiplier. `1` leaves the image unchanged, `0` produces a flat gray, values above `1` increase contrast. Defaults to `1`. */
+ readonly amount?: number;
+};
+
+type ContrastResolved = {
+ amount: number;
+};
+
+const resolve = (p: ContrastParams): ContrastResolved => ({
+ amount: p.amount ?? DEFAULT_AMOUNT,
+});
+
+const validateContrastParams = (params: ContrastParams): void => {
+ assertEffectParamsObject(params, 'Contrast');
+ assertOptionalFiniteNumber(params.amount, 'amount');
+
+ const {amount} = resolve(params);
+ validateNonNegative(amount, 'amount');
+};
+
+export const contrast = createEffect<ContrastParams, null>({
+ type: 'remotion/contrast',
+ label: 'Contrast',
+ documentationLink: 'https://www.remotion.dev/docs/effects/contrast',
+ backend: '2d',
+ calculateKey: (params) => {
+ const r = resolve(params);
+ return `contrast-${r.amount}`;
+ },
+ setup: () => null,
+ apply: ({source, target, width, height, params}) => {
+ const ctx = target.getContext('2d');
+ if (!ctx) {
+ throw new Error(
+ 'Failed to acquire 2D context for contrast effect. The canvas may have been assigned a different context type.',
+ );
+ }
+
+ const r = resolve(params);
+
+ ctx.clearRect(0, 0, width, height);
+ ctx.drawImage(source, 0, 0, width, height);
+
+ if (r.amount === 1) {
+ return;
+ }
+
+ const imageData = ctx.getImageData(0, 0, width, height);
+ const {data} = imageData;
+ const factor = r.amount;
+
+ for (let i = 0; i < data.length; i += 4) {
+ data[i] = clampColorChannel((data[i] - 128) * factor + 128);
+ data[i + 1] = clampColorChannel((data[i + 1] - 128) * factor + 128);
+ data[i + 2] = clampColorChannel((data[i + 2] - 128) * factor + 128);
+ }
+
+ ctx.putImageData(imageData, 0, 0);
+ },
+ cleanup: () => undefined,
+ schema: contrastSchema,
+ validateParams: validateContrastParams,
+});
diff --git a/packages/effects/src/test/effect-params.test.ts b/packages/effects/src/test/effect-params.test.ts
index 8693512630..382d973c7a 100644
--- a/packages/effects/src/test/effect-params.test.ts
+++ b/packages/effects/src/test/effect-params.test.ts
@@ -2,6 +2,7 @@ import {expect, test} from 'bun:test';
import {barrelDistortion} from '../barrel-distortion/index.js';
import {blur} from '../blur/index.js';
import {brightness} from '../brightness.js';
+import {contrast} from '../contrast.js';
import {grayscale} from '../grayscale.js';
import {halftone} from '../halftone.js';
import {hue} from '../hue.js';
@@ -22,6 +23,9 @@ test('@remotion/effects expose documentation links', () => {
expect(brightness().definition.documentationLink).toBe(
'https://www.remotion.dev/docs/effects/brightness',
);
+ expect(contrast().definition.documentationLink).toBe(
+ 'https://www.remotion.dev/docs/effects/contrast',
+ );
expect(grayscale().definition.documentationLink).toBe(
'https://www.remotion.dev/docs/effects/grayscale',
);
@@ -225,6 +229,33 @@ test('brightness() amount produces distinct effect keys', () => {
).toBe(3);
});
+test('contrast() accepts default params', () => {
+ expect(() => contrast()).not.toThrow();
+});
+
+test('contrast() accepts increased contrast', () => {
+ expect(() => contrast({amount: 2})).not.toThrow();
+});
+
+test('contrast() rejects non-finite amount', () => {
+ expect(() => contrast({amount: Number.NaN})).toThrow(
+ '"amount" must be a finite number',
+ );
+});
+
+test('contrast() rejects amount below range', () => {
+ expect(() => contrast({amount: -0.1})).toThrow('"amount" must be >= 0');
+});
+
+test('contrast() amount produces distinct effect keys', () => {
+ const flat = contrast({amount: 0});
+ const neutral = contrast({amount: 1});
+ const boosted = contrast({amount: 2});
+ expect(
+ new Set([flat.effectKey, neutral.effectKey, boosted.effectKey]).size,
+ ).toBe(3);
+});
+
test('halftone() accepts default params', () => {
expect(() => halftone()).not.toThrow();
});