pub fn color_scale(c: (u8, u8, u8, u8), coef: f32) -> (u8, u8, u8, u8)
Examples found in repository?
examples/demo/shared/character.rs (line 56)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
fn render_character(
    mut root_console: ResMut<RootConsole>,
    query: Query<(&Character, &Position)>,
    level_map: Res<LevelMap>,
    light_map: NonSend<LightMap>,
) {
    for (character, position) in &query {
        if !level_map
            .0
            .is_in_fov(position.x as usize, position.y as usize)
        {
            continue;
        }
        let (color, penumbra) = if character.light {
            (character.color, false)
        } else {
            let light = light_map
                .0
                .pixel(position.x as u32, position.y as u32)
                .unwrap();

            let penumbra = is_penumbra(light, 100);
            let mut color = color_mul(character.color, light);
            if penumbra {
                color = color_scale(color, LIGHT_COEF);
            }
            (color, penumbra)
        };
        root_console.ascii(
            position.character_x(),
            position.character_y(),
            if penumbra { '?' as u16 } else { character.ch },
        );
        root_console.fore(position.character_x(), position.character_y(), color);
    }
}
More examples
Hide additional examples
examples/demo/light.rs (line 147)
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
fn compute_lightmap(
    mut light_map: NonSendMut<LightMap>,
    size: Res<LevelSize>,
    query: Query<(&Light, &Position)>,
    level_map: Res<LevelMap>,
) {
    let light_map = &mut light_map.0;
    *light_map = Image::new_empty(size.width as u32 * 2, size.height as u32 * 2);
    let mut fov = FovRestrictive::new();
    for (light, light_position) in &query {
        let (px, py, intensity, radius) = if light.flicker {
            // alter light position, radius and intensity over time
            (
                light_position.x + (LIGHT_FLICKER_MOVE * (simplex(light.t) - 0.5)),
                light_position.y + (LIGHT_FLICKER_MOVE * (simplex(light.t + 2.0) - 0.5)),
                light.intensity + LIGHT_FLICKER_INTENSITY * (simplex(light.t + 4.0) - 0.5),
                light.radius * (1.0 + LIGHT_FLICKER_RADIUS * (simplex(light.t + 6.0) - 0.5)),
            )
        } else {
            (
                light_position.x,
                light_position.y,
                light.intensity,
                light.radius,
            )
        };

        let minx = ((px - radius).floor() as i32).max(0) as u32;
        let maxx = ((px + radius).ceil() as i32).min(light_map.width() as i32 - 1) as u32;
        let miny = ((py - radius).floor() as i32).max(0) as u32;
        let maxy = ((py + radius).ceil() as i32).min(light_map.height() as i32 - 1) as u32;
        let width = maxx - minx + 1;
        let height = maxy - miny + 1;
        let mut map = MapData::new(width as usize, height as usize);
        for y in miny..=maxy {
            for x in minx..=maxx {
                map.set_transparent(
                    (x - minx) as usize,
                    (y - miny) as usize,
                    level_map.0.is_transparent(x as usize, y as usize),
                );
            }
        }
        fov.compute_fov(
            &mut map,
            px as usize - minx as usize,
            py as usize - miny as usize,
            radius as usize,
            true,
        );
        let light_color = color_scale(light.color, intensity);
        let radius_squared = radius * radius;
        let radius_coef = 1.0 / (1.0 + radius_squared / 20.0);
        for y in miny..=maxy {
            for x in minx..=maxx {
                if map.is_in_fov((x - minx) as usize, (y - miny) as usize) {
                    let dx = x as f32 - px;
                    let dy = y as f32 - py;
                    // good looking lights.
                    let squared_dist = dx * dx + dy * dy;
                    let intensity_coef = 1.0 / (1.0 + squared_dist / 20.0);
                    let intensity_coef = intensity_coef - radius_coef;
                    let intensity_coef = intensity_coef / (1.0 - radius_coef);
                    if intensity_coef > 0.0 {
                        let light = color_blend(BLACK, light_color, intensity_coef);
                        let cur_light = light_map.pixel(x, y).unwrap();
                        light_map.put_pixel(x, y, color_add(light, cur_light));
                    }
                }
            }
        }
    }
}