Skip to content

🎥 Make videos programmatically with React

small refactor and test more cases, works wel!

Jonny Burger5fad66c8ffde
diff --git a/packages/core/src/interpolateColors.ts b/packages/core/src/interpolateColors.ts
index 9c19f1c956..4bb18c068e 100644
--- a/packages/core/src/interpolateColors.ts
+++ b/packages/core/src/interpolateColors.ts
@@ -402,46 +402,34 @@ function normalizeColor(color: string): number {
throw new Error(`invalid color string ${color} provided`);
}
-export const opacity = (c: number): number => {
+const opacity = (c: number): number => {
return ((c >> 24) & 255) / 255;
};
-export const red = (c: number): number => {
+const red = (c: number): number => {
return (c >> 16) & 255;
};
-export const green = (c: number): number => {
+const green = (c: number): number => {
return (c >> 8) & 255;
};
-export const blue = (c: number): number => {
+const blue = (c: number): number => {
return c & 255;
};
-export const rgbaColor = (
- r: number,
- g: number,
- b: number,
- alpha = 1
-): string => {
+const rgbaColor = (r: number, g: number, b: number, alpha: number): string => {
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
};
-export function processColorInitially(color: string): number {
+function processColorInitially(color: string): number {
let normalizedColor = normalizeColor(color);
normalizedColor = ((normalizedColor << 24) | (normalizedColor >>> 8)) >>> 0; // argb
return normalizedColor;
}
-export function isColor(value: unknown): boolean {
- if (typeof value !== 'string') {
- return false;
- }
- return processColorInitially(value) != null;
-}
-
-export function processColor(color: string): number {
+function processColor(color: string): number {
const normalizedColor = processColorInitially(color);
return normalizedColor;
}
@@ -451,81 +439,50 @@ const interpolateColorsRGB = (
inputRange: readonly number[],
colors: readonly number[]
) => {
- const r = Math.round(
- interpolate(
+ const [r, g, b, a] = [red, green, blue, opacity].map((f, i) => {
+ const unrounded = interpolate(
value,
inputRange,
- colors.map((c) => red(c)),
+ colors.map((c) => f(c)),
{
extrapolateLeft: 'clamp',
extrapolateRight: 'clamp',
}
- )
- );
- const g = Math.round(
- interpolate(
- value,
- inputRange,
- colors.map((c) => green(c)),
- {
- extrapolateLeft: 'clamp',
- extrapolateRight: 'clamp',
- }
- )
- );
- const b = Math.round(
- interpolate(
- value,
- inputRange,
- colors.map((c) => blue(c)),
- {
- extrapolateLeft: 'clamp',
- extrapolateRight: 'clamp',
- }
- )
- );
- const a = interpolate(
- value,
- inputRange,
- colors.map((c) => opacity(c)),
- {
- extrapolateLeft: 'clamp',
- extrapolateRight: 'clamp',
+ );
+ if (f === opacity) {
+ return Number(unrounded.toFixed(3));
}
- );
+ return Math.round(unrounded);
+ });
return rgbaColor(r, g, b, a);
};
export const interpolateColors = (
input: number,
inputRange: readonly number[],
- outputRange: readonly string[],
- colorSpace: 'RGB' = 'RGB'
+ outputRange: readonly string[]
): string => {
- if (
- typeof input === 'undefined' ||
- typeof inputRange === 'undefined' ||
- typeof outputRange === 'undefined'
- ) {
- throw new Error('input or inputRange or outputRange can not be undefined');
+ if (typeof input === 'undefined') {
+ throw new TypeError('input can not be undefined');
+ }
+ if (typeof inputRange === 'undefined') {
+ throw new TypeError('inputRange can not be undefined');
+ }
+ if (typeof outputRange === 'undefined') {
+ throw new TypeError('outputRange can not be undefined');
}
if (inputRange.length !== outputRange.length) {
- throw new Error(
+ throw new TypeError(
'inputRange (' +
inputRange.length +
- ') and outputRange (' +
+ ' values provided) and outputRange (' +
outputRange.length +
- ') must have the same length'
+ ' values provided) must have the same length'
);
}
const processedOutputRange = outputRange.map((c) => processColor(c));
- if (colorSpace === 'RGB') {
- return interpolateColorsRGB(input, inputRange, processedOutputRange);
- }
- throw new Error(
- `invalid color space provided: ${colorSpace}. Supported values are: ['RGB', 'HSV']`
- );
+ return interpolateColorsRGB(input, inputRange, processedOutputRange);
};
diff --git a/packages/core/src/test/interpolateColors.test.ts b/packages/core/src/test/interpolateColors.test.ts
index c2c1c77806..d5abd94afd 100644
--- a/packages/core/src/test/interpolateColors.test.ts
+++ b/packages/core/src/test/interpolateColors.test.ts
@@ -1,13 +1,6 @@
import {interpolateColors} from '../interpolateColors';
import {expectToThrow} from './expect-to-throw';
-test('Throws if color space is not right', () => {
- expectToThrow(() => {
- //@ts-expect-error
- interpolateColors(1, [0, 1], ['#ffaadd', '#fabedf'], 'GFR');
- }, /invalid color space provided: GFR. Supported values are: \['RGB', 'HSV'\]/);
-});
-
test('Throws if color string is not right', () => {
expectToThrow(() => {
interpolateColors(1, [0, 1], ['#fabgdf', '#ffaabb']);
@@ -17,14 +10,22 @@ test('Throws if color string is not right', () => {
test('Throws error if input, inputRange or outputRange is undefined', () => {
expectToThrow(() => {
//@ts-expect-error
- interpolateColors(undefined, undefined, undefined);
- }, /input or inputRange or outputRange can not be undefined/);
+ interpolateColors(undefined, ['#aaa', '#bbb'], ['#fff', '#000']);
+ }, /input can not be undefined/);
+ expectToThrow(() => {
+ //@ts-expect-error
+ interpolateColors(1, undefined, ['#fff', '#000']);
+ }, /inputRange can not be undefined/);
+ expectToThrow(() => {
+ //@ts-expect-error
+ interpolateColors(1, ['#fff', '#000'], undefined);
+ }, /outputRange can not be undefined/);
});
test('inputRange and outputRange must be of same length', () => {
expectToThrow(() => {
interpolateColors(1, [1, 2, 3], ['#ffffff', '#aaaaaa']);
- }, /inputRange \(3\) and outputRange \(2\) must have the same length/);
+ }, /inputRange \(3 values provided\) and outputRange \(2 values provided\) must have the same length/);
});
test('Basic interpolate Colors', () => {
@@ -38,3 +39,21 @@ test('Color shorthands', () => {
'rgba(250, 191, 223, 1)'
);
});
+
+test('Color names', () => {
+ expect(interpolateColors(1, [0, 1], ['red', 'blue'])).toBe(
+ 'rgba(0, 0, 255, 1)'
+ );
+});
+
+test('Mix transparency', () => {
+ expect(interpolateColors(0.5, [0, 1], ['transparent', 'blue'])).toBe(
+ 'rgba(0, 0, 128, 0.5)'
+ );
+});
+
+test('HSV', () => {
+ expect(
+ interpolateColors(0.5, [0, 1], ['hsla(120, 100%, 25%, 0)', 'blue'])
+ ).toBe('rgba(0, 64, 128, 0.5)');
+});