<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def animate_rainbow(self, group1='LEFT', group2='RIGHT', increment_by=0.1, sleeptime=0.1, duration=5, block=True):
 """ Gradually fade from one color to the next Animate for ``duration`` seconds. If ``duration`` is None animate for forever. Example: .. code-block:: python from ev3dev2.led import Leds leds = Leds() leds.animate_rainbow() """


        def _animate_rainbow():
            # state 0: (LEFT,RIGHT) from (0,0) to (1,0)...RED
            # state 1: (LEFT,RIGHT) from (1,0) to (1,1)...AMBER
            # state 2: (LEFT,RIGHT) from (1,1) to (0,1)...GREEN
            # state 3: (LEFT,RIGHT) from (0,1) to (0,0)...OFF
            state = 0
            left_value = 0
            right_value = 0
            MIN_VALUE = 0
            MAX_VALUE = 1
            self.all_off()
            start_time = dt.datetime.now()

            while True:

                if state == 0:
                    left_value += increment_by
                elif state == 1:
                    right_value += increment_by
                elif state == 2:
                    left_value -= increment_by
                elif state == 3:
                    right_value -= increment_by
                else:
                    raise Exception("Invalid state {}".format(state))

                # Keep left_value and right_value within the MIN/MAX values
                left_value = min(left_value, MAX_VALUE)
                right_value = min(right_value, MAX_VALUE)
                left_value = max(left_value, MIN_VALUE)
                right_value = max(right_value, MIN_VALUE)

                self.set_color(group1, (left_value, right_value))
                self.set_color(group2, (left_value, right_value))

                if state == 0 and left_value == MAX_VALUE:
                    state = 1
                elif state == 1 and right_value == MAX_VALUE:
                    state = 2
                elif state == 2 and left_value == MIN_VALUE:
                    state = 3
                elif state == 3 and right_value == MIN_VALUE:
                    state = 0

                if self.animate_thread_stop or duration_expired(start_time, duration):
                    break

                sleep(sleeptime)

            self.animate_thread_stop = False
            self.animate_thread_id = None

        self.animate_stop()

        if block:
            _animate_rainbow()
        else:
            self.animate_thread_id = _thread.start_new_thread(_animate_rainbow, ())