react-native-reanimated
Version:
More powerful alternative to Animated library for React Native.
51 lines (42 loc) • 1.24 kB
text/typescript
;
import type { ColorValue } from 'react-native';
import {
processColor,
processColorNumber,
type ProcessedColor,
type ValueProcessor,
} from '../../../../common';
export const ERROR_MESSAGES = {
invalidColor: (color: unknown) =>
`Invalid color value: ${JSON.stringify(color)}`,
};
export const processColorSVG: ValueProcessor<
ColorValue | number,
number | false | string
> = (value) => {
const processed = processColorNumber(value);
if (value === 'none') {
return 0;
}
if (processed !== null) {
// Same convention as the main `processColor`: the `transparent` keyword
// becomes the `false` sentinel; explicit zero-alpha colours pass through.
if (processed === 0 && value === 'transparent') {
return false;
}
return processed;
}
if (value === 'currentColor') {
return 'currentColor';
}
throw new Error(`[Reanimated] ${ERROR_MESSAGES.invalidColor(value)}`);
};
export const processColorOrPlatformColorSVG: ValueProcessor<
ColorValue | number,
number | false | string | ProcessedColor
> = (value, context) => {
if (typeof value === 'object' && value !== null) {
return processColor(value);
}
return processColorSVG(value, context);
};