|
1 | 1 | import { Animated } from 'react-native'; |
2 | 2 |
|
| 3 | +import color from 'color'; |
| 4 | + |
3 | 5 | export const isAnimatedValue = ( |
4 | 6 | it: number | string | Animated.AnimatedInterpolation<number | string> |
5 | 7 | ): it is Animated.Value => it instanceof Animated.Value; |
| 8 | + |
| 9 | +// TODO: Revist this function to see if it's still needed |
| 10 | +export default function overlay<T extends Animated.Value | number>( |
| 11 | + elevation: T, |
| 12 | + surfaceColor: string |
| 13 | +): T extends number ? string : Animated.AnimatedInterpolation<number | string> { |
| 14 | + if (isAnimatedValue(elevation)) { |
| 15 | + const inputRange = [0, 1, 2, 3, 8, 24]; |
| 16 | + |
| 17 | + // @ts-expect-error: TS doesn't seem to refine the type correctly |
| 18 | + return elevation.interpolate({ |
| 19 | + inputRange, |
| 20 | + outputRange: inputRange.map((elevation) => { |
| 21 | + return calculateColor(surfaceColor, elevation); |
| 22 | + }), |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + // @ts-expect-error: TS doesn't seem to refine the type correctly |
| 27 | + return calculateColor(surfaceColor, elevation); |
| 28 | +} |
| 29 | + |
| 30 | +function calculateColor(surfaceColor: string, elevation: number = 1) { |
| 31 | + let overlayTransparency: number; |
| 32 | + if (elevation >= 1 && elevation <= 24) { |
| 33 | + overlayTransparency = elevationOverlayTransparency[elevation]; |
| 34 | + } else if (elevation > 24) { |
| 35 | + overlayTransparency = elevationOverlayTransparency[24]; |
| 36 | + } else { |
| 37 | + overlayTransparency = elevationOverlayTransparency[1]; |
| 38 | + } |
| 39 | + return color(surfaceColor) |
| 40 | + .mix(color('white'), overlayTransparency * 0.01) |
| 41 | + .hex(); |
| 42 | +} |
| 43 | + |
| 44 | +const elevationOverlayTransparency: Record<string, number> = { |
| 45 | + 1: 5, |
| 46 | + 2: 7, |
| 47 | + 3: 8, |
| 48 | + 4: 9, |
| 49 | + 5: 10, |
| 50 | + 6: 11, |
| 51 | + 7: 11.5, |
| 52 | + 8: 12, |
| 53 | + 9: 12.5, |
| 54 | + 10: 13, |
| 55 | + 11: 13.5, |
| 56 | + 12: 14, |
| 57 | + 13: 14.25, |
| 58 | + 14: 14.5, |
| 59 | + 15: 14.75, |
| 60 | + 16: 15, |
| 61 | + 17: 15.12, |
| 62 | + 18: 15.24, |
| 63 | + 19: 15.36, |
| 64 | + 20: 15.48, |
| 65 | + 21: 15.6, |
| 66 | + 22: 15.72, |
| 67 | + 23: 15.84, |
| 68 | + 24: 16, |
| 69 | +}; |
0 commit comments