Trait Hue

Source
pub trait Hue: Sized {
    // Required methods
    fn with_hue(&self, hue: f32) -> Self;
    fn hue(&self) -> f32;
    fn set_hue(&mut self, hue: f32);

    // Provided method
    fn rotate_hue(&self, degrees: f32) -> Self { ... }
}
Expand description

Trait for manipulating the hue of a color.

Required Methods§

Source

fn with_hue(&self, hue: f32) -> Self

Return a new version of this color with the hue channel set to the given value.

Source

fn hue(&self) -> f32

Return the hue of this color [0.0, 360.0].

Source

fn set_hue(&mut self, hue: f32)

Sets the hue of this color.

Provided Methods§

Source

fn rotate_hue(&self, degrees: f32) -> Self

Return a new version of this color with the hue channel rotated by the given degrees.

Examples found in repository?
examples/3d/animated_material.rs (line 42)
13fn setup(
14    mut commands: Commands,
15    asset_server: Res<AssetServer>,
16    mut meshes: ResMut<Assets<Mesh>>,
17    mut materials: ResMut<Assets<StandardMaterial>>,
18) {
19    commands.spawn((
20        Camera3d::default(),
21        Transform::from_xyz(3.0, 1.0, 3.0).looking_at(Vec3::new(0.0, -0.5, 0.0), Vec3::Y),
22        EnvironmentMapLight {
23            diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
24            specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
25            intensity: 2_000.0,
26            ..default()
27        },
28    ));
29
30    let cube = meshes.add(Cuboid::new(0.5, 0.5, 0.5));
31
32    const GOLDEN_ANGLE: f32 = 137.507_77;
33
34    let mut hsla = Hsla::hsl(0.0, 1.0, 0.5);
35    for x in -1..2 {
36        for z in -1..2 {
37            commands.spawn((
38                Mesh3d(cube.clone()),
39                MeshMaterial3d(materials.add(Color::from(hsla))),
40                Transform::from_translation(Vec3::new(x as f32, 0.0, z as f32)),
41            ));
42            hsla = hsla.rotate_hue(GOLDEN_ANGLE);
43        }
44    }
45}
46
47fn animate_materials(
48    material_handles: Query<&MeshMaterial3d<StandardMaterial>>,
49    time: Res<Time>,
50    mut materials: ResMut<Assets<StandardMaterial>>,
51) {
52    for material_handle in material_handles.iter() {
53        if let Some(material) = materials.get_mut(material_handle) {
54            if let Color::Hsla(ref mut hsla) = material.base_color {
55                *hsla = hsla.rotate_hue(time.delta_secs() * 100.0);
56            }
57        }
58    }
59}
More examples
Hide additional examples
examples/3d/query_gltf_primitives.rs (line 31)
16fn find_top_material_and_mesh(
17    mut materials: ResMut<Assets<StandardMaterial>>,
18    mut meshes: ResMut<Assets<Mesh>>,
19    time: Res<Time>,
20    mat_query: Query<(
21        &MeshMaterial3d<StandardMaterial>,
22        &Mesh3d,
23        &GltfMaterialName,
24    )>,
25) {
26    for (mat_handle, mesh_handle, name) in mat_query.iter() {
27        // locate a material by material name
28        if name.0 == "Top" {
29            if let Some(material) = materials.get_mut(mat_handle) {
30                if let Color::Hsla(ref mut hsla) = material.base_color {
31                    *hsla = hsla.rotate_hue(time.delta_secs() * 100.0);
32                } else {
33                    material.base_color = Color::from(Hsla::hsl(0.0, 0.9, 0.7));
34                }
35            }
36
37            if let Some(mesh) = meshes.get_mut(mesh_handle) {
38                if let Some(VertexAttributeValues::Float32x3(positions)) =
39                    mesh.attribute_mut(Mesh::ATTRIBUTE_POSITION)
40                {
41                    for position in positions {
42                        *position = (
43                            position[0],
44                            1.5 + 0.5 * ops::sin(time.elapsed_secs() / 2.0),
45                            position[2],
46                        )
47                            .into();
48                    }
49                }
50            }
51        }
52    }
53}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§