public AnimationSet ToAnimationSet() { AnimationSet set = new AnimationSet(); // Collect all colors used and stuff them in the palette var colors = new Dictionary <Color32, int>(); int index = 0; foreach (var anim in animations) { foreach (var animTrack in anim.tracks) { foreach (var keyframe in animTrack.keyframes) { var color = keyframe.color; if (color.a != 0) { int ignore = 0; if (!colors.TryGetValue(keyframe.color, out ignore)) { colors.Add(keyframe.color, index); index++; } } // else its a special color } } } // Add the colors to the palette set.palette = new byte[colors.Count * 3]; foreach (var ci in colors) { Color32 color = ColorMapping.RemapColor(ci.Key); int i = ci.Value; set.palette[i * 3 + 0] = color.r; set.palette[i * 3 + 1] = color.g; set.palette[i * 3 + 2] = color.b; } int currentTrackOffset = 0; int currentKeyframeOffset = 0; var anims = new List <Animation>(); var tracks = new List <AnimationTrack>(); var rgbTracks = new List <RGBTrack>(); var keyframes = new List <RGBKeyframe>(); // Add animations for (int animIndex = 0; animIndex < animations.Count; ++animIndex) { var editAnim = animations[animIndex]; var anim = new Animation(); anim.duration = (ushort)(editAnim.duration * 1000.0f); anim.tracksOffset = (ushort)currentTrackOffset; anim.trackCount = (ushort)editAnim.tracks.Sum(t => t.ledIndices.Count); anim.animationEvent = (byte)editAnim.@event; anim.specialColorType = (byte)editAnim.@specialColorType; anims.Add(anim); // Now add tracks for (int j = 0; j < editAnim.tracks.Count; ++j) { var editTrack = editAnim.tracks[j]; foreach (var led in editTrack.ledIndices) { var track = new AnimationTrack(); var rgbTrack = new RGBTrack(); rgbTrack.keyframesOffset = (ushort)currentKeyframeOffset; rgbTrack.keyFrameCount = (byte)editTrack.keyframes.Count; track.trackOffset = (ushort)rgbTracks.Count; rgbTracks.Add(rgbTrack); track.ledIndex = (byte)led; // Now add keyframes for (int k = 0; k < editTrack.keyframes.Count; ++k) { var editKeyframe = editTrack.keyframes[k]; int colorIndex = AnimationSet.SPECIAL_COLOR_INDEX; if (editKeyframe.color.a != 0) { colorIndex = colors[editKeyframe.color]; } var keyframe = new RGBKeyframe(); keyframe.setTimeAndColorIndex((ushort)(editKeyframe.time * 1000.0f), (ushort)colorIndex); keyframes.Add(keyframe); } currentKeyframeOffset += editTrack.keyframes.Count; tracks.Add(track); } } currentTrackOffset += editAnim.tracks.Sum(t => t.ledIndices.Count); } set.keyframes = keyframes.ToArray(); set.rgbTracks = rgbTracks.ToArray(); set.tracks = tracks.ToArray(); set.animations = anims.ToArray(); set.Compress(); return(set); }