Need help with RGB rainbow colors

I might have misunderstood. If you were asking about fading the current color to dark, then gradually bringing up another color, then you might want to try something like this (untested):

void write_rgb_components(unsigned char r,unsigned char g,unsigned char b)
{  static unsigned char old_r, old_g, old_b;

   if (r != old_r) analogWrite(R,old_r = r);
   if (g != old_g) analogWrite(G,old_g = g);
   if (b != old_b) analogWrite(B,old_b = b);
   delay(50);
}

void fade_from(unsigned char r,unsigned char g,unsigned char b)
{  unsigned i;
   for (i = 255; i != 0; i--)
      write_rgb_components((i * r) >> 8,(i * g) >> 8,(i * b) >> 8);
   write_rgb_components(0,0,0);
}

void fade_to(unsigned char r,unsigned char g,unsigned char b)
{  unsigned i;
   for (i = 0; i < 256; i++)
      write_rgb_components((i * r) >> 8,(i * g) >> 8,(i * b) >> 8);
   write_rgb_components(r,g,b);
}