Skip to content

🎥 Make videos programmatically with React

@remotion/starburst: Accept CSS color strings in starburst (#7926)

Jonny Burgerd5d14700a69f
diff --git a/packages/docs/docs/starburst/starburst-effect.mdx b/packages/docs/docs/starburst/starburst-effect.mdx
index 1481622b6f..7cddf17ff3 100644
--- a/packages/docs/docs/starburst/starburst-effect.mdx
+++ b/packages/docs/docs/starburst/starburst-effect.mdx
@@ -58,7 +58,7 @@ The number of rays in the starburst pattern. Must be between `2` and `100`.
### `colors`
-An array of hex color strings for the rays. Colors are assigned to rays cyclically. Must contain at least 2 colors.
+An array of CSS color strings for the rays, for example hex values, color names like `"red"` or `hsl()` colors. Colors are assigned to rays cyclically. Must contain at least 2 colors.
### `rotation?`
diff --git a/packages/docs/docs/starburst/starburst.mdx b/packages/docs/docs/starburst/starburst.mdx
index 050da640ab..996fe1e889 100644
--- a/packages/docs/docs/starburst/starburst.mdx
+++ b/packages/docs/docs/starburst/starburst.mdx
@@ -45,7 +45,7 @@ The number of rays in the starburst pattern. Must be between `2` and `100`.
### `colors`
-An array of hex color strings for the rays. Colors are assigned to rays cyclically. Must contain at least 2 colors.
+An array of CSS color strings for the rays, for example hex values, color names like `"red"` or `hsl()` colors. Colors are assigned to rays cyclically. Must contain at least 2 colors.
```tsx
<Starburst rays={12} colors={['#ff0066', '#6600ff', '#00ccff']} />
diff --git a/packages/starburst/src/Starburst.tsx b/packages/starburst/src/Starburst.tsx
index fb0aad3ef1..a911b94a9e 100644
--- a/packages/starburst/src/Starburst.tsx
+++ b/packages/starburst/src/Starburst.tsx
@@ -1,5 +1,4 @@
import React, {useCallback, useEffect, useRef, useState} from 'react';
-import {type SequenceControls, type SequenceSchema} from 'remotion';
import {
AbsoluteFill,
Internals,
@@ -8,9 +7,11 @@ import {
useVideoConfig,
type AbsoluteFillLayout,
type LayoutAndStyle,
+ type SequenceControls,
type SequenceProps,
+ type SequenceSchema,
} from 'remotion';
-import {hexToRgb} from './hex-to-rgb';
+import {colorToRgb} from './color-to-rgb';
export type StarburstProps = Omit<
SequenceProps,
@@ -218,10 +219,10 @@ const StarburstCanvas: React.FC<{
const pixelData = new Uint8Array(colors.length * 4);
for (let i = 0; i < colors.length; i++) {
- const rgb = hexToRgb(colors[i]);
- pixelData[i * 4] = Math.round(rgb[0] * 255);
- pixelData[i * 4 + 1] = Math.round(rgb[1] * 255);
- pixelData[i * 4 + 2] = Math.round(rgb[2] * 255);
+ const rgb = colorToRgb(colors[i]);
+ pixelData[i * 4] = rgb[0];
+ pixelData[i * 4 + 1] = rgb[1];
+ pixelData[i * 4 + 2] = rgb[2];
pixelData[i * 4 + 3] = 255;
}
diff --git a/packages/starburst/src/color-to-rgb.ts b/packages/starburst/src/color-to-rgb.ts
new file mode 100644
index 0000000000..f0a8440d86
--- /dev/null
+++ b/packages/starburst/src/color-to-rgb.ts
@@ -0,0 +1,7 @@
+import {NoReactInternals} from 'remotion/no-react';
+
+export const colorToRgb = (color: string): [number, number, number] => {
+ const packed = NoReactInternals.processColor(color);
+
+ return [(packed >>> 16) & 255, (packed >>> 8) & 255, packed & 255];
+};
diff --git a/packages/starburst/src/hex-to-rgb.ts b/packages/starburst/src/hex-to-rgb.ts
deleted file mode 100644
index 2e75816eeb..0000000000
--- a/packages/starburst/src/hex-to-rgb.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-export const hexToRgb = (hex: string): [number, number, number] => {
- const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
- if (!result) {
- throw new Error(`Invalid hex color: ${hex}`);
- }
-
- return [
- parseInt(result[1], 16) / 255,
- parseInt(result[2], 16) / 255,
- parseInt(result[3], 16) / 255,
- ];
-};
diff --git a/packages/starburst/src/starburst-effect.ts b/packages/starburst/src/starburst-effect.ts
index ad711cf02d..14fc27c24c 100644
--- a/packages/starburst/src/starburst-effect.ts
+++ b/packages/starburst/src/starburst-effect.ts
@@ -1,6 +1,6 @@
import type {SequenceSchema} from 'remotion';
import {Internals} from 'remotion';
-import {hexToRgb} from './hex-to-rgb';
+import {colorToRgb} from './color-to-rgb';
const {createEffect, createWebGL2ContextError} = Internals;
@@ -129,7 +129,7 @@ const validateStarburstEffectParams = (params: StarburstEffectParams): void => {
}
for (const c of r.colors) {
- hexToRgb(c);
+ colorToRgb(c);
}
};
@@ -364,10 +364,10 @@ export const starburst = createEffect<StarburstEffectParams, StarburstGlState>({
const {palettePixelData} = state;
for (let i = 0; i < r.colors.length; i++) {
- const rgb = hexToRgb(r.colors[i]);
- palettePixelData[i * 4] = Math.round(rgb[0] * 255);
- palettePixelData[i * 4 + 1] = Math.round(rgb[1] * 255);
- palettePixelData[i * 4 + 2] = Math.round(rgb[2] * 255);
+ const rgb = colorToRgb(r.colors[i]);
+ palettePixelData[i * 4] = rgb[0];
+ palettePixelData[i * 4 + 1] = rgb[1];
+ palettePixelData[i * 4 + 2] = rgb[2];
palettePixelData[i * 4 + 3] = 255;
}
}
diff --git a/packages/starburst/src/test/starburst-effect-params.test.ts b/packages/starburst/src/test/starburst-effect-params.test.ts
index f675ee4c10..9598adb6e4 100644
--- a/packages/starburst/src/test/starburst-effect-params.test.ts
+++ b/packages/starburst/src/test/starburst-effect-params.test.ts
@@ -1,4 +1,5 @@
import {expect, test} from 'bun:test';
+import {colorToRgb} from '../color-to-rgb.js';
import {starburst} from '../starburst-effect.js';
test('starburst() throws when rays is not passed', () => {
@@ -15,6 +16,15 @@ test('starburst() accepts valid params', () => {
).not.toThrow();
});
+test('starburst() accepts CSS named colors', () => {
+ expect(() => starburst({rays: 12, colors: ['red', 'blue']})).not.toThrow();
+});
+
+test('colorToRgb() parses CSS colors using Remotion color parsing', () => {
+ expect(colorToRgb('red')).toEqual([255, 0, 0]);
+ expect(colorToRgb('hsl(120, 100%, 50%)')).toEqual([0, 255, 0]);
+});
+
test('starburst() exposes its documentation link', () => {
expect(
starburst({rays: 12, colors: ['#ff0000', '#00ff00']}).definition