Keyframe Color Interpolators#

Color animation explained in this tutorial and how to use different color space interpolators.

import numpy as np

from fury import actor, window
from fury.animation import Animation
from fury.animation.interpolator import (
    hsv_color_interpolator,
    lab_color_interpolator,
    step_interpolator,
    xyz_color_interpolator,
)
from fury.animation.timeline import Timeline
from fury.colormap import distinguishable_colormap

scene = window.Scene()

showm = window.ShowManager(
    scene, size=(900, 768), reset_camera=False, order_transparent=True
)
/opt/homebrew/Caskroom/miniforge/base/envs/py39/lib/python3.9/site-packages/sphinx_gallery/gen_rst.py:722: UserWarning: We'll no longer accept the way you call the __init__ function in future versions of FURY.

Here's how to call the Function __init__: __init__(self_value, scene='value', title='value', size='value', png_magnify='value', reset_camera='value', order_transparent='value', interactor_style='value', stereo='value', multi_samples='value', max_peels='value', occlusion_ratio='value')

  exec(self.code, self.fake_main.__dict__)

Initializing positions of the cubes that will be color-animated.

cubes_pos = np.array(
    [
        [[-2, 0, 0]],
        [[0, 0, 0]],
        [[2, 0, 0]],
        [[4, 0, 0]],
        [[6, 0, 0]],
    ]
)

Static labels for different interpolators (for show)

linear_text = actor.vector_text("Linear", (-2.64, -1, 0))
lab_text = actor.vector_text("LAB", (-0.37, -1, 0))
hsv_text = actor.vector_text("HSV", (1.68, -1, 0))
xyz_text = actor.vector_text("XYZ", (3.6, -1, 0))
step_text = actor.vector_text("Step", (5.7, -1, 0))
scene.add(step_text, lab_text, linear_text, hsv_text, xyz_text)
/opt/homebrew/Caskroom/miniforge/base/envs/py39/lib/python3.9/site-packages/sphinx_gallery/gen_rst.py:722: UserWarning: We'll no longer accept the way you call the vector_text function in future versions of FURY.

Here's how to call the Function vector_text: vector_text(text='value', pos='value', scale='value', color='value', direction='value', extrusion='value', align_center='value')

  exec(self.code, self.fake_main.__dict__)

Creating an animation to animate the actor. Also cube actor is provided for each timeline to handle as follows: Animation(actor), Animation(list_of_actors), or actors can be added later using animation.add() or animation.add_actor()

anim_linear_color = Animation(actor.cube(cubes_pos[0]))
anim_LAB_color = Animation(actor.cube(cubes_pos[1]))
anim_HSV_color = Animation(actor.cube(cubes_pos[2]))
anim_XYZ_color = Animation(actor.cube(cubes_pos[3]))
anim_step_color = Animation(actor.cube(cubes_pos[4]))
/opt/homebrew/Caskroom/miniforge/base/envs/py39/lib/python3.9/site-packages/sphinx_gallery/gen_rst.py:722: UserWarning: We'll no longer accept the way you call the __init__ function in future versions of FURY.

Here's how to call the Function __init__: __init__(self_value, actors='value', length='value', loop='value', motion_path_res='value')

  exec(self.code, self.fake_main.__dict__)

Creating a timeline to control all the animations (one for each color interpolation method)

timeline = Timeline(playback_panel=True)

Adding animations to a Timeline.

timeline.add_animation(
    [anim_linear_color, anim_LAB_color, anim_HSV_color, anim_XYZ_color, anim_step_color]
)

Setting color keyframes#

Setting the same color keyframes to all the animations

First, we generate some distinguishable colors

colors = distinguishable_colormap(nb_colors=4)

Then, we set them as keyframes for the animations

for t in range(0, 20, 5):
    col = colors.pop()
    anim_linear_color.set_color(t, col)
    anim_LAB_color.set_color(t, col)
    anim_HSV_color.set_color(t, col)
    anim_XYZ_color.set_color(t, col)
    anim_step_color.set_color(t, col)

Changing the default scale interpolator to be a step interpolator The default is linear interpolator for color keyframes

anim_HSV_color.set_color_interpolator(hsv_color_interpolator)
anim_LAB_color.set_color_interpolator(lab_color_interpolator)
anim_step_color.set_color_interpolator(step_interpolator)
anim_XYZ_color.set_color_interpolator(xyz_color_interpolator)

Adding the main timeline to the show manager

showm.add_animation(timeline)

interactive = False

if interactive:
    showm.start()

window.record(scene, out_path="viz_keyframe_animation_colors.png", size=(900, 768))
viz color interpolators
/opt/homebrew/Caskroom/miniforge/base/envs/py39/lib/python3.9/site-packages/sphinx_gallery/gen_rst.py:722: UserWarning: We'll no longer accept the way you call the record function in future versions of FURY.

Here's how to call the Function record: record(scene='value', cam_pos='value', cam_focal='value', cam_view='value', out_path='value', path_numbering='value', n_frames='value', az_ang='value', magnification='value', size='value', reset_camera='value', screen_clip='value', stereo='value', verbose='value')

  exec(self.code, self.fake_main.__dict__)

Total running time of the script: (0 minutes 0.114 seconds)

Gallery generated by Sphinx-Gallery