Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align handling colors with RN #5825

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

maciekstosio
Copy link
Contributor

@maciekstosio maciekstosio commented Mar 22, 2024

Summary

Fixes #5746. Not all color formats and syntax supported by RN is supported by reanimated. This PR should align with RN implementation. An example for rgb,rgba,hsl,hsl with comma and without (for view and Animated.View):

Before After
Color.Berfore.mov
Color.After.mov

Test plan

Reused RN tests.

Example from the video
import { StyleSheet, View, Text } from 'react-native';

import Animated, {
  useAnimatedStyle,
  useSharedValue,
  withRepeat,
  withTiming,
} from 'react-native-reanimated';
import React, { useEffect } from 'react';

const CompareColumnAnimated = ({
  title,
  withComma,
}: {
  title: string;
  withComma?: boolean;
}) => {
  const sv = useSharedValue(0);

  const toRgb = (value: number) => {
    'worklet';
    return Math.floor(Math.abs(value) * 255);
  };

  useEffect(() => {
    sv.value = withRepeat(withTiming(1, { duration: 1000 }), -1, true);
    return () => {
      sv.value = 0;
    };
  }, [sv]);

  return (
    <View style={{}}>
      <View style={styles.header}>
        <Text>{title}</Text>
      </View>
      <Animated.View
        style={[
          styles.box,
          useAnimatedStyle(() => ({
            backgroundColor: `rgb(${toRgb(sv.value)} 255 255)`
              .split(' ')
              .join(withComma ? ', ' : ' '),
          })),
        ]}
      />
      <Animated.View
        style={[
          styles.box,
          useAnimatedStyle(() => ({
            backgroundColor: `rgba(${toRgb(sv.value)} 255 255 0.5)`
              .split(' ')
              .join(withComma ? ', ' : ' '),
          })),
        ]}
      />
      <Animated.View
        style={[
          styles.box,
          useAnimatedStyle(() => ({
            backgroundColor: `hsl(${sv.value * 180} 100% 50%)`
              .split(' ')
              .join(withComma ? ', ' : ' '),
          })),
        ]}
      />
      <Animated.View
        style={[
          styles.box,
          useAnimatedStyle(() => ({
            backgroundColor: `hsla(${sv.value * 180} 100% 50% 0.5)`
              .split(' ')
              .join(withComma ? ', ' : ' '),
          })),
        ]}
      />
    </View>
  );
};

const CompareColumn = ({
  title,
  withComma,
}: {
  title: string;
  withComma?: boolean;
}) => (
  <View style={{}}>
    <View style={styles.header}>
      <Text>{title}</Text>
    </View>
    <View
      style={[
        styles.box,
        {
          backgroundColor: 'rgb(100 255 255)'
            .split(' ')
            .join(withComma ? ', ' : ' '),
        },
      ]}
    />
    <View
      style={[
        styles.box,
        {
          backgroundColor: 'rgba(100 255 255 0.5)'
            .split(' ')
            .join(withComma ? ', ' : ' '),
        },
      ]}
    />
    <View
      style={[
        styles.box,
        {
          backgroundColor: 'hsl(100 100% 50%)'
            .split(' ')
            .join(withComma ? ', ' : ' '),
        },
      ]}
    />
    <View
      style={[
        styles.box,
        {
          backgroundColor: 'hsla(100 100% 50% 0.5)'
            .split(' ')
            .join(withComma ? ', ' : ' '),
        },
      ]}
    />
  </View>
);

export default function EmptyExample() {
  return (
    <View style={styles.container}>
      <CompareColumnAnimated title="anim comma" withComma />
      <CompareColumn title="view comma" withComma />
      <CompareColumnAnimated title="anim no comma" />
      <CompareColumn title="view no comma" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    flexDirection: 'row',
  },
  box: {
    width: 100,
    height: 100,
  },
  header: { justifyContent: 'center', alignSelf: 'center' },
});

@maciekstosio maciekstosio marked this pull request as ready for review March 22, 2024 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useAnimatedStyle does not support hsl(x y% z%) syntax for colors
1 participant